Page 1 of 1

mysql_fetch_array +MySQL ...confused on how it works

Posted: Thu Mar 15, 2007 11:46 am
by c_323_h
Hey everyone, I have a question about how mysql_fetch_array...

My PHP code looks like this:

$query = "my query here";
$result = mysql_query($query);
$row = mysql_fetch_array($result,MYSQL_NUM);

I ran a MySQL query through the client and it returns this (which is how I want it):

Code: Select all

+------------------------+-----------------------------+
|              date      |             name            |
+------------------------+-----------------------------+
|       Wednesday        |             Bill            |
|       Wednesday        |             Bob             |
|       Thursday         |             Joe             |
+------------------------+-----------------------------+
Where date and name are the two columns.

Now, how would I reference these values after using mysql_fetch_array()? I tried

echo $row[0]. " " .$row[1]; but it doesn't output anything.

So I would I output the array?

Thanks

Posted: Thu Mar 15, 2007 12:08 pm
by volka
try

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', true);

$mysql = mysql_connect( 'host', 'mysqluser', 'password for that mysql account' ) or die(mysql_error());
mysql_select_db('name of the database', $mysql) or die(mysql_error());

$query = "SELECT x,y,z FROM foobar WHERE yadda='y'";
$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($result, MYSQL_ASSOC);
if ( false===$row ) {
	echo '<div>No results</div>';
}
else {
	echo $row['x'], "<br />\n";
}