Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Rower_CPU

Moderator emeritus
Original poster
I'm working on new projects with a more strict php.ini and running into some weird behavior.

For instance, I'm pulling info from a db and trying to run good old nl2br to display new lines as <br> in the HTML. But, if I try to access the array value by name and put it into a new variable I get errors - it works fine if I echo it out though.

If I access the variable by it's position, it works fine.
PHP:
$variable = $row[variable]; // Doesn't work
$variable = $row[1];  // Works

Thoughts?
 
sounds like your array isn't being created with associative keys. what's the line you're using to create the array?

try one of the following:
PHP:
$array = mysql_fetch_array($result, MYSQL_ASSOC);
or
PHP:
$array = mysql_fetch_assoc($result);

see if forcing it to use associative indices has any effect. (both lines do the same thing.)

edit: sorry, should have looked closer. i take it, since you're using an array variable called $row, that you're using mysql_fetch_row()? if that's the case, then this is definitely the culprit. mysql_fetch_row() only returns a numeric array. use one of the lines above if you need associative indices. or keep it as is, if calling an array variable by offset is ok with you.
 
I can access the variables by name when I echo them out, though (should've made that clearer).

Would the associative array make a difference between ability to echo and manipulate the strings?
 
Rower_CPU said:
I can access the variables by name when I echo them out, though (should've made that clearer).

Would the associative array make a difference between ability to echo and manipulate the strings?

can you give me an example of the code you're using to echo out your variables?
 
Sure thing...
PHP:
while ($row = mysql_fetch_array($data)) {
      if ($alternate == "1") {
	     $class = "yellow";
			$alternate = "2";
	} else {
		$class = "orange";
		$alternate = "1";
	}
	echo "<tr class=\"$class\">\n";
	$machine = nl2br($row[2]);
	$comment = nl2br($row[3]);
	echo "<td><a href=\"http://$row[IP]\" target=\"blank\">$row[IP]</td>
		<td>$machine</td>
		<td>$comment</td>
		<td><a href=\"ip.php?ip=$row[IP]\">edit</a></td>\n";
}
You can see I'm echoing $row[IP] just fine, but for the nl2br, I have to go with position numbers.

I'm sure there are other optimizations that can be made, too...feel free to point them out.
 
kingjr3 said:
Try

$row["IP"] instead

Well, $row[IP] works fine for echoing...

Are you saying I need the quotes for tossing the value into another variable???
 
mrjamin said:
what errors are you actually getting? can you put up a phpinfo file for us to look at?

Actually, it's only a notice:
Notice: Use of undefined constant machine - assumed 'machine'

The notice is if I try something like:
PHP:
$machine = $row[machine];

phpinfo()

kingjr3-
The quotes thing seems to work. Makes no sense to me, though...
 
Rower_CPU said:
Actually, it's only a notice:


The notice is if I try something like:
PHP:
$machine = $row[machine];

phpinfo()

kingjr3-
The quotes thing seems to work. Makes no sense to me, though...

thats all down to the way PHP had it error handling set! thats a very poorly set up server if you ask me...

put an .htaccess file in the root folder of your site, containing the following:
Code:
<IfModule mod_php4.c>
    php_value error_reporting 247
</IfModule>

We've just got to hope that the apache config of the server is set to allow htaccess files to override stuff!

So yeh, that'll get rid of the notices. Its not great programming practise to call stuff by $array[key_name] (hence php kicking up a warning), i always use $array['key_name'] where possible.

Displaying notices can expose all kinds of vulnerabilities in your code for even a novice hacker to exploit, so best turn them off anyway

edit: removed register_globals stuff, already turned off 😀
 
mrjamin said:
thats all down to the way PHP had it error handling set! thats a very poorly set up server if you ask me...

put an .htaccess file in the root folder of your site, containing the following:
Code:
<IfModule mod_php4.c>
    php_value error_reporting 247
    # php_flag register_globals off
</IfModule>

If you're comfortable working with register_globals off, uncomment the second line.

We've just got to hope that the apache config of the server is set to allow htaccess files to override stuff!

So yeh, that'll get rid of the notices. Its not great programming practise to call stuff by $array[key_name] (hence php kicking up a warning), i always use $array['key_name'] where possible.

Displaying notices can expose all kinds of vulnerabilities in your code for even a novice hacker to exploit, so best turn them off anyway

Well, it's my personal box at work...😉

Errors were completely off, but for troubleshooting purposes I turned them on. I can edit php.ini to turn them back off.

So can you explain why $array[key_name] is fine in echo statements but not elsewhere? That's what I'm stumped on.
 
Rower_CPU said:
So can you explain why $array[key_name] is fine in echo statements but not elsewhere? That's what I'm stumped on.

No, I can't! It is an odd one and got me stumped too 🙄
 
I can't explain why the echo works, but I do know that an associative array (aka hashtable) uses strings as the key. When you use $row[ID], PHP is looking for a constant called ID, which doesn't exist. $row["ID"] is asking for the element in the array with the key="ID" (the string). ALWAYS use quotes when addressing elements in a hashtable.

Glad it worked.
 
Rower_CPU said:
I'm sure there are other optimizations that can be made, too...feel free to point them out.

Rower,
are you familiar with the ternary operator: (condition ? if true : if false) here's an example of how i use it to set alternating row styles:
PHP:
$result = mysql_query($query) or die(mysql_error());
$i = 1;
echo '<table>';
while($array = mysql_fetch_assoc($result))
{
	$style = ($i & 1 ? 'odd' : 'even');
	echo '<tr class="'.$style.'"><td>'.$array["key"].'</td></tr>';
	$i++;
}
echo '</table>';
the important part is:
PHP:
$i = 1;
$style = ($i & 1 ? 'odd' : 'even');
$i++;
which iterates a simple counter variable and checks at each pass if it's odd or even by comparing it bitwise to 1. you can get the same effect using the modulo operator:
PHP:
$style = ($i % 2 ? 'odd' : 'even');
but i like to use the bitwise function, because binary is so punk rock.

the bitwise comparator (the & above) takes two variables and compares their binary equivalents, beginning with the least significant digit (the 1's place.) so whatever $i is, it turns it into binary and compares it to, in this case, the numeral 1. and since there's only one digit to be compared to, it never looks past the right-most digit -- and that's the digit that tells us if a binary number is odd or even.
 
That's a new one for me. Thanks for pointing that out.

That's a much nicer way to switch the class style than my 1,2 method. 🙂
 
well, you heard it here first... i can't wait for MR to get blogged for being the birthplace of the phrase.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.