Jump to content

MySQL Select Problem w/PHP


Sekwel

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.