简体   繁体   中英

Get a MYSQL Query from .php?id=

Let's just say for the purpose of this question, we have a blog. Now, on that default page for the blog displays the latest... 10 posts let's say.

These posts have been retrieved from a MySql query in PHP.

What I want to know is, how do you then, create a link on that latest blog posts page which takes you to the actual page of the blog post?

I'm guessing it's going to be something like:

http://www.example.com/showposts.php?id=1

But I don't know how to set that up to get the post query from the MySql database?

Please help me out,

Adam.

$result =  mysql_query("select * from table where id = " . intval($_GET['id']));

An even cleaner way would be using prepared statements (via mysqli or PDO). However, that's outside the scope of the question and you'll already find tons of information about it here on SO.

Just don't forget to escape the value as well:

$res = mysql_query('SELECT * FROM yourTable WHERE tableID = ' . mysql_real_escape_string(trim(intval($_GET['id']))));

Just to be on the safe side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM