Jump to content

Need HELP! PHP code not working on a webserver


oneplusdave

Recommended Posts

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).

 

#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

Link to comment
Share on other sites

I don't believe this worked on any server!

#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";

Does not select all data, it is LIMITED to one row!

And

$result = mysql_query($sql,$connection);

		if($result = mysql_query($sql ,$connection)) {

runs twice $result = mysql_query($sql,$connection); and I suspect this will move the record pointer on so that the one row you have retrieved is not displayed.

Link to comment
Share on other sites

harristweed has pretty much summed it all up.

You said "the script does generate an error", what's the error?

 

#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(mysql_num_rows($result) {
$count =0;

//while loop to cycle through results, not needed if you keep the limit to 1.
//use a while loop instead of the while($count < $num) (with $count being a count of the results) loop you had before.
while ($row = mysql_fetch_array($result)){

$q1 = $row['quote'];

$q2 = $row['author'];

}

} else{

echo "ERROR: ".mysql_error(); 

}

 

That is a more appropriate rendition of your code, however, I don't see what you're trying to do with the data pulled from the database table. In the code comment you say "and displays it in textfields", although you're not displaying any code in textfields? And if you were, it is cycling through all the rows of data and assigning the last row to the $q1 / $q2 variables. If you only want one row, you can just do


$sql = "SELECT * from tblquotes ORDER BY Rand() LIMIT 1";
$query = mysql_query($sql);
//don't have to loop result cause it's limited to one row.
$result = mysql_fetch_array($query);

//results are indexed into $result array. i.e. 
$quote = $result['quote'];
$author = $result['author'];

Link to comment
Share on other sites

The code was supposed to display a random quote each time a page is refreshed, and I forgot to change the comment

it was supposed to be

 

#selects single row from a table, and store it in a variable for display, if failed, display an error 

 

I inserted a echo

"ERROR:".mysql_error();

statement but it does not show up in the code either, it just displays "ERROR", without the error information, maybe because of the configuration of php on that webserver, that does not display errors in php code.

 

I also checked the database connections, it was just right.

 

what seems to be the problem?

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

I suspect that you don't have a connection to the database. Try echoing $connection.

 

that seemed to be the problem, thanks anyway. I had a hard time configuring connection parameters, because the web host lacks sufficient info to their clients.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.