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);
...
?>
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.