homepage Welcome to WebmasterWorld Guest from 23.23.57.182
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
MySQL to Mysqli query inside method
hairytongue



 
Msg#: 4591770 posted 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



 
Msg#: 4591770 posted 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

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4591770 posted 2:43 pm on Jul 10, 2013 (gmt 0)

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



 
Msg#: 4591770 posted 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

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4591770 posted 7:08 pm on Jul 10, 2013 (gmt 0)

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

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



 
Msg#: 4591770 posted 11:59 am on Jul 11, 2013 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved