Page 1 of 1

Need HELP! PHP code not working on a webserver

Posted: Thu Jul 29, 2010 12:48 am
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

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

Posted: Thu Jul 29, 2010 1:46 am
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?

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

Posted: Thu Jul 29, 2010 3:21 am
by oneplusdave
@cpetercarter, i had checked the parameters (hostname,username,password) but it was still useless.

was there an error in my code?

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

Posted: Thu Jul 29, 2010 4:31 am
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;

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

Posted: Thu Jul 29, 2010 7:46 am
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?

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

Posted: Thu Jul 29, 2010 1:27 pm
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.

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

Posted: Thu Jul 29, 2010 11:43 pm
by manojsemwal1
Check your code $num = mysql_numrows($result);
replace this line $num = mysql_num_rows($result);
thanks