0

I am new in php.Instead of using

$result=mysql_fetch_assoc($qry)
$result['id']; 

i want to pass a variable instead of id like this

$id='id';
$result=mysql_fetch_assoc($qry)
    $result[$id]; 

I have tried like this but not working.How can i pass a variable as argument to fetch a value from database

Blessan Kurien
  • 1,645
  • 9
  • 31
  • 63
  • [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you choose PDO, [here is a good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – Kevin Nov 07 '14 at 07:22
  • http://php.net/manual/en/mysqli.query.php that page has all you want to know about it. Also this: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – Hanky Panky Nov 07 '14 at 07:22
  • I cant understand that thing. – Blessan Kurien Nov 07 '14 at 07:24
  • @Root, If you are using PHP5.5 then every function that starts with `mysql_` will generates error. – jogesh_pi Nov 07 '14 at 07:30

1 Answers1

1

mysql_fetch_assoc provides you the multi-dimentional array.

$result = array(
    array('id'=>`some_value`)
);

So you can do like this:

$ID = "id";
foreach($result as $row) {
    echo $row[$ID] . "\n";
}

NOTE:

Please, don't use mysql_* functions in new code.

Community
  • 1
  • 1
jogesh_pi
  • 9,762
  • 4
  • 37
  • 65