wright67uk Posted March 13, 2015 Share Posted March 13, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/295213-fatal-error-when-using-mysqli-prepare/ Share on other sites More sharing options...
requinix Posted March 13, 2015 Share Posted March 13, 2015 db_connect() never actually returns anything. Quote Link to comment https://forums.phpfreaks.com/topic/295213-fatal-error-when-using-mysqli-prepare/#findComment-1508021 Share on other sites More sharing options...
wright67uk Posted March 13, 2015 Author Share Posted March 13, 2015 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/295213-fatal-error-when-using-mysqli-prepare/#findComment-1508023 Share on other sites More sharing options...
ginerjm Posted March 13, 2015 Share Posted March 13, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/295213-fatal-error-when-using-mysqli-prepare/#findComment-1508025 Share on other sites More sharing options...
requinix Posted March 13, 2015 Share Posted March 13, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/295213-fatal-error-when-using-mysqli-prepare/#findComment-1508027 Share on other sites More sharing options...
ginerjm Posted March 13, 2015 Share Posted March 13, 2015 Good catch! Didn't even notice that... Quote Link to comment https://forums.phpfreaks.com/topic/295213-fatal-error-when-using-mysqli-prepare/#findComment-1508028 Share on other sites More sharing options...
cutielou22 Posted March 15, 2015 Share Posted March 15, 2015 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"); Quote Link to comment https://forums.phpfreaks.com/topic/295213-fatal-error-when-using-mysqli-prepare/#findComment-1508091 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.