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; } ?> 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. 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 )) { 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? 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! 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); ?> 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=" } 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']; ?> 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; ?> 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. 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 Link to comment https://forums.phpfreaks.com/topic/157547-solved-resource-id-3/#findComment-830707 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.