开发者

Unexplained PHP/MySQL error. Code works on my test server, but not the permanent server

开发者 https://www.devze.com 2023-03-07 11:20 出处:网络
http://zeel.dyndns.org/ss/home/ This is the homepage for my site on my test server, it works great. So do all the other pages. However on my permanent remote server (justhost)

http://zeel.dyndns.org/ss/home/

This is the homepage for my site on my test server, it works great. So do all the other pages. However on my permanent remote server (justhost)

http://cvca.ohiofbla.org/store/home/

I get:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in

/home/ohiofbl1/public_html/schools/cvca/store/home/index.php on line 43

I don't see any problems in the code, and the same code works fine on the testin开发者_开发技巧g server.

The only difference is that I have an included file that defines variables for SQL user/pass/dbname that are different depending on the server. But the only file that is different is that include file.

Am I missing something?

<?php session_start(); require("../includes/config.php");?>
<?php $title = "Home"; ////////////////////////////////////////////?>
<?php require("../includes/head.php");
///////////////////////////////////////////////////////////////////?>
<?php
$con = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}?>

<div id="index_structure">
<div id="index_left">
<div id="index_top">
<h2>Welcome to the CVCA School Store! </h2>
<p>Here you can purchase items from the school store, and then pick them 
   up from school the next day. </p>
</div>

<div id="index_bottom">
<p align="center"><img src="../images/School Store 014.png" width="30%" />&nbsp;
<img src="../images/School Store 015.png" width="30%" />&nbsp;
<img src="../images/School Store 016.png" width="30%" /></p><br />
<p><strong>Hours:</strong> Mon - Fri, 7:45 - 8:05 AM. On school days. </p>
<p><strong>Location:</strong> Corner of west cafe </p>
<p>Backpack sales start in May - August for all new 7th graders. 
   Supplies are available year round.</p>
<p>Empty shoeboxes are accepted for Operation Shoebox at all times. </p>
<p>We are always looking for parent helpers and student employees. </p>
<p>School uniforms are available for fitting purposes. 
   Uniforms are available for purchase at 
<span class="bluelink"><a href="http://cvcastore.com/">cvcastore.com</a>
</span></p>
<p><img src="../images/shirts.png" width="32%" /></p></div></div>
<div id="index_sidebar">
<h3 align="left">Featured items:</h3><hr />

<?php
function random_one($dbname, $con) 
{ 
  mysql_select_db($dbname, $con);
  unset($result);
  unset($col);
  $i = 1;
  while($i = 1) 
  { 
    $result = mysql_query("SELECT * FROM items\n"."ORDER BY RAND()\n"."LIMIT 1");
    $col = mysql_fetch_array($result); //This is line 43 from the error
    if (file_exists("../" . $col['ImagePath']) == true)
    {
      echo "<h4><a href='../item/?item=" . $col['ItemNumber'] . "'>" .
           $col['ItemName'] . "</h4><br />
           <img src='../" . $col['ImagePath'] . "' border='0' /></a>";
      break;
    }
  }
  ?><hr /><?php
}  
random_one($dbname, $con);
random_one($dbname, $con);
random_one($dbname, $con);

?></div></div>    

<?php require("../includes/foot.php"); ///////////////////////////////?> 


Well nothing is wrong with the query string I checked it myself (although I agree with rineez, there is no reason for those \n). The problem might be either with the variable $dbname, or more likely the while loop itself. It is an infinte loop that will not exit, since you haven't done anything to the $i variable for it to change thus exiting the while loop.


I found the problem myself.

"SELECT * FROM items\n"."ORDER BY RAND()\n"."LIMIT 1"

The table "items" is actually "Items", but my Windows-based test server ignored that difference. The permanent server is Linux-based however, and it saw "i" and got lost. I changed the names and everything works fine.


You should always check if any rows are returned prior to iterating.

Run the query against your production DB directly, via terminal, or GUI (like PHPMyAdmin, SQLYog, etc.) and see what record(s), if any, are returned.


Try echoing the variable $dbname and verify it has correct value. I think just host will add your cpanel username as prefix when you create a database name...

...and I wonder why you have to use \n and concatenations to form your query string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号