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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

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

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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) ?
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;
}
Post Reply