-2

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>"
            }
            }
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Habe
  • 1
  • 6
  • Hello. You're mixing up `mysqli` and `mysql` - that doesn't work. Please stop using the `mysql_*` driver, its outdated and not longer supported in PHP7. Use `mysqli_*` only instead, then your code will work. Simply adapt your querys, oh and btw have a look at http://bobby-tables.com and learn something about SQL injection. With this code your database can be hacked in a few seconds without any need of deep knowledge about databases.. simply SQL injection would work perfectly on this one. – Twinfriends Jul 24 '17 at 11:20
  • 1
    `echo "
    Please write something in Search Box
    " ` misses a `;` at the end.
    – Loko Jul 24 '17 at 11:23
  • How said Loko, you have a error in line of echo.. – Maxi Schvindt Jul 24 '17 at 11:28

2 Answers2

2

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)

Vimukthi Guruge
  • 285
  • 3
  • 15
-1

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>";
  }
}
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • Why do you answer and still provide wrong code. What is `if (search == "") {` – Loko Jul 24 '17 at 14:32
  • Wait a minute. Don't ask me "*.. Why do you answer..*" Ok? You are no one to ask me. That's Typo. If you saw that typo, you can also edit it. It's not like who answered is having authority to correct the mistakes or typo. Got it? @Loko – Nana Partykar Jul 24 '17 at 15:05
  • @NanaPartykar Literally thats the reason why it went bad in the first place. A couple of typos. It makes no sense to me that you even try to answer this. Just for easy reputation. – Loko Jul 25 '17 at 07:04