我已经在网上搜索了,但我没有找到任何解决我的问题的方法。我有一个类DB_Functions,使用准备语句来执行SQL查询。DB_Functions.php
<?php
class DB_Functions {
private $con;
//constructor
function __construct(){
require_once __DIR__ . "/db_connect.php";
//connecting to database
$db = new DB_CONNECT();
$this->con = $db->connect();
/ /var_dump($conn);
}
//destructor
function __destruct(){
}
//store new firm //testing
public function storeFirm($name, $email, $tel, $address, $city, $code){
var_dump($this->con);
$stmt = $this->con->prepare("INSERT INTO Charset (charset) VALUES (?)");
$stmt->bind_param("s", $name);
$stmt->execute();
$stmt->close();
}
}
?>
变量$this->con使用var_dump返回类型为(mysql link)的资源(4)
db_connect.php
class DB_CONNECT{
function __construct(){
//import database connection variables
require_once __DIR__ . "/db_config.php";
}
/*
function to connect with database
*/
function connect(){
//connecting to mysql database
$conn = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die (mysql_error() . PHP_EOL);
//select database
$db = mysql_select_db(DB_DATABASE, $conn) or die (mysql_error() . PHP_EOL);
/*check connection*/
if ( mysql_error() ){
printf("Connect failed: %s'n", mysql_error());
exit();
}
else {
mysql_set_charset("utf8", $conn) or die (mysql_error() . PHP_EOL);
//$this->setCharacterSet($con);
//returning connection cursor
//var_dump($conn);
return $conn;
}
}
/*function to close db connection*/
function close(){
//closing db connection
mysql_close();
}
//set utf-8 character set
function setCharacterSet($con){
if ( mysql_client_encoding($con) != "utf8"){
//mysql_set_charset("utf8");
printf("The current character set is : " . mysql_client_encoding($con) . "'n");
}
}
}
?>
谁能告诉我罪魁祸首在我的代码中位于哪里?
php扩展mysql
已弃用,我相信它没有准备好的语句,所以您试图在类中不存在的对象上使用函数。
mysqli
和PDO
中有预处理语句