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

发布于 2024-12-11 11:33:21 字数 3166 浏览 0 评论 0原文

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>

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

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> ".$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>

Ok, now, this code works on my computer which I use to develop this my site, that is it returns the row from the database. However, when I put this code on my site it does not return anything, in fact, the table after the script does not even display at all. I would be grateful if I can get any help on this, it has been giving me sleepless nights for too long now...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停滞 2024-12-18 11:33:21

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

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

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

(我意识到这应该是评论而不是答案,但我没有要点)

I can't see any immediate reason why your script shouldn't run, as halfer pointed out your message and error logs are the best place to look.

As an aside though: The first line in the while loop if($row = Null) will never be true (if it were, the while loop wouldn't be entered)
Consider

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

(I realise this should be a comment not an answer but I don't have the points)

你怎么敢 2024-12-18 11:33:21

试试这个(请参阅代码中的注释以了解更改):

<?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> ".$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>

Try this (see comments in code for changes):

<?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> ".$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>
把昨日还给我 2024-12-18 11:33:21

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

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

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

编辑:*服务器日志的位置取决于您使用的操作系统,并且您没有指定这一点。然而,这很容易从搜索引擎获得 - 如果您使用的是 Windows,则可以在 Web 上搜索“windows apache 日志位置”。简单;-)

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

I presume from the $_GET usage that you're running this in a browser, rather than from a console. It might be that your mysql extension is not loaded, or you don't have a valid connection to the database server.

  • Firstly, check your error logs. These are usually kept in the same place as your Apache access logs*. This will give you an error message to work with.
  • Next, run <?php phpinfo(); ?> in an otherwise empty page on your server, and see if mysql support is supported by your web-based PHP.
  • Try running mysql_error() after db operations that might fail. This is also a good way to dig for errors.

Lastly bear in mind that your script has SQL injection security holes in it at present. Fix those probs before you go live with this :-).

Edit: * the location of your server logs depends on what OS you are using, and you have not specified this. This is quite easy to get from a search engine, however - if you are on Windows, then do a web search for "windows apache logs location". Easy ;-)

Edit 2: if SSH has been disabled, and you feel you need it, talk to your host. That is however a separate problem, and you don't need it to tackle the issue at hand. If your phpMyAdmin has problems, re-install it, or use the one provided by your host. (If you are using cPanel, this is included in your control panel.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文