-1

I'm trying to change my code to mysqli, I heard mysql isn't recommended and I'm having trouble changing my code to mysqli.

I managed to connect to my database but the rest of the code doesn't seem to work, I tried changing all mysql to mysqli but it didn't work. What could I do so the code works the same but in mysqli?

Could someone rewrite it so I could see the difference. I haven't seen any good tutorials.

<?php
 $db = new mysqli('localhost','root', '', 'searchengine');

    if($db->connect_errno) {
        die('sorry we are having some problbems');
        }

$sql = mysql_query(sprintf(
    "SELECT * FROM searchengine WHERE pagecontent LIKE '%s' LIMIT 0,%d",
        '%'. mysql_real_escape_string($_GET['term']) .'%',
        mysql_real_escape_string($_GET['results']))
);

while($ser = mysql_fetch_array($sql)) {
    echo "<h2><a href='$ser[pageurl]'>$ser[pageurl]</a></h2>";
}

mysql_close();
luchaninov
  • 6,792
  • 6
  • 60
  • 75
  • Your main problem appears to be that you're still using *mysql* functions like `mysql_query()`, `mysql_real_escape_string()` and `mysql_fetch_array()` – Phil May 09 '14 at 01:38
  • 1
    http://stackoverflow.com/questions/12020227/updating-from-mysql-to-mysqli – Barmar May 09 '14 at 01:38
  • 1
    @Phil That's because he hasn't done the rewrite, he's asking us to do it for him. – Barmar May 09 '14 at 01:39
  • You're mixing two types of MySQL functions. You start off with one, (*the "good" one*), then you're using the deprecated version. Add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` and troubleshoot it from there. – Funk Forty Niner May 09 '14 at 01:39
  • 1
    @Barmar Well, he started off in the right direction with `new mysqli()` – Phil May 09 '14 at 01:40
  • @Barmar That link you gave, should have been marked with as being a duplicate, using it to vote to close. – Funk Forty Niner May 09 '14 at 01:41
  • I wasn't sure -- It's mostly a pointer to a tool that automates it, but it's not really a great tool. – Barmar May 09 '14 at 01:42
  • @Barmar Urgh, not that Oracle tool I hope. That thing is awful – Phil May 09 '14 at 01:44
  • That's my point. I used it once a couple of years ago, the resulting code was a mess. But if you have a huge amount of code to convert, the choice may be between a mess and not doing it at all. – Barmar May 09 '14 at 01:45
  • Either way, this question is what Barmar also stated, asking "us" to do the work. I'm voting to close as unclear. It's already been asked more times than I can remember. (Actually, voted as ***primarily opinion-based***), because that's exactly what's going to happen, one will use prepared statements, and another PDO etc. – Funk Forty Niner May 09 '14 at 01:48
  • The documentation is your friend: http://php.net/manual/en/book.mysqli.php – t j May 09 '14 at 02:01

2 Answers2

2
<?php
 $db = new mysqli('localhost','root', '', 'searchengine');

    if($db->connect_errno) {
        die('sorry we are having some problbems');
        }

$sql = $db->query(sprintf(
    "SELECT * FROM searchengine WHERE pagecontent LIKE '%s' LIMIT 0,%d",
        '%'. $db->real_escape_string($_GET['term']) .'%',
        $db->real_escape_string($_GET['results']))
);

while($ser = $sql->fetch_array()) {
    echo "<h2><a href='" . $ser['pageurl'] . "'>" . $ser['pageurl'] . "</a></h2>";
}

$db->close();
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • Change `$ser[pageurl]` to `$ser['pageurl']` to prevent warnings. – Ozair Patel May 09 '14 at 01:38
  • @Phil Legitimate, but it will throw warnings if you don't suppress (bad idea). – Ozair Patel May 09 '14 at 01:40
  • I changed it to concatenation just for cleanliness. Better to tick your array vars anyways – Machavity May 09 '14 at 01:40
  • @OPatel No, it won't. Does nobody read the manual anymore :(. This is perfectly fine and triggers no warnings or notices - `"Array keys in strings like $arr[key] do not need to be quoted"` – Phil May 09 '14 at 01:41
  • `$sql->fetch_array($sql)` this line will not work, because you're mixing up contexts. When calling fetch_array in this manner, you don't have to pass $sql as a parameter. Doing so will cause an error. – DiMono Mar 23 '15 at 22:23
  • Yeah, it shouldn't have been there. Fixed – Machavity Mar 24 '15 at 00:16
0

Alternative to the answer of @Machavity who's using a object oriented style you can use a procedural style as well:

<?php
    $db = mysqli_connect('localhost','root', '', 'searchengine');

    if(!$db) {
        die('sorry we are having some problems');
    }

    $sql = mysqli_query(
        $db,
        sprintf(
            "SELECT * FROM searchengine WHERE pagecontent LIKE '%s' LIMIT 0,%d",
            '%'. mysqli_real_escape_string($db,$_GET['term']) .'%',
            mysqli_real_escape_string($db,$_GET['results'])
        )
    );

    while($ser = mysqli_fetch_array($sql)) {
        echo "<h2><a href='$ser[pageurl]'>$ser[pageurl]</a></h2>";
    }

    mysqli_close($db);
?>
Robin
  • 1,208
  • 8
  • 17