# RAND() with a generated link

I have a project from my college. I'm making a site which will display different quotations each time the page is loaded. I use the following code:

$con = mysql_connect("localhost","root",""); if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xlsx_db", $con);$result = mysql_query("SELECT * FROM sheet1 ORDER BY RAND() LIMIT 1");

if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; }$row = mysql_fetch_array($result); echo "<b>Quote: </b>"; echo$row['quote']."<br>";
echo $row['by']."<br>"; ?>  All works fine. But my teacher told me to make the text link-able. So that if anyone copy that link of the quote, that case, he can pest the code on the browser and can see that quote again. Could anyone please help me with this task? Any kind of help is really appreciated. ## 3 Answers Your link should look like this: <a href="http://www.yoursite.com/quotes?q_id=123">Permalink</a>  In your php code, you can grab the selected quote id like this: $q_id = $_GET['q_id'];  To check if the q_id has been set: if (array_key_exists('q_id',$_GET)) {
// the q_id was specified
}


To check that the q_id is numeric (important to do before plugging it into a mysql query):

if (is_numeric($_GET['q_id'])) { // q_id is a number }  To fetch that single quote: $result = mysql_query("SELECT * FROM sheet1 WHERE q_id = " . $q_id);  ### Deprecation of mysql_ functions PHP functions that start with mysql_ have been deprecated as of PHP 5.5.0. If you are in a position to do so, please consider updating your code to use the MySQLi or PDO extensions instead. • +1 I prefer this as a refresh will still produce a random quote and a link to return to that page. – Waygood Jul 12 '13 at 14:06 you'd need an ID for each quote in the database and add ?id=1245 after your domain and page if($_GET['id']){
$result = mysql_query("SELECT * FROM sheet1 WHERE id ".$_GET['id']."");
} else {
$result = mysql_query("SELECT * FROM sheet1 ORDER BY RAND() LIMIT 1"); }  • www.yoursite.com/quotes?id=;DROP TABLE sheet1;-- – Joe Frambach Jul 12 '13 at 14:08 • I use PDO prepared statement i kinda forgot about that – Kevin Roy Jul 12 '13 at 14:18 Here's a full solution from @eggyals comment Assuming quote_id is the index on the sheet1 table <?php$con = mysql_connect("localhost","root","");
if (!$con){ die('Could not connect: ' . mysql_error()); } mysql_select_db("xlsx_db",$con);
//get quote from url
$quote_id=intval(urldecode($_GET['quote_id']));
// if not set get a random one
if($quote_id==0) {$result = mysql_query("SELECT * FROM sheet1 ORDER BY RAND() LIMIT 1") or die('Could not run query: ' . mysql_error());
$row = mysql_fetch_array($result);
$get=$_GET;
$get['quote_id']=$row['quote_id'];
header("Location:".$_SERVER['PHP_SELF'].'?'.http_build_query($get));
exit();
}
// get the specified quote
$result = mysql_query("SELECT * FROM sheet1 WHERE quote_id=".intval($quote_id)." LIMIT 1") or die('Could not run query: ' . mysql_error());
$row = mysql_fetch_array($result);
echo "<b>Quote: </b>";
echo $row['quote']."<br>"; echo$row['by']."<br>";
?>

• Hi, thank you very much for the answer. Having a problem. My code returning with an id that is 0. I set the column "quote_id" as index on mysql database. but still show it is 0. and also I'm facing problem of "undefined index: quote_id"
– Provash Shoumma
Jul 12 '13 at 17:38
• in that case you should $quote_id=0; if(isset($_GET['quote_id'])) $quote_id=intval(urldecode($_GET['quote_id'])); BUT Joe Frambach's answer is better!
– Waygood
Jul 15 '13 at 15:25