how-to replace hard coded array with data from mysql?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
tinybit
Forum Newbie
Posts: 2
Joined: Sun Feb 17, 2008 7:18 pm

how-to replace hard coded array with data from mysql?

Post by tinybit »

Hi everyone,
I have a problem that I can't sort out.

I'm trying to replace a hard coded piece of code with dynamic data.

I have;

$data=array(
"PETER" => "Peter is nice",
"JANE" => "Jane is nice too"
);

I want to change this piece of the script so I can store these names in mysql but have no success doing so. I tried to replace the above with this but it's not working;

mysql_select_db($database_xyz, $xyz);
$query_person = "SELECT name,description";
$rs_person = mysql_query($query_person, $xyz) or die(mysql_error());
$data = mysql_fetch_object($rs_person);

So $data should hold the exact same set of data but the script does not work.

Anyone who can see what is wrong and be kind enough to tell me? Really appreciate it. :oops:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how-to replace hard coded array with data from mysql?

Post by califdon »

Your SQL syntax is incomplete. You must specify a Table.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how-to replace hard coded array with data from mysql?

Post by Christopher »

Code: Select all

$query_person = "SELECT name,description FROM people";
$rs_person = mysql_query($query_person, $xyz) or die(mysql_error());
$data = array();
while ($row = mysql_fetch_assoc($rs_person)) {
    $data[$row['name']] = $row['description'];
}
(#10850)
tinybit
Forum Newbie
Posts: 2
Joined: Sun Feb 17, 2008 7:18 pm

Re: how-to replace hard coded array with data from mysql?

Post by tinybit »

Thanks a lot everyone - problem solved. Arborint's solution example worked right away. :D
Post Reply