![]() |
In need of help understanding how to correctly connect and use MySQLi
I have created a testing code as I have not been able to understand how mysqli work or how I use it.
Below is the code I have setup that is currently showing the following error... Fatal error: Call to a member function query() on a non-object on line 20 Line 20 being Code:
$result = $mysqli->query("$query");Does anyone know of a website that shows examples of how to correctly use mysqli, I have tried php.net but so far not finding much help. Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
You got used to the autoglobal of the mysql resource (which was a really bad idea on zend's part). MySQLi doesn't exist outside of the scope it was created. You need to pass the resource to the functions in question.
PHP Code:
mysqli_init() is also not required. This is only required when you plan on modifying a few of the settings which must occur between the mysql_init and before the new mysqli() or mysqli_connect calls. There's not a whole lot of these configurations that require this though. |
so in short i can not use a database connection in a function unless I setup a connection inside the function as well or pass the connection resources to the function as well.
|
here is my code now. but this time i get the following error
Parse error: syntax error, unexpected T_VARIABLE on this line Code:
$getErrors = db_query(MySQLi $mysqli, "SELECT * FROM `errors` ORDER BY `id` DESC");Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
ok i think i have it...
the thing that is confusing the issue for me is this -> i do not understand what it does. whats is it, i really need to find out more about it. as the testing page now shows this error... mysql_num_rows() expects parameter 1 to be resource, object given in but i am guessing that this error is because i am using a mysql command and not a mysqli command to count the rows. I hope! |
Now I am even more confused about 'determining if it should issue a ->query or ->prepare statement.'
just about to google that! EDIT: ok i think i will need the basic query functions, nothing that will repeat the query, only used the once and use the returned data and then kill the results. i have found information about this 'arrow operator' and only found what it is called. But still not sure what it actually does. |
This isn't correct here:
$getErrors = db_query(MySQLi $mysqli, "SELECT * FROM `errors` ORDER BY `id` DESC");. You cannot provide a datatype hint during the calling of the function; I mean the signature of the function can accept the typehint:PHP Code:
PHP Code:
-> is object oriented de-reference operator. It resolves an object to a property or method of the class' instance. To use in context as I've indicated with the third parameter of the method, that would be like so: PHP Code:
If that works as I intend it to, than it would ultimately return a mysqli_result regardless of if it used a statement or not. I can't be 100% sure it would work; I've used pretty much prepared statements exclusively since the mysqli was released. |
ah I think I have cracked it... $result = db_query($mysqli, $query);
it is starting to make sense now. |
| All times are GMT +1. The time now is 10:28 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.