English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have a database with a connection and a valid result set from a mysql_query call. In PHP the function mysql_fetch_row($resultSet) returns an array of all the elements in one row.

My question is: Where is the function which returns an array of all elements in all rows?

For instance if my query returns 5 rows of a database I want it to pass back in array with 5 objects (each an array of that rows data)

2006-08-28 12:16:08 · 3 answers · asked by Gehan G 3 in Computers & Internet Programming & Design

You would think there is a native function in the mysql class which would do this automaticlly? Why did they leave out this commonly used function?

2006-08-28 12:52:07 · update #1

Thanks for the advice jaspal. I've studied the mysql library, and I understand the difference between a numerical and assoc array.

My question concerns the fact that all of those functions return 1 row from the result source.

I want to dump all rows, without having to use a while loop to place them in to an array. Why when I do:
$result = mysql_querty("bla bla",$connection)
print_r($result)

I get back: ResourceID4 or something like that.

I know I can do a while with
print_r(mysql_fetch_row($result) until null

I was hopeing there was some function I was not seeing, or looking over which would pass a complete array of all data returned from the query.

Dream code:
print_r(mysql_fetch_all(mysql_query('bla bla', $connection));

and have it return
Array (
[0] Joe -> [0] Smith -> [1] 342
[1] Mike -> [0] Smith -> [1] 344
[2] Deric ->[0] Scott -> [1] 345
}

2006-08-28 18:39:27 · update #2

Apparentlly the function I'm looking for exists so I'm giving the points to the first poster.

2006-08-29 03:49:19 · update #3

3 answers

you can use a while like this:

$res=mysql_query(,,,);
while($row=mysql_fetch_assoc($res)){
print_r($row);
}

this will print all rows and if you want can make an Array from All rows results ...

2006-08-28 12:29:32 · answer #1 · answered by narges 1 · 0 0

Use a multidimensional array.

Loop (with counter ($i))
$array[$i] = mysql_fetch_row($resultset);

2006-08-28 21:12:16 · answer #2 · answered by geekfound 2 · 1 0

mysql_fetch_array -- Fetch a result row as an associative array, a numeric array, or both


Example. mysql_fetch_array() with MYSQL_NUM

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}

mysql_free_result($result);
?>



Example. mysql_fetch_array() with MYSQL_ASSOC

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf("ID: %s Name: %s", $row["id"], $row["name"]);
}

mysql_free_result($result);
?>



Example. mysql_fetch_array() with MYSQL_BOTH

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("ID: %s Name: %s", $row[0], $row["name"]);
}

mysql_free_result($result);
?>

2006-08-28 15:05:49 · answer #3 · answered by aadhunik.com social networking s 3 · 1 0

fedest.com, questions and answers