Jump to content

PHP and MySQL: Displaying something within a table


mstevens

Recommended Posts

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);
...
?>

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.