Sekwel Posted April 18, 2011 Share Posted April 18, 2011 I'm working on my website, and I'm having a bit of trouble in getting PHP to select the proper data. I'm trying to select usernames from my database where rank is equal to one (the highest, in my system). As such, I attempted this code; // Connection info above this line... mysql_select_db("users"); $query = mysql_query ("SELECT displayname FROM login WHERE rank = 1"); $query_a = mysql_fetch_array($query); var_dump($query_a); The var_dump resulting from that is as follows; array 0 => string 'Seltzer' (length=7) 'displayname' => string 'Seltzer' (length=7) Everything is working correctly, except for the fact that my database contains two displayname rows where rank is equal to one (EDIT: Two distinct rows). In fact, I can run a search of it in phpMyAdmin and get the two that my PHP code should be returning. phpMyAdmin generated the following query, which Inserted into my code in order to double-check things; SELECT `displayname` FROM `login` WHERE `rank` =1 LIMIT 0 , 30 Even after I swapped my queries, the PHP code still returned the same var_dump as above. Complicating things further, I've noticed another function I've made, which queries "SELECT rank WHERE displayname = '$displayname'", functions perfectly. I've gotten rid of pretty much any source of the error I could think of; I'm testing the function on an otherwise empty page, I've removed any classes being used, and I've tried about a million different queries. Can someone help me out with this? I'm being held up by it and I'm sure that this is a simple fix. Thanks in advance, Dustin Quote Link to comment https://forums.phpfreaks.com/topic/234067-mysql-select-problem-wphp/ Share on other sites More sharing options...
spiderwell Posted April 18, 2011 Share Posted April 18, 2011 the first example is missing the table name? Quote Link to comment https://forums.phpfreaks.com/topic/234067-mysql-select-problem-wphp/#findComment-1203042 Share on other sites More sharing options...
Sekwel Posted April 18, 2011 Author Share Posted April 18, 2011 That was my mistake; I just forgot to include it in the example. Everything is using proper syntax and whatnot. Quote Link to comment https://forums.phpfreaks.com/topic/234067-mysql-select-problem-wphp/#findComment-1203045 Share on other sites More sharing options...
kenrbnsn Posted April 18, 2011 Share Posted April 18, 2011 The function mysql_fetch_array returns an array containing two entries for each field. You probably want to use the function mysql_fetch_assoc instead. Ken Quote Link to comment https://forums.phpfreaks.com/topic/234067-mysql-select-problem-wphp/#findComment-1203054 Share on other sites More sharing options...
Sekwel Posted April 18, 2011 Author Share Posted April 18, 2011 The function mysql_fetch_array returns an array containing two entries for each field. You probably want to use the function mysql_fetch_assoc instead. Ken Still getting the same problem. One row should be "Seltzer," the other should be "theOtherGuy" (actual username. Haha). Even if I run a "SELECT *" in my PHP code, it still only returns the "Seltzer" row. Quote Link to comment https://forums.phpfreaks.com/topic/234067-mysql-select-problem-wphp/#findComment-1203061 Share on other sites More sharing options...
kenrbnsn Posted April 18, 2011 Share Posted April 18, 2011 The mysql fetch functions return one row at a time, to see all the rows you need to put it into a loop: <?php $query = "SELECT displayname FROM login WHERE rank = 1"; $rs = mysql_query($query); while ($row = mysql_fetch_assoc($rs)) { echo $row['displayname'] . '<br>'; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/234067-mysql-select-problem-wphp/#findComment-1203066 Share on other sites More sharing options...
Pikachu2000 Posted April 18, 2011 Share Posted April 18, 2011 EDIT: Yeah, what he said ^^^ . . . If you're expecting more than one record to be returned, you need to mysql_fetch_* in a loop. Each iteration of the fetch function returns one record. while( $array = mysql_fetch_assoc($result) ) { var_dump($array); } Quote Link to comment https://forums.phpfreaks.com/topic/234067-mysql-select-problem-wphp/#findComment-1203071 Share on other sites More sharing options...
Sekwel Posted April 18, 2011 Author Share Posted April 18, 2011 I should have seen that sooner. Thanks, everyone. I'm going to get on with this project now. Haha. Quote Link to comment https://forums.phpfreaks.com/topic/234067-mysql-select-problem-wphp/#findComment-1203074 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.