-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>" } } ?> 
3
  • 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 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. Commented Jul 24, 2017 at 11:20
  • 1
    echo "<center> <b> Please write something in Search Box </b> </center>" misses a ; at the end. Commented Jul 24, 2017 at 11:23
  • How said Loko, you have a error in line of echo.. Commented Jul 24, 2017 at 11:28

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

2 Comments

I dont think this is enough to be an answer.
Loko, you don't think that this is enough to be an answer... give us the right answer
-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>"; } } 

3 Comments

Why do you answer and still provide wrong code. What is if (search == "") {
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
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.