I'm getting the Call to a member function query() on a non-object when I try to call my function.
My code looks like this:
function add_profile() {
$hostname = "localhost";
$dbusername = "username";
$dbname = "dbname";
$dbpassword = "password";
$link = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname);
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
$sql = "INSERT INTO payment_profiles(id, client_id) VALUES ( '','$profile_id')";
$result = $mysqli->query($sql);
if (!result)
{
echo 'Error: ', $mysqli->error;
}
}
add_profile();
It says my error is on the line: $result = $mysqli->query($sql);
I'm assuming I'm not calling something properly. Thanks in advance for any help
In your code you're mixing both procedural and object-oriented code. Choose either one or the other. Here's how you would solve the problem the procedural way.
That's because the
$mysqli
object is not declared anywhere (or is it)? Before you can use$mysqli
you should first create an instance ofmysqli
and assign it to your object.Only then you may call the methods of the mysqli class like
$mysqli->query();
The error you made depends probably on two misconceptions:
1) you pasted half of your code from the procedural-style part of the mysqli manual and half from the oop part
2) you assume
$mysqli
is a global object instantiated with$mysqli_connect();
. It is not. You should invoke the constructor with thenew
keyword if you'd like to use it as an object.