Please bear with me, I’m new here – and I’m just starting out with PHP. To be honest, this is my first project, so please be merciful. 🙂
$row = mysql_fetch_array(mysql_query("SELECT message FROM data WHERE code = '". (int) $code ."' LIMIT 1"));
echo $row['message'];
Would this be enough to fetch the message from the database based upon a pre-defined ‘$code’ variable? I have already successfully connected to the database.
This block of code seems to return nothing – just a blank space. 🙁
I would be grateful of any suggestions and help. 🙂
UPDATE:
Code now reads:
<?php
error_reporting(E_ALL);
// Start MySQL Connection
REMOVED FOR SECURITY
// Check if code exists
if(mysql_num_rows(mysql_query("SELECT code FROM data WHERE code = '$code'"))){
echo 'Hooray, that works!';
$row = mysql_fetch_array(mysql_query("SELECT message FROM data WHERE code = '". (int) $code ."' LIMIT 1")) or die(mysql_error());
echo $row['message'];
}
else {
echo 'That code could not be found. Please try again!';
}
mysql_close();
?>
It’s best not to chain functions together like this since if the query fails the fetch will also appear to fail and cause an error message that may not actually indicate what the real problem was.
Also, don’t wrap quotes around integer values in your SQL queries.
And the standard “don’t use
mysql_*functions because deprecated blah blah blah”…If you’re still getting a blank response you might want to check that you’re not getting 0 rows returned. Further testing would also include
echoing out the query to see if it’s formed properly, and running it yourself to see if it’s returning the correct data.