oneplusdave Posted July 29, 2010 Share Posted July 29, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/209193-need-help-php-code-not-working-on-a-webserver/ Share on other sites More sharing options...
harristweed Posted July 29, 2010 Share Posted July 29, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209193-need-help-php-code-not-working-on-a-webserver/#findComment-1092475 Share on other sites More sharing options...
joel24 Posted July 29, 2010 Share Posted July 29, 2010 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']; Quote Link to comment https://forums.phpfreaks.com/topic/209193-need-help-php-code-not-working-on-a-webserver/#findComment-1092476 Share on other sites More sharing options...
oneplusdave Posted July 29, 2010 Author Share Posted July 29, 2010 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] Quote Link to comment https://forums.phpfreaks.com/topic/209193-need-help-php-code-not-working-on-a-webserver/#findComment-1092496 Share on other sites More sharing options...
oneplusdave Posted July 29, 2010 Author Share Posted July 29, 2010 This is the screenshot of what happened. ON the left, it shows the screenshot from the localhost, while on the right is what is seen in the webserver/internet [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/209193-need-help-php-code-not-working-on-a-webserver/#findComment-1092499 Share on other sites More sharing options...
harristweed Posted July 29, 2010 Share Posted July 29, 2010 I suspect that you don't have a connection to the database. Try echoing $connection. Quote Link to comment https://forums.phpfreaks.com/topic/209193-need-help-php-code-not-working-on-a-webserver/#findComment-1092519 Share on other sites More sharing options...
oneplusdave Posted July 29, 2010 Author Share Posted July 29, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/209193-need-help-php-code-not-working-on-a-webserver/#findComment-1092541 Share on other sites More sharing options...
oneplusdave Posted July 29, 2010 Author Share Posted July 29, 2010 THANK GOD! My problem was finally resolved. thank you guys for your help and suggestions. More powers! Quote Link to comment https://forums.phpfreaks.com/topic/209193-need-help-php-code-not-working-on-a-webserver/#findComment-1092772 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.