ohdang888 Posted December 26, 2007 Share Posted December 26, 2007 i am learning MySQL and PHP. i created a very simple code here. in the table "test" from the database "realted"... there is one column of info, called "name"... there are 11 rows of data, each one has chacaters in it. but the results its showing is just 11 blank rows (the rows were created on the page, i can highlight them) why is the info not coming up? <?php mysql_connect("----", "----", "----") or die(mysql_error()); mysql_select_db("related") or die(mysql_error()); $result = mysql_query("SELECT * FROM test") or die(mysql_error()); $row = mysql_fetch_array( $result ); while($row = mysql_fetch_array($result)){ echo $row['name']; echo "<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/83245-solved-easy-mysql-query-error/ Share on other sites More sharing options...
revraz Posted December 26, 2007 Share Posted December 26, 2007 Remove this line or comment it out. You repeat it right after $row = mysql_fetch_array( $result ); Quote Link to comment https://forums.phpfreaks.com/topic/83245-solved-easy-mysql-query-error/#findComment-423461 Share on other sites More sharing options...
ohdang888 Posted December 26, 2007 Author Share Posted December 26, 2007 so... this? <?php mysql_connect("localhost", "root", "pancakes1") or die(mysql_error()); mysql_select_db("related") or die(mysql_error()); $result = mysql_query("SELECT * FROM test") or die(mysql_error()); //$row = mysql_fetch_array( $result ); while($row = mysql_fetch_array($result)){ echo $row['name']; echo '<br>'; } ?> that didn;t do anything at all. besudes, doesn't $row need to be specified about what it is??? Quote Link to comment https://forums.phpfreaks.com/topic/83245-solved-easy-mysql-query-error/#findComment-423462 Share on other sites More sharing options...
revraz Posted December 26, 2007 Share Posted December 26, 2007 while($row = mysql_fetch_array($result)){ echo $row[0]; echo '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/83245-solved-easy-mysql-query-error/#findComment-423465 Share on other sites More sharing options...
Darkstar Posted December 26, 2007 Share Posted December 26, 2007 the while sets $row so you don't need the above, commented, line. As for it still showing blank. try print_r($row) in the while instead of the echo to see what it really stored. Work from there Quote Link to comment https://forums.phpfreaks.com/topic/83245-solved-easy-mysql-query-error/#findComment-423466 Share on other sites More sharing options...
ohdang888 Posted December 26, 2007 Author Share Posted December 26, 2007 thanks revraz! it worked! Quote Link to comment https://forums.phpfreaks.com/topic/83245-solved-easy-mysql-query-error/#findComment-423580 Share on other sites More sharing options...
revraz Posted December 26, 2007 Share Posted December 26, 2007 If "name" is your field name, then you could use mysql_fetch_assoc instead of array and then your $row['name'] would work Quote Link to comment https://forums.phpfreaks.com/topic/83245-solved-easy-mysql-query-error/#findComment-423583 Share on other sites More sharing options...
ohdang888 Posted December 26, 2007 Author Share Posted December 26, 2007 ok thanks. i have anoher question though... this is new new query $result = mysql_query("SELECT * FROM test ORDER BY RAND(id) LIMIT 5") i want to pick 5 random ones and randomize their order. thorpe. in a previous forum, told me to add "ORDER BY RAND(id) LIMIT 5", but i am getting this error: Unknown column 'id' in 'order clause' whats wrong with the ID? Quote Link to comment https://forums.phpfreaks.com/topic/83245-solved-easy-mysql-query-error/#findComment-423591 Share on other sites More sharing options...
revraz Posted December 26, 2007 Share Posted December 26, 2007 I replied to that one, replace id with whatever your ID Field name is. Quote Link to comment https://forums.phpfreaks.com/topic/83245-solved-easy-mysql-query-error/#findComment-423593 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.