0

Hello everyone i'm getting the following error message.

Fatal error: Call to a member function prepare() on a non-object in C:\xampplite\htdocs\Prepared_Statements.php on line 20

My code so far

connection.php

$db_server = "localhost";
$db_user = "root";
$db_password = "";
$db = "test";

$connection = mysql_connect($db_server,$db_user,$db_password,$db);
if(!$connection){
	die("Database Connection Failed".mysql_error());
}
$select_database = mysql_select_db($db,$connection);
if(!$select_database){
	die("Database selection failed".mysql_error());
}

Prepared_Statements.php

include_once("connection.php");

$name = "John";
$surname = "Smith";
$address = "London";
$number = 63.23;;

$query = "INSERT INTO books VALUES(?,?,?,?)";
$stmt = $db->prepare($query);
$stmt->bind_param("sssd",$name,$surname,$address,$number);//"sssd"= string,string,string and double
$stmt->execute();
echo $stmt->affected_rows.'Book inserted into rows';
$stmt->close();

Could you please suggest me a solution to this problem.
And im using PHP Version 5.3.1 with xampp
Thank you for your time

Edited by Jhon E: n/a

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Stefano Mtangoo
0

In connection.php you are connecting with MySQL functions but in Prepared_Statements.php you are trying to execute queries with the MySQLi functions.

0

In connection.php you are connecting with MySQL functions but in Prepared_Statements.php you are trying to execute queries with the MySQLi functions.

Thank you very much Insensus. Problem solved!

connection.php

$db_host = "localhost"; 
$db_username = "root";  
$db_pass = "";  
$db_name = "test"; 


$myConnection = mysqli_connect("$db_host","$db_username","$db_pass", "$db_name") or die ("could not connect to mysql");

Prepared_Statements.php

include_once("connection.php");

$name = "Yrol";
$surname = "Fernando";
$address = "London";
$number = 63.23;

$query = "INSERT INTO books VALUES(?,?,?,?)";
$stmt = $myConnection->prepare($query);
$stmt->bind_param("sssd",$name,$surname,$address,$number); // "sssd" = string,string,string and double
$stmt->execute();
echo $stmt->affected_rows.'Book inserted into rows';
$stmt->close();

Edited by Jhon E: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.