【问题标题】:Change code and fix it?更改代码并修复它?
【发布时间】:2017-07-24 11:18:45
【问题描述】:

我知道我正在使用多种 PHP 样式,但我不知道如何设置。

我认为exit() 有问题;我不知道该怎么做才能得到想要的结果。

<?php
    $mysqli = new mysqli("localhost", "root", "", "search");
       if(isset($_GET['search_button']))
            {
                $search = $_GET['search'];
                    if(search=="")
             {
                echo "<center> <b> Please write something in Search Box </b> </center>"

                exit();
             }

                $sql = "select * from website where site_key like '%$search%' limit 0, 5";

                $rs =  mysql_query($sql);

                 if(mysql_num_rows($rs)<1)
                {
                 echo "<center> <h4> <b> Oops !!! Sorry, there is no result found to related the word. </b> </h4> </center>";
                 exit();
                 }

                echo "<font size='+1' color='#1a1aff'> images for $search</font>";

                 while($row = mysql_fetch_array($rs))
                 {
                     echo   "<td>
                     <table style='margin-top: 10px'>
                        <tr>
                            <td>
                                <img src='img/$row[5]' height='100px'>
                            </td>
                         </tr>
                     </table>
                     </td>"
            }
            }
?>

【问题讨论】:

  • 你好。您将mysqlimysql 混为一谈——这是行不通的。请停止使用 mysql_* 驱动程序,它已过时且不再受 PHP7 支持。仅使用mysqli_*,然后您的代码将起作用。只需调整您的查询,哦,顺便说一句,看看bobby-tables.com 并了解一些有关 SQL 注入的知识。使用此代码,您的数据库可以在几秒钟内被黑客入侵,而无需深入了解数据库.. 只需 SQL 注入就可以完美地处理这个问题。
  • echo "&lt;center&gt; &lt;b&gt; Please write something in Search Box &lt;/b&gt; &lt;/center&gt;" 最后错过了;
  • 洛科怎么说,你的回声线有错误..

标签: php


【解决方案1】:

mysql_query($sql) 应该 mysqli_query($mysqli,$sql)

(mysqli_query()需要连接参数)

mysql_num_rows($rs) 应该是mysqli_num_rows($rs)

mysql_fetch_array($rs) 应该是mysqli_fetch_array($rs)

【讨论】:

  • 我认为这不足以成为答案。
  • Loko,你认为这不足以成为一个答案......给我们正确的答案
【解决方案2】:

您通过一个数据库连接混合mysqlmysqli 函数,这是错误的。您可以混合使用 mysql 和 mysqli,但需要有不同的数据库连接。阅读Getting a PHP PDO connection from a mysql_connect()?

因为,只有一个通过 mysqli 连接的数据库连接。因此,在整个应用程序中,您必须使用 mysqli db 连接变量来使用 mysqli 函数。

更新代码

<?php
$mysqli = new mysqli("localhost", "root", "", "search");
if (isset($_GET['search_button'])) {
  $search = $_GET['search'];
  if ($search == "") {
    echo "<center> <b> Please write something in Search Box </b> </center>";
    exit();
  }

  $sql = "select * from website where site_key like '%$search%' limit 0, 5";
  $rs = mysqli_query($mysqli, $sql);
  if (mysqli_num_rows($rs) < 1) {
    echo "<center> <h4> <b> Oops !!! Sorry, there is no result found to related the word. </b> </h4> </center>";
    exit();
  }

  echo "<font size='+1' color='#1a1aff'> images for $search</font>";

  while ($row = mysqli_fetch_array($rs)) {
    echo "<td>
            <table style='margin-top: 10px'>
              <tr>
                 <td>
                    <img src='img/$row[5]' height='100px'>
                 </td>
               </tr>
            </table>
          </td>";
  }
}

【讨论】:

  • 你为什么回答却仍然提供错误的代码。什么是if (search == "") {
  • 等一下。不要问我“.. 你为什么回答..” 好吧?你不是要问我的人。那是错字。如果你看到那个错字,你也可以编辑它。这不像回答的人有权纠正错误或错字。知道了? @Loko
  • @NanaPartykar 从字面上看,这就是它一开始就变坏的原因。几个错别字。你甚至试图回答这个问题对我来说毫无意义。只是为了容易获得声誉。
猜你喜欢
  • 2014-08-26
  • 2022-01-16
  • 1970-01-01
  • 2022-10-04
  • 1970-01-01
  • 2016-01-11
  • 2023-04-07
  • 2020-11-09
  • 1970-01-01
相关资源
最近更新 更多