Jump to content
Adamhumbug

function prepare() on null mixing Object and Procedural

Recommended Posts

Hi All,

I have the following code which works fine:

$mysqli = new mysqli("server", "user", "pass", "database");
if($mysqli->connect_error) {
exit('Could not connect');
}



$sql = "SELECT customer_contactname, customer_companyname, customer_phonenumber, customer_emailaddress, customer_address1, customer_address2, customer_city, customer_postcode
FROM ssm_customer WHERE customer_id = ?";

$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $_GET['q']);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($ccontname, $ccompname, $cphoneno, $cemail, $cad1, $cad2, $ccity, $cpost);
$stmt->fetch();
$stmt->close();

however, when i use my dbconn.php as an include rather than the top section of this code, i get the following error: function prepare() on null

The error references the following

$stmt = $mysqli->prepare($sql);

In my database connection file, i am using the following:

$conn = mysqli_connect($db_servername, $db_username, $db_password, $db_name);

This seems to be the issue and i believe i am mixing procedural and object based but not sure why there should be a difference here?

when i used my database connection file, i do change $mysqli->prepare to $conn->prepare

Kind Regards

Edited by Adamhumbug

Share this post


Link to post
Share on other sites

The problem isn't you mixing styles. The problem is you using $mysqli in one place and $conn in another. So yes, if you use your include then you need to change your $mysqli variables to $conn variables.

Share this post


Link to post
Share on other sites

Never mind. Requinix beat me.

Edited by gw1500se
  • Haha 1

Share this post


Link to post
Share on other sites
12 minutes ago, Adamhumbug said:

when i used my database connection file, i do change $mysqli->prepare to $conn->prepare

Hi Requinix, is there something that i am missing?

Share this post


Link to post
Share on other sites

Actually, you were both correct and i had a spelling mistake when i changed the code over.  Thanks both

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.