Welcome to WebmasterWorld Guest from 54.146.248.111

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

MySQL to Mysqli query inside method

     

hairytongue

11:16 am on Jul 10, 2013 (gmt 0)



Hi Folks

Basically, I'm looking for some pointers in the right direction here. I am converting from MySQL to MySqli. What I have done so far is not working. Hints, tips examples or suggestions on how I can resolve this as simply as possible would be much appreciated(please bare I'm still new to PhP)


This is the old MySQL code.

 function getResult($sql) {
$result = mysql_query($sql, $this->conn);
if ($result) {
return $result;
} else {
die("SQL Retrieve Error: " . mysql_error());
}
}


This is the new MySQLI code.

 function getResult($sql){

$result = mysqli_query($this->conn->$sql); //Line 94!
if ($result) {
return $result;
} else {
die("SQL Retrieve Error: " . mysqli_error()); //98
}
}



This is the call from the web page.

include 'memberfunc.php'; //import the class in this web page 

$db1 = new dbmember();
$db1->openDB();
$sql="SELECT * from member";
$result=$db1->getResult($sql);


Here are the errors.

Notice: Trying to get property of non-object in C:\xampp\htdocs\1\memberfunc.php on line 94

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\1\memberfunc.php on line 94

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\cw1\memberfunc.php on line 98
SQL Retrieve Error:


Now, I somewhat understand what the errors are saying. But I can't mentally map out how to resolve it.

hairytongue

11:24 am on Jul 10, 2013 (gmt 0)



Edit -

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\1\memberfunc.php on line 98
SQL Retrieve Error:

dmorison

2:43 pm on Jul 10, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From a syntax / required parameters point of view, there are 2 errors with your replacement mysqli version of the code...

In general, all mysqli_ functions require the database link to specified which is the main difference from the legacy mysql_ functions which just used the link from the last mysql_ operation if not specified.

mysqli_query() expects exactly 2 parameters, the link, and the query (your code has a single parameter of $this->conn->$sql which I assume should be $this->conn and $sql separately). mysql_error() requires exactly 1 parameter - the link.

Assuming the rest of the code is in order, specifically that $this->conn is a valid link resource created by a mysqli function then the following should work;


$result = mysqli_query($this->conn,$sql);
if ($result) {
return $result;
} else {
die("SQL Retrieve Error: " . mysqli_error($this->conn));
}
}

hairytongue

6:19 pm on Jul 10, 2013 (gmt 0)



Thanks a lot for the very clear answer, much better than the downvotes and cryptic answers on SO. Really appreciated.

So yes, your suggestion is spot on BUT. I'm racking my brains over the null-error. So I need to figure out what I should do with private conn; because it seems like I'm passing it into itself for some reason.

Maybe I need a new function with the actual SQL code to pass this->'SQLcodeherevariable' into, rather than doing the SQL code in a localised fashion i.e on the page itself.

Seems like mysqli is that different.

ATB

dmorison

7:08 pm on Jul 10, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi ATB,

The problem i'm afraid then is likely higher up in your code hierarchy, but if you don't have already, make sure that whilst you are developing that you have error reporting and display turned up to the max - that will really help!

Make sure that prior to the code you are working on, the following lines of PHP are executed:


error_reporting(E_ALL);
ini_set('display_errors','on');

swa66

11:59 am on Jul 11, 2013 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You'll get much more out of mysqli if you look into the prepared statements.

[php.net...]

It allows you to separate out the SQL code from the data, avoiding the vast majority (if not all) of the SQL injection.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month