I’m trying to make a php page that i can navigate to like… http://mydomain.com?id=12345
in my mysql table there is a id column and a text column….how can I make my php page get the ID, find it, then return whats in the text cell in the same row and echo it on the page?
Here’s what i’ve come up with so far..i’m mostly stuck on what I should do with the Mysql query. then how to actually get the data into a variable that I can echo to the page. Thanks!
EDIT: made a little progress…
<?php
mysql_connect("my.mysql.com", "user", "pass");
mysql_select_db("mydb");
$id= $_GET['id'];
$result = mysql_query("SELECT text FROM mytable WHERE id='$id'")
or die(mysql_error());
echo nl2br($result);
?>
Right after you’ve constructed your query, you pass it to the database and fetch the results
Important note: your data should always be sanitised before inputting it in a database, to avoid sql injection
Eg.: imagine someone put “‘; drop table mytable;” as the id in the url. Then passing this to mysql would delete your table.
Note2: when outputting your post, make sure to escape certain characters: instead of < and > you should put < and >
Recommended tutorial
Script copied from here