Need HELP! PHP code not working on a webserver

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
oneplusdave
Forum Newbie
Posts: 5
Joined: Thu Jul 29, 2010 12:31 am

Need HELP! PHP code not working on a webserver

Post by oneplusdave »

This snippet was running perfectly on localhost, but after I uploaded the file containing this snippet on a webserver, the script does generate an error, or even displaying none (no results).

Code: Select all

		#selects all data from table and displays it in textfields. If failed, it will display the error
			$sql = "SELECT * from tblquotes ORDER BY Rand() LIMIT 1";
			$result = mysql_query($sql,$connection);
			
			if($result = mysql_query($sql ,$connection)) {
			$num = mysql_numrows($result);
			$count =0;
			
			while ($count < $num){
			$q1 = mysql_result($result,$count,"quote");
			$q2 = mysql_result($result,$count,"author");
			$count++;
			}
			} else{
			echo "ERROR: ".mysql_error(); 
			}

what seems to be the problem? Need help... :(

I'm running on PHP 4.4.4 on Localhost and PHP 5.2.9 on the webserver.
Mysql 4/5 on Localhost and Mysql 4.1.22 on the webserver
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Need HELP! PHP code not working on a webserver

Post by cpetercarter »

The problem is probably that you are unable to connect to the database on the server. Have you checked username, password, host name etc?
oneplusdave
Forum Newbie
Posts: 5
Joined: Thu Jul 29, 2010 12:31 am

Re: Need HELP! PHP code not working on a webserver

Post by oneplusdave »

@cpetercarter, i had checked the parameters (hostname,username,password) but it was still useless.

was there an error in my code?
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Need HELP! PHP code not working on a webserver

Post by Grizzzzzzzzzz »

oneplusdave wrote:@cpetercarter, i had checked the parameters (hostname,username,password) but it was still useless.

was there an error in my code?

have you actually tried echo'ing q1&q2??

Code: Select all

#selects all data from table and displays it in textfields. If failed, it will display the error
                        $sql = "SELECT * from tblquotes ORDER BY Rand() LIMIT 1";
                        $result = mysql_query($sql,$connection);
                       
                        if($result = mysql_query($sql ,$connection)) {
                        $num = mysql_numrows($result);
                        $count =0;
                       
                        while ($count < $num){
                        $q1 = mysql_result($result,$count,"quote");
                        $q2 = mysql_result($result,$count,"author");
                        $count++;
                        }
                        } else{
                        echo "ERROR: ".mysql_error();
                        }

echo $q1."\n";
echo $q2;
oneplusdave
Forum Newbie
Posts: 5
Joined: Thu Jul 29, 2010 12:31 am

Re: Need HELP! PHP code not working on a webserver

Post by oneplusdave »

Grizzzzzzzzzz wrote:
oneplusdave wrote:@cpetercarter, i had checked the parameters (hostname,username,password) but it was still useless.

was there an error in my code?

have you actually tried echo'ing q1&q2??

Code: Select all

#selects all data from table and displays it in textfields. If failed, it will display the error
                        $sql = "SELECT * from tblquotes ORDER BY Rand() LIMIT 1";
                        $result = mysql_query($sql,$connection);
                       
                        if($result = mysql_query($sql ,$connection)) {
                        $num = mysql_numrows($result);
                        $count =0;
                       
                        while ($count < $num){
                        $q1 = mysql_result($result,$count,"quote");
                        $q2 = mysql_result($result,$count,"author");
                        $count++;
                        }
                        } else{
                        echo "ERROR: ".mysql_error();
                        }

echo $q1."\n";
echo $q2;

oh, I just forgot to include the suceeding code, but I did include a way of displaying those variables.

The code was really fine, except for minor adjustments but now It seemed to be I can't establish a connection with the database.

is the hostname = localhost, for a web hosted account?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Need HELP! PHP code not working on a webserver

Post by Jade »

It depends on whether or not your SQL server is on the same machine as your web server or if they've done a hostname change. You need to contact your web host to find out.
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: Need HELP! PHP code not working on a webserver

Post by manojsemwal1 »

Check your code $num = mysql_numrows($result);
replace this line $num = mysql_num_rows($result);
thanks
Post Reply