Code working on command line but not Browser

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
pangooan
Forum Newbie
Posts: 4
Joined: Tue Apr 18, 2006 10:56 pm

Code working on command line but not Browser

Post 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]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
pangooan
Forum Newbie
Posts: 4
Joined: Tue Apr 18, 2006 10:56 pm

Post 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
Last edited by pangooan on Fri Apr 21, 2006 11:49 am, edited 2 times in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
pangooan
Forum Newbie
Posts: 4
Joined: Tue Apr 18, 2006 10:56 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It will parse the ini file if it finds it.
pangooan
Forum Newbie
Posts: 4
Joined: Tue Apr 18, 2006 10:56 pm

Post by pangooan »

I used the following command:
# php -q myfile.php
This will parse the file. But yet I am getting the same error.
tonera
Forum Newbie
Posts: 5
Joined: Mon Apr 10, 2006 10:40 pm

Post 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.
Post Reply