mysql_fetch_array +MySQL ...confused on how it works

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
c_323_h
Forum Newbie
Posts: 1
Joined: Thu Mar 15, 2007 11:30 am

mysql_fetch_array +MySQL ...confused on how it works

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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";
}
Post Reply