Friday, 21 April 2017

php - converting from MySQL to MySQLi Object-oriented

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("A fatal MySQL error occured.
Query: ".$query."
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

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...