Okay so Im gettin an error of Fatal error: Call to a member function fetch_array() on a non-object in C:\wamp\www\Usersearch\search.php on line 54...Im trying a tutorial on AJAX live search...http://ninetofive.me/blog/build-a-live-search-with-ajax-php-and-mysql...I only replaced the database connection to my own database...
here's the Usersearch.php

<?php
/************************************************
    The Search PHP File
************************************************/


/************************************************
    MySQL Connect
************************************************/

// Credentials
$dbhost = "localhost";
$dbname = "sics";
$dbuser = "root";
$dbpass = "";

//  Connection
global $tutorial_db;

$tutorial_db = new mysqli();
$tutorial_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$tutorial_db->set_charset("utf8");

//  Check Connection
if ($tutorial_db->connect_errno) {
    printf("Connect failed: %s\n", $tutorial_db->connect_error);
    exit();
}

/************************************************
    Search Functionality
************************************************/

// Define Output HTML Formating
$html = '';
$html .= '<li class="result">';
$html .= '<a target="_blank" href="urlString">';
$html .= '<h3>nameString</h3>';
$html .= '<h4>functionString</h4>';
$html .= '</a>';
$html .= '</li>';

// Get Search
$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search_string = $tutorial_db->real_escape_string($search_string);

// Check Length More Than One Character
if (strlen($search_string) >= 1 && $search_string !== ' ') {
    // Build Query
    $query = 'SELECT * FROM search WHERE function LIKE "%'.$search_string.'%" OR name LIKE "%'.$search_string.'%"';

    // Do Search
    $result = $tutorial_db->query($query);
    while($results = $result->fetch_array()) {
        $result_array[] = $results;
    }

    // Check If We Have Results
    if (isset($result_array)) {
        foreach ($result_array as $result) {

            // Format Output Strings And Hightlight Matches
            $display_function = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['function']);
            $display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['name']);
            $display_url = 'http://php.net/manual-lookup.php?pattern='.urlencode($result['function']).'&lang=en';

            // Insert Name
            $output = str_replace('nameString', $display_name, $html);

            // Insert Function
            $output = str_replace('functionString', $display_function, $output);

            // Insert URL
            $output = str_replace('urlString', $display_url, $output);

            // Output
            echo($output);
        }
    }else{

        // Format No Results Output
        $output = str_replace('urlString', 'javascript:void(0);', $html);
        $output = str_replace('nameString', '<b>No Results Found.</b>', $output);
        $output = str_replace('functionString', 'Sorry :(', $output);

        // Output
        echo($output);
    }
}


/*
// Build Function List (Insert All Functions Into DB - From PHP)

// Compile Functions Array
$functions = get_defined_functions();
$functions = $functions['internal'];

// Loop, Format and Insert
foreach ($functions as $function) {
    $function_name = str_replace("_", " ", $function);
    $function_name = ucwords($function_name);

    $query = '';
    $query = 'INSERT INTO search SET id = "", function = "'.$function.'", name = "'.$function_name.'"';

    $tutorial_db->query($query);
}
*/
?>

I really don't to be reiterating the common knoweledge at all times, but I think this one is one of those exemptions.

Codes below are bad

// Connection
global $tutorial_db;
$tutorial_db = new mysqli();
$tutorial_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$tutorial_db->set_charset("utf8");
// Check Connection
if ($tutorial_db->connect_errno) {
printf("Connect failed: %s\n", $tutorial_db->connect_error);
exit();
}

We can instantiate the MySQLI object in one declaration like this

$tutorial_db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

we catch error if there is one

    if ($tutorial_db->connect_errno) {
        printf("Connect failed: %s\n", $tutorial_db->connect_error());
        exit();
      }

I still don't understand the unecessary while loop followed by foreach loop. However if that is what you want, change this block of codes

 // Do Search
    $result = $tutorial_db->query($query);
    while($results = $result->fetch_array()) {
    $result_array[] = $results;
    }

with this

 $result_array[] = array(); //or like this $result_array = array();
 $result = $tutorial_db->query($query);
    while($results = $result->fetch_assoc()) {
    $result_array[] = $results;
    }

Alternatively, you can eliminate the entire foreach loop and just do it like this

$result = $tutorial_db->query($query);

if($result->num_rows > 0) {
    while($result_array = $result->fetch_assoc()) {

    /*
    * add items from foreach loop here as shown on your code
    */

    echo $result_array['function']; //this is a test

    }
}

else {

echo 'Sorry your request does not exist';

}

here is another really quick and dirty MySQLI connector using foreach loop .

<?php

$con =mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);

## Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

    $sql='SELECT * FROM search WHERE function LIKE "%'.$search_string.'%" OR name LIKE "%'.$search_string.'%"';

    $result=mysqli_query($con,$sql);

    ## Fetch all all the result array
    $result_array = mysqli_fetch_all($result,MYSQLI_ASSOC);

    ## Free result_array set
    mysqli_free_result($result_array);

    mysqli_close($con);

    foreach($result_array as $v){

        echo $v['function'].'<br/>';
    }

thanks..dude..ima remember everything

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.