更改代码并修复它?

I know that I'm using multiple PHP styles, but I don't know how can I set this.

I think that there something wrong in exit(); and I don't know what to do to get the desired result.

<?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>"
            }
            }
?>
php
drbe16008
drbe16008 怎么说Loko,你有一个错误的回声..
大约 3 年之前 回复
dqkf49487
dqkf49487 echo“<center><b>请在搜索框中写一些东西</b></center>”在末尾。
大约 3 年之前 回复
dqydp44800
dqydp44800 你好。你混淆了mysqli和mysql-这是行不通的。请停止使用mysql_*驱动程序,它在PHP7中已过时且不再受支持。仅使用mysqli_*,然后您的代码将起作用。简单地调整你的查询,哦,顺便看看bobby-tables.com并了解一些关于SQL注入的知识。使用此代码,您的数据库可以在几秒钟内被黑客攻击,而无需深入了解数据库。简单地说,SQL注入可以完美地在这个上运行。
大约 3 年之前 回复

2个回答



mysql_query($ sql)</ code>
应该是
mysqli_query($ mysqli,$ sql) </ code> </ p>

(mysqli_query()所需的连接参数)</ h2>

mysql_num_rows($ rs)</ code>应< code> mysqli_num_rows($ rs)</ code> </ p>

mysql_fetch_array($ rs)</ code>应为 mysqli_fetch_array($ rs)</ code> </ p>
</ div>

展开原文

原文

mysql_query($sql) should be mysqli_query($mysqli,$sql)

(Connection parameter required for mysqli_query() )

mysql_num_rows($rs) should be mysqli_num_rows($rs)

mysql_fetch_array($rs) should be mysqli_fetch_array($rs)

dsq2015
dsq2015 Loko,你不认为这足以成为一个答案......给我们正确的答案
大约 3 年之前 回复
dongqiao9583
dongqiao9583 我不认为这足以成为一个答案。
大约 3 年之前 回复

You are mixing mysql and mysqli functions through one DB connection, which is wrong. You can mix mysql and mysqli but, need to have different DB connection. Read Getting a PHP PDO connection from a mysql_connect()?

Since, only one DB connection made and that is connected through mysqli. So,through out the application you have to use mysqli db connection variable for using mysqli functions.

Updated Code

<?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>";
  }
}
dongruo0909
dongruo0909 too long
大约 3 年之前 回复
dou6577
dou6577 Loko,给我正确的代码
大约 3 年之前 回复
drhgzx4727
drhgzx4727 等一下。 别问我“..你为什么回答......”好吗? 你没有人问我。 这是错字。 如果你看到了这个拼写错误,你也可以编辑它。 这并不是说谁回答有权纠正错误或拼写错误。 得到它了? @Loko
大约 3 年之前 回复
dsuw85815
dsuw85815 你为什么回答并仍然提供错误的代码。 什么是if(search ==“”){
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐