Jump to content

Random quotes (noob)


nisroc

Recommended Posts

Im very new to php/mysql and what i am trying to do is pull a random quote from a table and display it on a webpage. So far the code below is  what i have but does not appear to work. On the database table I have ID and quote, id being the entry of the quote. Can anyone please help me figure out what i am doing wrong?

 

<?php

define ('HOSTNAME', 'localhost_or_hostname');
define ('USERNAME', 'username');
define ('PASSWORD', 'database_password');
define ('DATABASE_NAME', 'database_name');

$db = mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die ('Cannot connect to MySQL.');

mysql_select_db(db);

$query = "SELECT * FROM quotes WHERE id=1 and id=2 ORDER by rand() LIMIT 1";

$result = mysql_query($query);


echo $result;


mysql_free_result($result);
mysql_close();
?>

 

Link to comment
Share on other sites

$query = "SELECT * FROM quotes ORDER by RAND() LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo $row['quote'];

 

Edit: Also change this:

 

mysql_select_db(db);

 

to

mysql_select_db(DATABASE_NAME, $db);

Link to comment
Share on other sites

Edit:  AlexWD beat me to it (a long time ago...  lol), but I'll go ahead and post since I talked about checking the success of previous operations.

 

 

Well, I see two things wrong:

 

mysql_select_db(db);

 

Your constant is called DATABASE_NAME, so perhaps you meant something like:

 

mysql_select_db(DATABASE_NAME);

 

or:

 

mysql_select_db(DATABASE_NAME, $db);

 

 

Then the second error is that $result does not contain the results of the query.  It contains a resource that points to the results of the query.  (Or it could be set to false if the query failed.)

 

You will need to use one of the following functions to extract the actual result from the resource.

 

mysql_fetch_assoc

mysql_fetch_array

mysql_fetch_row

mysql_result

 

 

Oh, and by the way, at the end of script execution, PHP will automatically close opened resources (like DB connections), so the mysql_close() is unnecessary (it does no harm though).

 

 

 

 

And, you shouldn't assume things won't fail.  For example, you should check that $db is not FALSE, since calling mysql_select_db on a FALSE value instead of a database resource will cause a warning to be thrown.

 

 

Something like:

 

 

$db = mysql_connect(....);

if($db) {

    if(mysql_select_db(DATABASE_NAME, $db)) {

        $q = "....";

        $result = mysql_query($q, $db);

        if($result) {

            //do something with $result

        }

    }

}

Link to comment
Share on other sites

I got it working nicely thanks corbin and alex. I looked at the errors you both pointed out and studied up on them played with it a bit and it fit right in. I do have another question here. This is a small database and will only be used once within the page. This page is included into the main page. My question is if i wanted to have show previous quote, random quote, next quote can it be done? like i said this is an include page not a page that stands on its own. I worried  that i will sit there and  try  figure out how to do this then find out the include page will open up after alone a when one of the features is used.

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.