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

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

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();

Thanks evstevemd.

You are welcome! If it is solved mark it solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.