Jump to content

Recommended Posts

It's been a while since I have written any php so please excuse me if this code isn't very well constructed.

I get an error-

 

 Fatal error: Call to a member function prepare() on a non-object in insert2.php on line 12

<?php
function db_connect() {
       $config = parse_ini_file('config.ini'); 
       $db = new mysqli($config['host'],$config['username'],$config['password'],$config['dbname']);
	   }

if ( isset($_POST['description']) && isset($_POST['name']) )
{
    $description = $_POST['description'];
    $name =        $_POST['name'];
    $sql = "UPDATE shrubs SET description=? WHERE name=? ";
    $stmt = db_connect()->prepare( $sql ) or die( "could not prepare statement");
    $stmt->bind("ss",$description,$name) or die( "could not bind parameters");
    if ( $stmt->execute() )
    { 
        echo "Description Added Successfully" ;
    } else {
        echo "Error in query: " . $stmt->error;
    }
}
?>

Where am I going wrong and how can I rectify this problem?

 

Link to comment
https://forums.phpfreaks.com/topic/295213-fatal-error-when-using-mysqli-prepare/
Share on other sites

Thankyou.

 

I have added return db, and now im getting Fatal error: Call to undefined method mysqli_stmt::bind()   :-\ 

<?php
function db_connect() {
       $config = parse_ini_file('config.ini'); 
       $db = new mysqli($config['host'],$config['username'],$config['password'],$config['dbname']);
	   return $db;
	   }

if ( isset($_POST['description']) && isset($_POST['name']) )
{
    $description = $_POST['description'];
    $name =        $_POST['name'];
    $sql = "UPDATE shrubs SET description=? WHERE name=? ";
    $stmt = db_connect()->prepare( $sql ) or die( "could not prepare statement");
    $stmt->bind("ss",$description,$name) or die( "could not bind parameters");
    if ( $stmt->execute() )
    { 
        echo "Description Added Successfully" ;
    } else {
        echo "Error in query: " . $stmt->error;
    }
}
?>

I'm not comfortable with using a function in place of a variable (or object). How about simplifying your code and making the call and then using the returned variable in the bind call? You might also want to add some error checking code in your db_connect function to be sure it creates that object $db.

and now im getting Fatal error: Call to undefined method mysqli_stmt::bind()   :-\

At this point you should be thinking "Undefined method? You mean that mysqli_stmt does not have a 'bind' method? I wonder what methods it does have..." and then heading over to the documentation.

 

and now im getting Fatal error: Call to undefined method mysqli_stmt::bind()   :-\

 

You should be using bind_param.

 

Instead of . .

 

$stmt->bind("ss",$description,$name) or die( "could not bind parameters");

 

Use  . . .

$stmt->bind_param("ss",$description,$name) or die( "could not bind parameters");

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.