mstevens Posted December 2, 2014 Share Posted December 2, 2014 Greetings, My current code logs into a database, opens a table named randomproverb, randomly selects 1 proverb phrase, and then SHOULD display the proverb in the footer of my web page. As of right now, the best I can do is get it to display "Array", but not the text proverb... this code below actually causes my whole footer to not even show up. Please help! <?php include("inc_connect.php"); //Connects to the database, does work properly, already tested $Proverb = "randomproverb"; $SQLproverb = "SELECT * FROM $Proverb ORDER BY RAND() LIMIT 1"; $QueryResult = @mysql_query($SQLproverb, $DBConnect); while (($Row = mysql_fetch_assoc($QueryResult)) !== FALSE) { echo "<p style = 'text-align:center'>" . {$Row[proverb]} . "</p>\n"; } $SQLString = "UPDATE randomproverb SET display_count = display_count + 1 WHERE proverb = $QueryResult[]"; $QueryResult = @mysql_query($SQLstring, $DBConnect); ... ?> Quote Link to comment Share on other sites More sharing options...
hansford Posted December 3, 2014 Share Posted December 3, 2014 You need to quote your fields when pulling from the database. $Row['proverb'] Also, you might want to use htmlspecialchars() when displaying your data as html. Quote Link to comment Share on other sites More sharing options...
maxxd Posted December 3, 2014 Share Posted December 3, 2014 You also don't need the curly braces around your variable if you're concatenating in a string. I'm not sure if it throws an error if you do use them in this situation, but you don't need them. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 3, 2014 Share Posted December 3, 2014 A couple of other things Dont use SELECT * You only select 1 record, so don't need while loop Your update should use the primary key to select the record You should be using mysqli or PDO, not mysql_ functions $SQLproverb = "SELECT id, proverb FROM $Proverb ORDER BY RAND() LIMIT 1"; $QueryResult = mysql_query($SQLproverb, $DBConnect); $Row = mysql_fetch_assoc($QueryResult); echo "<p style = 'text-align:center'>{$Row[proverb]}</p>\n"; $SQLString = "UPDATE randomproverb SET display_count = display_count + 1 WHERE id = {$Row[id]}"; $QueryResult = mysql_query($SQLstring, $DBConnect); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.