gpt4 book ai didi

php - mysqli_query() 期望参数 1 为 mysqli,在中给出的对象

转载 作者:太空宇宙 更新时间:2023-11-03 11:50:19 26 4
gpt4 key购买 nike

<分区>

当我尝试使用 wampServer 在 mysql 中保存数据库时出现此错误。

mysqli_query() expects parameter 1 to be mysqli, object given in

有 3 个 PHP 文件,db_config.php 和 db_connect.php 以及 create_person.php,我已经在 phpmyadmin 中重新创建了表 PERSON。

db_config.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
?>

db_connect.php

<?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);
}
?>

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com