且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从基于php的mysql中的mysql获取数据?

更新时间:2023-02-26 09:41:40

使用超级全局$ _GET数组访问GET参数:

Access the GET parameter using the super global $_GET array:

$pagename = $_GET['page'];

然后编写数据库查询.请注意,您必须转义来自外部的所有数据:

Then write a query for the database. Note that you have to escape every data that comes from the outside:

$sql = "SELECT * FROM page_table WHERE page_name = '%s' LIMIT 1";
$sql = sprintf(%sql, mysql_real_escape_string($pagename));

然后执行查询并检查其是否有效.

Then execute the query and check that it worked.

$result = mysql_query($sql);
if(!$result) {
    // error occured
}

使用功能mysql_fetch_assoc访问数据:

$data = mysql_fetch_assoc($result);

您现在可以访问此关联数组中的所有数据:

You can now access all data in this asscociative array:

echo $data["title"];