nisroc Posted September 11, 2009 Share Posted September 11, 2009 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(); ?> Quote Link to comment Share on other sites More sharing options...
Alex Posted September 11, 2009 Share Posted September 11, 2009 $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); Quote Link to comment Share on other sites More sharing options...
corbin Posted September 11, 2009 Share Posted September 11, 2009 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 } } } Quote Link to comment Share on other sites More sharing options...
nisroc Posted September 12, 2009 Author Share Posted September 12, 2009 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. 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.