3

Ok, so I've been informed that it would be best practice to convert over to the new mysqli So I've been working on this on a new site, so far so good, but I ran into a problem where I can't figure out how to convert it for my search query

I have a search feature added to my site, but now I can't get it to work.

This was my old code:

$query = "SELECT * FROM snippet_tools WHERE `db_title` LIKE ".sql_val('%'.$_GET['search'].'%')." OR `db_body`=".sql_val('%'.$_GET['search'].'%');
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());

$anymatches = mysql_num_rows($result);

if ($anymatches == 0 ) {

I've upgraded my code to user oo

this is what I have:

$servername = "localhost"; $username = "xxxxxxxxx"; $password = "xxxxxxxxx"; $dbname = "xxxxxxxxxxxx"; 
// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
}

$sql_search = "SELECT * FROM `questions` WHERE `q_title` LIKE ".sql_val('%'.$_GET['search'].'%')." OR `q_answered` LIKE ".sql_val('%'.$_GET['search'].'%');
$result = $conn->query($sql_search);

$anymatches = $result->num_rows;

if ($anymatches == 0 ) {

but every time I run it to perform a search I keep getting this error message:

Notice: Trying to get property of non-object in H:\root\site5\questions.php on line 611

roullie
  • 2,830
  • 16
  • 26
TrevTech
  • 49
  • 6
  • 2
    if the `line 611` is referring to `$result = $conn->query($sql_search);` . How did you build `$conn` object? – roullie Feb 16 '16 at 02:25
  • 3
    Possible duplicate of [Trying to get property of non-object MySQLi result](http://stackoverflow.com/questions/7332842/trying-to-get-property-of-non-object-mysqli-result) – Phiter Feb 16 '16 at 02:25
  • 1
    could you show were you create your connection (`$conn`)? – davejal Feb 16 '16 at 02:25
  • 2
    First off, which line is like `611`? Second, where do you define `$conn`? – Matthew Herbst Feb 16 '16 at 02:26
  • 1
    It would really help if you would tell us what line 611 is. – Matt Feb 16 '16 at 02:32
  • I'm calling it from another file called config.php $servername = "localhost"; $username = "xxxxxxxxx"; $password = "xxxxxxxxxx"; $dbname = "xxxxxxxxxxxx"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } – TrevTech Feb 16 '16 at 02:34
  • 2
    time to ditch out that `sql_val` function and move to prepared statements instead – Kevin Feb 16 '16 at 02:36
  • @TrevTech Still doesn't answer which line 611 is. – Matt Feb 16 '16 at 02:37
  • Add the statement `error_reporting(E_ALL);` to the top of your file, run the program, then check your `error.log` and tell us exactly which line of code is failing. – Darwin von Corax Feb 16 '16 at 04:43

2 Answers2

0

You can refer W3 School site and get basic idea about the variations of MySQL and MySQLi and their functions. It is really helpful to go ahead with your project.

Ex Database connection example in both ways

So you can solve your problem based on that concept. Try to extract concepts.

Dis_Pro
  • 633
  • 1
  • 10
  • 21
0

Try this approach!

<?php

    error_reporting(E_ALL);

    $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "cities"; 
    // Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 
    // Check connection 
    if ($conn->connect_error) { 
        die("Connection failed: " . $conn->connect_error); 
    }

    $get = array('search' => "Lon");

    $sql_search = "SELECT * FROM cities WHERE (city_name LIKE '%$get[search]%')";

    $result = $conn->query($sql_search);

    $anymatches = $result->num_rows;

    if ($anymatches == 0 ){
        echo "No matches!";
    }
    else
    {
        echo $anymatches . " match found!";
    }

?>



Screenshot of my database


Screenshot of the result

CodeWalker
  • 2,281
  • 4
  • 23
  • 50