0

The array I want is given below;

Array ( [0] => John [1] => toby [2] => hrithik )

I tried below code for

$r = array();
$queryx = mysql_query("SELECT `name` FROM `student` WHERE `status` = '1'") or die ('Query is invalid: ' . mysql_error());

while ($row = mysql_fetch_assoc($queryx)) {
$r[] = $row;
}


print_r($r);

The output I am getting is given below;

Array ( [0] => Array ( [name] => John ) [1] => Array ( [name] => toby ) [2] => Array ( [name] => hrithik ) )

2
  • @Deep I tried but its fetching only one row and same array which I was getting earlier. Commented Feb 28, 2017 at 6:12
  • then no need to declare as array , just use as in the answer given below Commented Feb 28, 2017 at 6:20

3 Answers 3

3

Try this:

while ($row = mysql_fetch_assoc($queryx)) {
    $r[] = $row['name'];
}

It will put name in $r array on different index like 0, 1, 2 and so on

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

Comments

1

try the following code below,

$r = array();
$i = 0;
$queryx = mysql_query("SELECT `name` FROM `student` WHERE `status` = '1'") or die ('Query is invalid: ' . mysql_error());
while ($row = mysql_fetch_assoc($queryx)) {
$r[$i] = row["name"];
$i++;
}
print_r($r);

Comments

0

what you want is mysql-fetch-row http://php.net/manual/en/function.mysql-fetch-row.php

you can refer api to check how these function works mysql_fetch_array http://php.net/manual/en/function.mysql-fetch-array.php mysql_fetch_assoc http://php.net/manual/en/function.mysql-fetch-assoc.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.