Page 1 of 1

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

Posted: Sun Feb 17, 2008 7:38 pm
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:

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

Posted: Sun Feb 17, 2008 10:34 pm
by califdon
Your SQL syntax is incomplete. You must specify a Table.

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

Posted: Sun Feb 17, 2008 10:53 pm
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'];
}

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

Posted: Sun Feb 17, 2008 11:41 pm
by tinybit
Thanks a lot everyone - problem solved. Arborint's solution example worked right away. :D