0

This query keeps returning 2 entries and I can not work out why.

I have a database holding my members in a table called members. At the moment it has 5 records as it is just for testing. When I use the following query I get two results, my current id which I am logged into the website with and a blank result.

$memarray = mysql_query("SELECT id FROM members") or die ("unable to get info");
$fArray = mysql_fetch_array($memarray);

echo count($fArray);    //shows 2
echo $fArray[0];        //shows '102' my current id
echo $fArray[1];        //shows nothing
3
  • can you show the output? and use msqli_query instead of mysql_query Commented Jan 21, 2014 at 11:15
  • added the outputs above thanks Commented Jan 21, 2014 at 11:18
  • Do yourself a favor and replace mysql_* with mysqli_* as mysql_* is deprecated. us3.php.net/manual/en/changelog.mysql.php Commented Jan 21, 2014 at 11:25

4 Answers 4

3

mysql_fetch_array() stores one row in an array. To retrieve all rows, you have to call it repeatedly.

while ($row = mysql_fetch_array($result))
{
    ...
}

To store all rows in an array, simply do

$rows = array();
while ($row = mysql_fetch_array($result))
    $rows[] = $row;

This, however, is rather inefficient. If possible, you should process your data within the while-loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. can i ask how i could push each value into a new array to hold all the ids? thanks
0

By using mysql_fetch_array($memarray) you get the first row from the query result.
The default second parameter for the function is "MYSQL_BOTH".
This means you get [0=>'someID', 'id'=>"someID"]
You can use "MYSQL_ASSOC" instead.
See this php manual

Comments

0

Use this:

$memarray = mysqli_query("SELECT id FROM members") or die ("unable to get info");
while ($fArray = mysqli_fetch_array($memarray))
{
   echo $fArray["id"];
}

Comments

0

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both http://php.net/mysql_fetch_array

$memarray = mysql_query("SELECT id FROM members") or die ("unable to get info");
$fArray = mysql_fetch_array($memarray);
echo '<pre>';
print_r($fArray);

result would be some thing like this :

Array ( [0] => 23 [id] => 23 )

obviously count($fArray) returns 2

to count all recors

$memarray = mysql_query("SELECT COUNT(id) as count FROM members") or die ("unable to get info");
$fArray = mysql_fetch_assoc($memarray);
echo $fArray['count'];

or

$memarray = mysql_query("SELECT id FROM members") or die ("unable to get info");
echo mysql_num_rows($memarray);

to fetch all your records

while($row = mysql_fetch_assoc($memarray))
{
    print_r($row);
}

and I also suggest take look at this notice :‌

Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_fetch_array() PDOStatement::fetch()

https://www.php.net/manual/en/mysqli-result.fetch-array.php https://www.php.net/manual/en/pdostatement.fetch.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.