这是我的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搜索了这个并尝试了所有解决方案,但仍然无法解决。
答案 0 :(得分:0)
http://docs.php.net/mysqli_query:
程序风格
mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )
第一个参数必须是mysqli_connect()
返回的有效/活动连接资源答案 1 :(得分:0)
正如错误所说,你的第一个参数应该是mysqli类,而不是查询。像这样:
if (mysqli_query($db, $result)) {
答案 2 :(得分:0)
使用此
mysqli_query($db,$result) //connection first.. query at second position
而不是
mysqli_query($result,$db)
答案 3 :(得分:0)
必须是这样的
$result = "INSERT INTO userrequests(userName, contactNumber, userAddress, storeList, requestBody) VALUES('$userName', '$userContact', '$userAddress', '$userStore', '$userRequest')";
// check if row inserted or not
if (mysqli_query($db,$result)) {
}
答案 4 :(得分:0)
修改db connect类以维护连接:
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)) {