如何从基于php的mysql中的mysql获取数据? [英] how to get data from mysql in php based on url?
问题描述
我在mysql db中有一个页表,其中有'page_name','title',content,'author'字段.我想使用 http://www.domain.com/index.php之类的php来获取它?page = page_name .我该怎么做 ?请帮助
i have a page table in mysql db with 'page_name', 'title', content, 'author' fields. I want to get it with php like http://www.domain.com/index.php?page=page_name . How do i do it ? pls help
推荐答案
使用超级全局$ _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"];
这篇关于如何从基于php的mysql中的mysql获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!