berry05 Posted May 10, 2009 Share Posted May 10, 2009 i get this error "Resource id #3 " when i run the script... <?php // Make a MySQL Connection mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("math") or die(mysql_error()); // Get all the data from the "example" table $result = mysql_query("SELECT * FROM problems ORDER BY RAND() LIMIT 0,1;") or die(mysql_error()); // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo $result; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/157547-solved-resource-id-3/ Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 I think you meant to echo something in the $row array instead of $result. Quote Link to comment https://forums.phpfreaks.com/topic/157547-solved-resource-id-3/#findComment-830682 Share on other sites More sharing options...
berry05 Posted May 10, 2009 Author Share Posted May 10, 2009 mhm when i do that it now just says.. Array ... wouldn't i need to change this part then? while($row = mysql_fetch_array( $result )) { Quote Link to comment https://forums.phpfreaks.com/topic/157547-solved-resource-id-3/#findComment-830684 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 $row is an array. What is it you want to do? Quote Link to comment https://forums.phpfreaks.com/topic/157547-solved-resource-id-3/#findComment-830686 Share on other sites More sharing options...
berry05 Posted May 10, 2009 Author Share Posted May 10, 2009 I have 3 math problems in my table...I want to show a random math problem when i run the script...just 1 out of the 3... btw thanks for helping me out man! Quote Link to comment https://forums.phpfreaks.com/topic/157547-solved-resource-id-3/#findComment-830690 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 Tell me what output you get when you run this: <?php // Make a MySQL Connection mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("math") or die(mysql_error()); // Get all the data from the "example" table $result = mysql_query("SELECT * FROM problems ORDER BY RAND() LIMIT 0,1;") or die(mysql_error()); // READ!! // if you put LIMIT 0, 1, then the maximum rows you get back is 1. // no need for a loop $result = mysql_fetch_assoc($result); var_dump($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/157547-solved-resource-id-3/#findComment-830693 Share on other sites More sharing options...
berry05 Posted May 10, 2009 Author Share Posted May 10, 2009 now i get this array(1) { ["problems"]=> string(4) "1+1=" } Quote Link to comment https://forums.phpfreaks.com/topic/157547-solved-resource-id-3/#findComment-830696 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 <?php // Make a MySQL Connection mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("math") or die(mysql_error()); // Get all the data from the "example" table $result = mysql_query("SELECT * FROM problems ORDER BY RAND() LIMIT 0,1;") or die(mysql_error()); // READ!! // if you put LIMIT 0, 1, then the maximum rows you get back is 1. // no need for a loop $result = mysql_fetch_assoc($result); echo $result['problems']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/157547-solved-resource-id-3/#findComment-830699 Share on other sites More sharing options...
berry05 Posted May 10, 2009 Author Share Posted May 10, 2009 im learning! aha i figured it out by using arrays! now i dont need to use the database.. here's the simple code.. <?php $problems = array ( "1+1=", "1+2=", "1+3=" ); $random = $problems[array_rand($problems)]; echo $random; ?> Quote Link to comment https://forums.phpfreaks.com/topic/157547-solved-resource-id-3/#findComment-830704 Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 It's probably best to use the DB though. Quote Link to comment https://forums.phpfreaks.com/topic/157547-solved-resource-id-3/#findComment-830705 Share on other sites More sharing options...
berry05 Posted May 10, 2009 Author Share Posted May 10, 2009 LOL I need to learn PHP waaay better than what I know now..aha Quote Link to comment https://forums.phpfreaks.com/topic/157547-solved-resource-id-3/#findComment-830707 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.