更改代码并修复它?

时间:2017-07-24 11:18:45

标签: php

我知道我使用多种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>"
            }
            }
?>

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:-1)

您正在通过一个数据库连接混合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>";
  }
}