Php mysqli_query()要求参数1为mysqli,对象在
当我尝试使用wampServer在mysql中保存数据库时,出现了这个错误 mysqli_query()要求参数1为mysqli,对象在 有3个PHP文件,db_config.PHP和db_connect.PHP,以及create_person.PHP,我已经在phpmyadmin中创建了表person db_config.phpPhp mysqli_query()要求参数1为mysqli,对象在,php,mysqli,Php,Mysqli,当我尝试使用wampServer在mysql中保存数据库时,出现了这个错误 mysqli_query()要求参数1为mysqli,对象在 有3个PHP文件,db_config.PHP和db_connect.PHP,以及create_person.PHP,我已经在phpmyadmin中创建了表person db_config.php <?php /* * All database connection variables */ define('DB_USER', "ro
<?php
/*
* All database connection variables
*/
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "othmane"); // database name
define('DB_SERVER', "localhost"); // db server
?>
<?php
/**
* A class file to connect to database
*/
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysqli_error());
// Selecing database
$db = mysqli_select_db($con, DB_DATABASE) or die(mysqli_error()) or die(mysqli_error());
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysqli_close();
}
}
?>
db_connect.php
<?php
/*
* All database connection variables
*/
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "othmane"); // database name
define('DB_SERVER', "localhost"); // db server
?>
<?php
/**
* A class file to connect to database
*/
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysqli_error());
// Selecing database
$db = mysqli_select_db($con, DB_DATABASE) or die(mysqli_error()) or die(mysqli_error());
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysqli_close();
}
}
?>
创建person.php:
<?php
/*
* Following code will create a new person row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['nom']) && isset($_POST['pass_world'])) {
$name = $_POST['nom'];
$pass = $_POST['pass_world'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result =mysqli_query($db,"INSERT INTO person (nom,pass_world) VALUES ('$name', '$pass')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
您的
返回DB_CONNECT类的实例
你的生活需要什么
mysqli_query(...)
call是mysqli\u connect调用返回的对象。
尝试更改您的:
function __construct() {
// connecting to database
$this->connect();
}
与:
然后,改变:
$result =mysqli_query($db,"INSERT INTO person (nom,pass_world) VALUES ('$name', '$pass')");
与:
此外,我认为您应该阅读PHP手册,了解您正在使用的函数:
您的
返回DB_CONNECT类的实例
你的生活需要什么
mysqli_query(...)
call是mysqli\u connect调用返回的对象。
尝试更改您的:
function __construct() {
// connecting to database
$this->connect();
}
与:
然后,改变:
$result =mysqli_query($db,"INSERT INTO person (nom,pass_world) VALUES ('$name', '$pass')");
与:
此外,我认为您应该阅读PHP手册,了解您正在使用的函数:
只是一个小的修正。我们不通过if(isset($\u POST['nom'])…
检查必填字段,因为即使它们仍然是空的,它们仍然被设置。也就是说,空也是一个值。试试if($\u POST['nom'])!='
类似的东西,只是一个小的更正。我们不通过if(isset($\u POST['nom'])检查必填字段…
因为即使它们仍然是空的,它们仍然被设置。也就是说,empty也是一个值。试试if($\u POST['nom']!='')
类似的东西谢谢你,行被保存到表中,但是我仍然在close()方法中出错,我尝试了$this->con=$this->close();但是它不起作用,我应该使用mysqli\u close($this->con);谢谢你说得对,我应该读PHP手册是的!总是阅读PHP手册。大多数时候,你会在那里找到答案。谢谢你,行被保存到表中,但我仍然在close()方法中出错,我尝试了这个$this->con=$this->close();但它不起作用,我应该使用mysqli\u close($this->con);谢谢你说得对,我应该读PHP手册是的!总是阅读PHP手册。大多数时候,你会在那里找到答案。