函数 getResult 调用 [英] Function getResult call
问题描述
我正在将我的功能从 MySQL 转移到 MySQLi 标准.
I'm working transferring my functions from MySQL to MySQLi standards.
这是我用于 getresult 的旧函数
This is my old function for getresult
function getResult($sql) {
$result = mysql_query($sql, $this->conn);
if ($result) {
return $result;
} else {
die("SQL Retrieve Error: " . mysql_error());
}
}
不过,我一直在关注 W3schools 关于 mysqli_query 函数.这就是我目前所在的位置.
However, I have been following the W3schools on the mysqli_query function. Here's where I'm presently at.
function getResult($connection){
$result = mysqli_query($connection->conn);
if ($result) {
return $result;
} else {
die("SQL Retrieve Error: " . mysqli_error());
}
}
现在,W3schools 上的示例与我想使用 mysqli_query 的方式略有不同.我试图让它针对我的数据库,而不是像 W3S 上的示例那样针对某些输入或常量.
Now, the examples on W3schools are just a bit different from how I want to use mysqli_query. I'm trying to make it be directed at my DB, not some input or constants as per examples on W3S.
Notice: Trying to get property of non-object in C:\xampp\htdocs\cad\func.php on line 92
Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\cad\func.php on line 92
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\cad\func.php on line 96
SQL Retrieve Error:
谢谢
推荐答案
您的版本无法运行的原因有很多,其中最重要的是您没有包含要发送的查询.假设 $this->conn 以某种方式正确设置,试试这个:
Your version won't work for a number of reasons, not the least of which is that you haven't included the query you want to send. Assuming $this->conn is set up properly somehow, try this:
function getResult($sql) {
$result = mysqli_query($this->conn, $sql); //Note parameters reversed here.
if ($result) {
return $result;
} else {
die("SQL Retrieve Error: " . mysql_error());
}
}
这篇关于函数 getResult 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!