Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

help with simple query

Tags:

sql

php

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']

like image 997
Remy Avatar asked Feb 28 '23 18:02

Remy


2 Answers

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

like image 159
Andrew Hare Avatar answered Mar 08 '23 07:03

Andrew Hare


$getCfg = mysql_query("SELECT * FROM config");
$config = array();
while ($row = mysql_fetch_assoc($getCfg)) {
  $config[$row['name']] = $row['value'];
}

echo $config['title'];
like image 25
Tor Valamo Avatar answered Mar 08 '23 09:03

Tor Valamo