Custom search php for wordpress

i got an warning like this “mysql_fetch_assoc() expects parameter 1 to be resource”, how can i fix this?
Thanks!

global $wpdb;

$con=mysqli_connect("localhost","root","");
mysqli_select_db('search');

$search = mysql_real_escape_string(trim($_POST['searchquery']));
$sqlCommand = $wpdb->get_results("SELECT * FROM `wp_doctors` WHERE `name` LIKE '%searchquery%'");


while($row = mysql_fetch_assoc($sqlCommand)){
    $id = $row['id'];
    $name = $row['name'];
    $spec = $row['spec'];
    echo "Nume: $name<br />Specializare: $spec";
}

Related posts

Leave a Reply

2 comments

  1. There is no need for this ,

    $row = mysql_fetch_assoc($sqlCommand)

    $wpdb->get_results already did the job just var_dump $sqlCommand and you will see the mistake.

  2. Mulitple row results can be pulled from the database with get_results. The function returns the entire query result as an array,

    So you could make this change in your code:

    global $wpdb;
    
    $con=mysqli_connect("localhost","root","");
    mysqli_select_db('search');
    
    $search = mysql_real_escape_string(trim($_POST['searchquery']));
    $sqlCommand = $wpdb->get_results("SELECT * FROM `wp_doctors` WHERE `name` LIKE '%searchquery%'");
    
    foreach ( $sqlCommand as $result) 
    {
        echo $result->id;
            echo $result->name;
            echo $result->spec;
    }