Jump to content


Photo

Stupid PHP Problem


  • Please log in to reply
5 replies to this topic

#1 Inferno_str1ke

Inferno_str1ke
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationManchester, UK

Posted 26 September 2006 - 04:44 PM

Ok, simple thing, I have the following lines:
$query="SELECT * FROM ".$L_sqlprefix."config";
$result=mysql_query($query) OR die($query.'<br/>'.mysql_error());
while ($line=mysql_fetch_array($result,MYSQL_ASSOC))
{
	$_[ $line['title'] ] = $line['value'];
}
What this does is take a title and value out of the database and stick it into an array. When printed ( print_r($_); ) the array should look like this:
Array ( 
[blogtitle] => 1.k.e.Net 
[thisurl] => http://127.0.0.1:85/ 
[feedurl] => rss.php 
[feedtype] => RSS 2.0 
[install] => complete )
When executing on my test server the code performs fine, but when I run it on my web server the array comes out as follows:
Array ( 
[blogtitle] => 1 
[thisurl] => h
[feedurl] => r
[feedtype] => R
[install] => c )
Which is obviously useless to me. Now I'm stumped as to why it's behaving differently in different locations, and also stumped as to another way to get the info into an array.

I've tried a a few different ways on the web server, all produce random and useless results:
array ($_);
$query="SELECT * FROM config";
$result=mysql_query($query) OR die($query.'<br/>'.mysql_error());
while ($line=mysql_fetch_array($result,MYSQL_ASSOC))
{
	$_[$line['title']]=$line['value'];
	echo $line['title'].'<br/>';
	echo $line['value'].'<br/>';
	print_r($_);
	echo '<br/><br/>';
}
Result:
blogtitle
1keNet
1usr/local/apache/bin/httpd

thisurl
http://127.0.0.1:85/
husr/local/apache/bin/httpd

feedtype
RSS 2.0
Rusr/local/apache/bin/httpd

perpage
10
1usr/local/apache/bin/httpd
There are more but I've cut it short and you get the point. Incidentally using:
$array=mysql_fetch_array($result,MYSQL_ASSOC);
foreach ($array as $subarray)
{
	echo $subarray.'<br/><br/>';
}
instead of the While loop gives me the result (not cut short):
1

blogtitle

1keNet

0

Things get hot when there's an Inferno str1ke
1.k.e.Net - My blog and other animals.


#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 26 September 2006 - 05:05 PM

Try

$_ = array(); rather than array($_)

also you could try using mysql_fetch_assoc rather than mysql_fetch_array. I know they sort of mean the same thing but I have had different results using different functions.

Ray

#3 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 26 September 2006 - 05:07 PM

change:
$_[ $line['title'] ] = $line['value'];
to
$_[ $line ]['title'] = $line['value'];

...if you're trying to make "title" in the 1st dimension of the $_[$line] array.


#4 Inferno_str1ke

Inferno_str1ke
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationManchester, UK

Posted 26 September 2006 - 07:35 PM

No, title does not need to be an attribute of the array - each array item needs to be $_[' *option name here* '].

I shall try defining the array differently.

Things get hot when there's an Inferno str1ke
1.k.e.Net - My blog and other animals.


#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 26 September 2006 - 07:40 PM

try
<?php

$query="SELECT * FROM ".$L_sqlprefix."config";
$result=mysql_query($query) OR die($query.'<br/>'.mysql_error());
$data = array();
while ($line=mysql_fetch_array($result,MYSQL_ASSOC))
{
	$data[] = $line;
}

// view results
echo '<pre>', print_r($data, true), '</pre>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 Inferno_str1ke

Inferno_str1ke
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationManchester, UK

Posted 26 September 2006 - 08:53 PM

I have found, thanks to that, that the error was that it just didn't like $_ as a name of an array. I changed it to $l and my old code worked too. Thanks for helping me see that.

Things get hot when there's an Inferno str1ke
1.k.e.Net - My blog and other animals.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users