Help with a simple request

My settings table:

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

How will I try it:

 $getCfg = mysql_query("SELECT * FROM config"); $config = mysql_fetch_assoc($getCfg); echo $config['title']; 

Equality:

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

How to get the value in which the name is the title?

The above does not work. Well, if I add WHERE title = 'test', then echo $ config ['title']

+4
source share
4 answers
 $getCfg = mysql_query("SELECT * FROM config"); $config = array(); while ($row = mysql_fetch_assoc($getCfg)) { $config[$row['name']] = $row['value']; } echo $config['title']; 
+3
source

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 the "name".

+4
source

@ Andrey is right. To avoid these problems in the future, print the contents of the configuration object:

 echo "<pre>"; print_r($config); echo "</pre>"; 
+1
source

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

The above does not work. Well, if I add WHERE title = 'test', then echo $ config ['title']

 SELECT * from config where name like 'title' 

and you get this value with

 echo $config['name'] 

You must reference the data by column name, SQL does not automatically search for you, although that would be a killer ...

0
source

All Articles