gpt4 book ai didi

php - mysqli_query() 期望参数 1 为 mysqli,字符串给出

转载 作者:行者123 更新时间:2023-11-29 22:55:07 24 4
gpt4 key购买 nike

这是我的 php 文件

<?php

/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['userName']) && isset($_POST['userContact']) && isset($_POST['userAddress']) && isset($_POST['userStore']) && isset($_POST['userRequest'])) {

$userName = $_POST['userName'];
$userContact = $_POST['userContact'];
$userAddress = $_POST['userAddress'];
$userStore = $_POST['userStore'];
$userRequest = $_POST['userRequest'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();


// mysql inserting a new row
$result = "INSERT INTO userrequests(userName, contactNumber, userAddress, storeList, requestBody) VALUES('$userName', '$userContact', '$userAddress', '$userStore', '$userRequest')";


// check if row inserted or not
if (mysqli_query($result,$db)) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";

// echoing JSON response
echo json_encode($response);
echo $result;
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "IsitdispllayingthusOops! 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.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, DB_DATABASE) 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_query() 期望参数 1 为 mysqli,给出的字符串。有人知道如何解决这个问题吗?我已经通过 StackOverFlow 搜索了这方面的内容并尝试了所有解决方案,但仍然无法解决。

最佳答案

修改您的数据库连接类以维护连接:

class DB_CONNECT {

protected $connection = null;

// constructor
function __construct() {
// connecting to database
$this->connection = $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, DB_DATABASE) 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;
}

public function getConnection() {
return $this->connection;
}

/**
* Function to close db connection
*/
function close() {
// closing db connection

}

}

然后将查询行修改为:

if (mysqli_query($db->getConnection(), $result)) {

关于php - mysqli_query() 期望参数 1 为 mysqli,字符串给出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28742217/

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