Page 1 of 1

Code working on command line but not Browser

Posted: Tue Apr 18, 2006 11:04 pm
by pangooan
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi All,

I am newbie here and also new to PHP and MySQL. I have wrote a PHP script to open the db connection and read through one of the table's record.  My script works in command line. I can see the result, but when i am calling it from browser, it just apear blank page. 

I am using Apache2, php5 and mysql 5, installaed on SuSe linux 10.0 . 
here is my script:

Code: Select all

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'pass';
$dbname = 'plaincart';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
		('Error connecting to mysql');
mysql_select_db($dbname);
$result = mysql_query("SELECT user_name FROM tbl_user");
while($r=mysql_fetch_array($result,$conn))
{
echo $r['user_name'];
}
mysql_close($conn);
?>
Any idea why?

Thank you.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Apr 19, 2006 9:29 am
by pickle
That's a good question. Try getting that page to output a full xhtml page (with DOCTYPE, <html><head> & <body> tags). See if that shows up.

Posted: Fri Apr 21, 2006 7:31 am
by pangooan
Hi Pickle,

That is good. thank you. Would you explain a little bit more. after editing my code look like this:

Code: Select all

<html><head>
<title>msh-monkey</title></head>
<body>
<?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'pass';
$dbname = 'plaincart';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
		('Error mohammad connecting to mysql');
mysql_select_db($dbname);

$result = mysql_query("SELECT user_name FROM tbl_user");

while($r=mysql_fetch_array($result,$conn))
{

echo $r['user_name'];

}
mysql_close($conn);
?>
</body></html>
Then result i get is:

Code: Select all

Content-type: text/html
X-Powered-By: PHP/5.0.4
<html><head>
<title>msh-monkey</title></head>
<body>
admin admin1 webmaster
</body>
</html>No log handling enabled - turning on stderr logging
Cannot rename /var/lib/net-snmp/snmpapp.conf to /var/lib/net-snmp/snmpapp.0.conf
Cannot unlink /var/lib/net-snmp/snmpapp.conf
read_config_store open failure on /var/lib/net-snmp/snmpapp.conf
read_config_store open failure on /var/lib/net-snmp/snmpapp.conf
read_config_store open failure on /var/lib/net-snmp/snmpapp.conf
I really don't understand this. Is this a PHP5 bug?

Thank you

Posted: Fri Apr 21, 2006 9:58 am
by Chris Corbyn
It looks normal to me. You've run that on command line so you've seen all the HTTP junk. What does it look like in the source code when viewed in a web browser. Also turn error reporting on.

Posted: Fri Apr 21, 2006 11:56 am
by pangooan
hi d11wtq,

Well if i add some echo just before and after loop like this:

Code: Select all

echo "Name:";
echo $r['user_name'];
echo "          ";
then the output in command line is:

Code: Select all

Name:admin          Name:admin1          Name:webmaster
while in browser it shows:

Code: Select all

Name:Name:Name:
it doesn't read my database record when it called from browser. Might it be due to pear installation?
I ON the error reporting, but no changes for result. It is same as before.

Thank you.

Posted: Fri Apr 21, 2006 12:46 pm
by Chris Corbyn
PHP on the command line doesn't parse the ini file by default. You may need to specify the location of php.ini using the -l option if I remember correctly. Try: `php --help' if not :)

Posted: Fri Apr 21, 2006 4:49 pm
by feyd
It will parse the ini file if it finds it.

Posted: Fri Apr 21, 2006 10:07 pm
by pangooan
I used the following command:
# php -q myfile.php
This will parse the file. But yet I am getting the same error.

Posted: Mon Apr 24, 2006 2:56 am
by tonera

Code: Select all

while($r=mysql_fetch_array($result,$conn))
should be

Code: Select all

while($r=mysql_fetch_array($result))
or

Code: Select all

while($r=mysql_fetch_array($result,MYSQL_ASSOC))
try it.