tags:

views:

55

answers:

4

My config table:

name    | value
--------+------
version | 1.5.6
title   | test

How I try and get it:

$getCfg = mysql_query("SELECT * FROM config");
$config = mysql_fetch_assoc($getCfg);

echo $config['title'];

Equals to:

Notice: Undefined index: title in C:\web\index.php on line 5

How would I get the value where the name is title?

The above doesn't work..well if I add WHERE title = 'test' and then echo $config['title']

+4  A: 

Try this instead:

echo $config['name'];

You need to index the result of mysql_fetch_assoc with the field name from the database which is "name".

Andrew Hare
-1. doesn't answer the question.
Tor Valamo
+1  A: 

@Andrew is right. To avoid this problems in the future, print the contents of the config object:

echo "<pre>";
print_r($config);
echo "</pre>";
AlfaTeK
A: 

How would I do to get the value where the name is title?

The above doesn't work..well if I add WHERE title = 'test' and then echo $config['title']

SELECT * from config where name like 'title'

and you get that value with

echo $config['name']

You have to refer to the data by the column name, SQL doesn't automatically search for you, although that would be killer...

Chris Thompson
+3  A: 
$getCfg = mysql_query("SELECT * FROM config");
$config = array();
while ($row = mysql_fetch_assoc($getCfg)) {
  $config[$row['name']] = $row['value'];
}

echo $config['title'];
Tor Valamo