Pregunta

I've established a successful connection via MySQLi on my website. However now my search script is outdated, and being as new to this as I am I'm struggling to understand how to convert my script to mysqli.

    function doSearch() {
    $output = '';
    if(isset($_POST['search'])) {
        $searchq = $_POST['search'];
        $searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);

        $query = mysql_query("SELECT * FROM entries WHERE name LIKE '%$searchq%' or description LIKE '%$searchq%' or content LIKE '%$searchq%'") or die("Could not search");
        $count = mysql_num_rows($query);
        if($count == 0) {
            $output = '<tr><tr>No results found.</tr></td>';
        } else {
            while($row = mysql_fetch_array($query)) {
                $eName = $row['name'];
                $eDesc = $row['description'];
                $eCont = $row['content'];
                $id = $row['id'];
                $elvl = $row['level'];
                $ehp = $row['hp'];

                $output .= '<tr><td><a href="http://seersvillage.com/v1.2/npc.php?id=' .$id. '" onclick="document.linkform.submit();">'.$eName.'</a></td><td>'.$eDesc.'</td><td>'.$elvl.'</td><td>'.$ehp.'</td></tr>';
            }
        }
    return $output;
    }
 }

As far as I understand, mysql_query needs to be changed am I right? Although I'm entirely unsure what else I need to change. I've tried looking at other examples, but I'm overfaced by the code.

I have my connect.php file attatched to the functions.php file, in which the code above is placed.

Some assistance would be much appreciated.

EDIT: Also ignore the poor scripting @ $output, although have a good laugh at it. :)

No hay solución correcta

Otros consejos

You can find more information here http://php.net/mysqli

The $connect variable is the variable you set in you connection script.

<?php
    function doSearch() {
    $output = '';
    if(isset($_POST['search'])) {
        $searchq = $_POST['search'];
        $searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);
        $sql = "SELECT * FROM entries WHERE name LIKE '%$searchq%' or description LIKE '%$searchq%' or content LIKE '%$searchq%'";
        $query = mysqli_query($connect, $sql);
        $count = mysqli_num_rows($query);
        if($count == 0) {
            $output = '<tr><tr>No results found.</tr></td>';
        } else {
            while($row = mysqli_fetch_array($query)) {
                $eName = $row['name'];
                $eDesc = $row['description'];
                $eCont = $row['content'];
                $id = $row['id'];
                $elvl = $row['level'];
                $ehp = $row['hp'];

                $output .= '<tr><td><a href="http://seersvillage.com/v1.2/npc.php?id=' .$id. '" onclick="document.linkform.submit();">'.$eName.'</a></td><td>'.$eDesc.'</td><td>'.$elvl.'</td><td>'.$ehp.'</td></tr>';
            }
        }
    return $output;
    }
 }

if you are going to the bother of upgrading your mysql you should consider PDO as PDO supports 12 different drivers, opposed to MySQLi, which supports MySQL only.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top