Jump to content

PHP - Column names in array


cancer10

Recommended Posts

ok, see the code below

 

<?
$rsQry = mysql_query ("select * from tblCustomer");
$arrQry  = mysql_fetch_row($rsQry);
?>

 

 

Now if I try to fetch a Customer's Name from the  "Customer_Name" column from the "tblCustomer" table with the following code then I dont get anything:

 

<?

echo $arrQry["Customer_Name"];

?>

 

Why?

 

 

PS: I have tried this way and it works:

 

<?

echo $arrQry[0];

?>

 

But I want to use the column name instead of the column number. Any idea?

 

 

 

Thanx

Link to comment
https://forums.phpfreaks.com/topic/50228-php-column-names-in-array/
Share on other sites

use

$arrQry  = mysql_fetch_assoc($rsQry);

 

PS or you can use mysql_fetch_array() in which case both $arrQry[0] and $arrQry['Custome_Name'] both work

 

Now that is a quick and effective reply

 

 

Thanx a ton mate you solved my problem :)

 

 

2 Quick questiona -

 

1) does using mysql_fetch_array() will take more resource compared to mysql_fetch_row()?

 

2) Is this the only difference between mysql_fetch_array()  and mysql_fetch_row()?

 

 

 

 

Thanx again :)

1 ) mysql_fetch_row() is fastest - returns an indexed array

 

2 ) mysql_fetch_assoc() returns associative array, keys are the column names

 

3 ) mysql_fetch_array() by default returns an array with numeric and colnames as indexes (ie each value is returned twice. (you can supply an option second argument to return numeric or associative or both.

 

If you try each of the options followed by

 

echo '<pre>', print_r($arrQry, true), '</pre>';

 

you will see the diffences

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.