PHP while循环没有在服务器上返回任何内容

时间:2011-10-22 12:25:11

标签: php while-loop

if ($area == Null || $area == "Area of City") {
  $ment = "CREATE OR REPLACE VIEW water 
           AS SELECT postable.postid, description, dated, image, posterid, country, state, city, area, cat
             FROM postable 
           INNER JOIN ".$_GET['Cat']."
             ON postable.postid = ".$_GET['Cat'].".postid
           WHERE ".$_GET["Cat"].".cat = '".$_GET["what"]."'
             AND postable.country = '".$_GET["Country"]."'
             AND postable.state = '".$_GET["State"]."'
             AND postable.city = '".$_GET["City"]."'";
}

$ment = "CREATE OR REPLACE VIEW water
         AS SELECT postable.postid, description, dated, image, posterid, country, state, city, area, cat
           FROM postable, ".$_GET["Cat"]."
         WHERE (
           (postable.postid = ".$_GET["Cat"].".postid
           AND ".$_GET["Cat"].".cat = '".$_GET["what"]."'
           AND postable.country = '".$_GET["Country"]."'
           AND postable.state = '".$_GET["State"]."'
           AND postable.city = '".$_GET["City"]."'
           AND postable.area = '".$area."')
             OR
           (postable.postid = ".$_GET["Cat"].".postid
           AND ".$_GET["Cat"].".cat = '".$_GET["what"]."'
           AND postable.country = '".$_GET["Country"]."'
           AND postable.state = '".$_GET["State"]."'
           AND postable.city = '".$_GET["City"]."')
         )";
$tester = "SELECT * FROM water, userguy
           WHERE userguy.posterid = water.posterid";

if (!mysql_query($ment, $con)) {
  die('There are no posts matching your search, please enter another search in either another location or category, or both. ' );
}

if (!mysql_query($tester,$con)) {
  die('There are no posts matching your search, please enter another search in either another location or category, or both.');
}

$result = mysql_query($tester,$con);


while ($row = mysql_fetch_array($result)) {
if ($row == Null) {
  echo "<tr><td colspan'4'>There are no posts matching your search, please enter another search in either another location or category, or both.</td></tr>";}
  echo "<tr><td colspan='4' class='sult'>";
  echo "<div id='main'>".$row['description']."</div> ".$row['dated']." <a     href='mai.php?tofield=".$row['email']."'><b>".$row['email']."</b></a><b>&nbsp".$row['mobile']."</b>";
  if ($row['image'] != Null) {
    echo "<img src='upload/".$row['image']."' class='relt'/>";
  }
  echo "</td></tr>";
}

?>

<tr>
  <td colspan='4'>
     <br/><br/><br/><br/>
  </td>
</tr>
<tr>
  <td colspan='2' align='center'>
    <img src='first.jpg'/>
  </td>
  <td colspan='2' align='center'>
    <img src='second.jpg'/>
  </td>
</tr>
<tr>
  <td colspan='2' align='center'>
    <img src='high.jpg'/>
  </td>
  <td colspan='2'></td>
</tr>
</table>

</body>

好的,现在,这个代码可以在我的计算机上运行,​​我用它来开发我的站点,就是它返回数据库中的行。但是,当我将此代码放在我的网站上时,它不会返回任何内容,事实上,脚本后的表甚至根本不显示。如果我能得到任何帮助,我将不胜感激,它已经给了我不眠之夜... ...

3 个答案:

答案 0 :(得分:1)

我看不出你的脚本不应该运行的任何直接原因,因为halfer指出你的消息和错误日志是最好看的地方。

顺便说一下:while循环if($row = Null)中的第一行永远不会为真(如果是,则不会输入while循环) 考虑

$n_rows = 0;
while($row = mysql_fetch_array($result)) {
    $n_rows++;
    // Process row
}
if(!$n_rows) {
    // No rows message
}

(我知道这应该是一个评论而不是一个答案,但我没有意见)

答案 1 :(得分:1)

尝试此操作(请参阅代码中的注释以获取更改):

<?php

  if ($area == Null || $area == "Area of City") {
    $ment = "CREATE OR REPLACE VIEW water 
             AS SELECT postable.postid, description, dated, image, posterid, country, state, city, area, cat
               FROM postable 
             INNER JOIN ".$_GET['Cat']."
               ON postable.postid = ".$_GET['Cat'].".postid
             WHERE ".$_GET["Cat"].".cat = '".$_GET["what"]."'
               AND postable.country = '".$_GET["Country"]."'
               AND postable.state = '".$_GET["State"]."'
               AND postable.city = '".$_GET["City"]."'";
  } else {
    // Pretty sure this should be in an else block
    // As it was, the query above would never be executed, because it would
    // always be overwritten by this one
    $ment = "CREATE OR REPLACE VIEW water
             AS SELECT postable.postid, description, dated, image, posterid, country, state, city, area, cat
               FROM postable, ".$_GET["Cat"]."
             WHERE (
               (postable.postid = ".$_GET["Cat"].".postid
               AND ".$_GET["Cat"].".cat = '".$_GET["what"]."'
               AND postable.country = '".$_GET["Country"]."'
               AND postable.state = '".$_GET["State"]."'
               AND postable.city = '".$_GET["City"]."'
               AND postable.area = '".$area."')
                 OR
               (postable.postid = ".$_GET["Cat"].".postid
               AND ".$_GET["Cat"].".cat = '".$_GET["what"]."'
               AND postable.country = '".$_GET["Country"]."'
               AND postable.state = '".$_GET["State"]."'
               AND postable.city = '".$_GET["City"]."')
             )";
  }

  $tester = "SELECT * FROM water, userguy
             WHERE userguy.posterid = water.posterid";

  /*
    Do you really not want the result of this query?
    Shouldn't you be catching the results in a variable?
    If not, consider adding a LIMIT 1 clause to the query, as the
    database will have to do more work to return a full result set
    you never use...
    Regardless of that, testing for !mysql_query doesn't tell you there
    were no results, it tells you whether the query itself failed. You
    test the number of results using mysql_num_rows() or a SELECT count() query
  */
  if (mysql_num_rows(mysql_query($ment, $con)) < 1) {
    die('There are no posts matching your search, please enter another search in either another location or category, or both. ' );
  }

  // Same goes for this.
  // However, since you definitely do want the results of this, why run it twice?
  $result = mysql_query($tester,$con);
  if (mysql_num_rows($result) < 1) {
    die('There are no posts matching your search, please enter another search in either another location or category, or both.');
  }

  // Use mysql_fetch_assoc() instead of mysql_fetch_array(), it's more efficient
  while ($row = mysql_fetch_assoc($result)) {
    // if ($row == Null) {
    // Sorry, what? If the row is null??
    // It will never be null... if it were, the loop would break immediately
    // and never reach this point, plus mysql_fetch_* never returns null!
    //   echo "<tr><td colspan'4'>There are no posts matching your search, please enter another search in either another location or category, or both.</td></tr>";
    // }
    echo "<tr><td colspan='4' class='sult'>";
    echo "<div id='main'>".$row['description']."</div> ".$row['dated']." <a     href='mai.php?tofield=".$row['email']."'><b>".$row['email']."</b></a><b>&nbsp".$row['mobile']."</b>";
    if ($row['image'] != Null) {
      echo "<img src='upload/".$row['image']."' class='relt'/>";
    }
    echo "</td></tr>";
  }

?>

<tr>
  <td colspan='4'>
     <!--
       Really? You can't just use a CSS height?
     -->
     <br/><br/><br/><br/>
  </td>
</tr>
<tr>
  <td colspan='2' align='center'>
    <img src='first.jpg'/>
  </td>
  <td colspan='2' align='center'>
    <img src='second.jpg'/>
  </td>
</tr>
<tr>
  <td colspan='2' align='center'>
    <img src='high.jpg'/>
  </td>
  <td colspan='2'></td>
</tr>
</table>

</body>

答案 2 :(得分:0)

我从$_GET用法假设你在浏览器中运行它,而不是从控制台运行。可能是您的mysql扩展未加载,或者您没有与数据库服务器的有效连接。

  • 首先,检查错误日志。这些通常与Apache访问日志*保持在同一位置。这将为您提供一条错误消息。
  • 接下来,在服务器上的空白页面中运行<?php phpinfo(); ?>,看看基于Web的PHP是否支持mysql支持。
  • 尝试在可能失败的数据库操作之后运行mysql_error()。这也是挖掘错误的好方法。

最后请记住,您的脚本目前还有SQL注入安全漏洞。在你开始使用之前修复这些问题: - )。

编辑:*服务器日志的位置取决于您使用的操作系统,并且您没有指定。这很容易从搜索引擎获得,但是 - 如果你在Windows上,那么在网上搜索“windows apache logs location”。容易; - )

编辑2:如果已禁用SSH,并且您认为需要它,请与您的主机联系。然而,这是一个单独的问题,你不需要它来解决手头的问题。如果您的phpMyAdmin有问题,请重新安装或使用主机提供的问题。 (如果您使用的是cPanel,则它将包含在您的控制面板中。)