Page 1 of 1

Stumped: MySQL only returning 1/2 results with *?

Posted: Sat Nov 25, 2006 12:58 pm
by LiveFree
Hello All,

Well I am quite stumped on this function which does not seem to want to work. Im trying to pull the contents into an array
MySQL:
------------------------
`config_name`
`config_value`

PHP:
-------------------------
(array) $config['config_name'] = $config_value;

The problem is, using this function

Code: Select all

function loadConfig() {
	$sql = mysql_query("SELECT * FROM `config`");
	$config = array();
		while ($row = mysql_fetch_array($sql)) {
		$config[$row['config_name']] = $row['config_value'];
		}
	mysql_free_result($sql);
	return $config;
}
And a print_r, I get this: multipart/x-zip;multipart/x-gzip [max_file_size] => 2MB [paypal_addr] => )
Which shows its not complete (as in the first value does not have a key and it doesnt have an opening parentheses

Im stumped, anyone have a clue?

Thanks

Posted: Sat Nov 25, 2006 1:03 pm
by volka
try

Code: Select all

function loadConfig() {
	$sql = mysql_query("SELECT * FROM `config`");
	$config = array();
	while ($row = mysql_fetch_array($sql)) {
		echo 'Debug: ', htmlentities($row['config_name']) , '=>', htmlentities($row['config_value']), "<br />\n";
		$config[$row['config_name']] = $row['config_value'];
	}
	mysql_free_result($sql);
	return $config;
}
What does it print (please copy&paste) ?

Posted: Sat Nov 25, 2006 1:07 pm
by LiveFree
Debug: file_types=>multipart/x-zip;multipart/x-gzip
Debug: max_file_size=>2MB
Debug: paypal_addr=>

Thanks for the help volka!

Edit: So that means I need to use htmlentities() when assigning to the array? Why is it correctly outputting, but not assigning?

Edit 2: THats the only way it works when I assign it to $config, when I use htmlentities on the DB input

Posted: Sat Nov 25, 2006 1:13 pm
by volka
htmlentitites() converts characters like > to >
I thought you might display the results in a webbrowser and it contains some characters that need encoding to be displayed correctly. Doesn't look that way.
Edit: So that means I need to use htmlentities() when assigning to the array?
No.
Why is it correctly outputting, but not assigning?
I really don't know.
Next step.

Code: Select all

function loadConfig() {
	$sql = mysql_query("SELECT * FROM `config`");
	$config = array();
	while ($row = mysql_fetch_array($sql)) {
		$config[$row['config_name']] = $row['config_value'];
	}
	mysql_free_result($sql);
	
	echo '<pre>'; var_export($config); echo "</pre>\n";
	return $config;
}