• Notice regarding image hosts and attachments. See here.

php mysql results into an array?

Associate
Joined
19 Jul 2006
Posts
1,847
I think this should be simple but cant find the answer.
I have the following which returns some data.
Code:
<?php 
$result = mysql_query("SELECT col1, col2 FROM table WHERE col1 LIKE 'something%'");
if (!$result) {
    die("Error");
}
I need to use the output to create a table but I need to modify the data returned before putting this in the table.
i though echo $result['col1']; would print the value of col1 but this didn't work.

What I need to do is a table of results in the following format
|some string + value of col1 | col2/10 |

So how do i get back these values to manipulate

TIA
 
Interestingly unlike PGSQL, MySQL has no way of exporting the contents of a query to a 2d array like.... $array[row][col] , my understanding of PGSQL is that it will do this.
 
It's easy enough to get data into a 2d array:
PHP:
$result = mysql_query("SELECT ...");
$data = array();
while ($row = mysql_fetch_assoc($result) {
	$data[] = $row;
}
// $data is now a 2d array containing all of the results

Using the above it's easy to write a wrapper function to do this.
 
Back
Top Bottom