如何从基于php的mysql中的mysql获取数据? [英] how to get data from mysql in php based on url?

查看:67
本文介绍了如何从基于php的mysql中的mysql获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆