I have php code for an upload form, and am trying to update this mysql to the recommended mysqli. I am trying to learn the OO style (and also procedural, but am leaning towards learning the OO style).
我有一个上传表单的PHP代码,我正在尝试将此mysql更新为推荐的mysqli。我正在努力学习OO风格(也是程序性的,但我倾向于学习OO风格)。
I have been successful on updating all my codes so far, except for this snippet:
到目前为止,我已成功更新了所有代码,但此代码段除外:
function query($query) {
$database = $this->options['database'];
$host = $this->options['host'];
$username = $this->options['username'];
$password = $this->options['password'];
$link = mysql_connect($host,$username,$password);
if (!$link) {
die(mysql_error());
}
$db_selected = mysql_select_db($database);
if (!$db_selected) {
die(mysql_error());
}
$result = mysql_query($query);
mysql_close($link);
return $result;
}
Can anyone help me translate that into MySQLi, object oriented style? I tried so many combinations I don't know where I am going wrong (I am getting a db error when trying to upload a photo).
任何人都可以帮我翻译成MySQLi,面向对象的风格吗?我尝试了很多组合,我不知道我哪里出错了(我在尝试上传照片时遇到了db错误)。
thanks!
1 个解决方案
#1
0
This should work for you. I'm not sure that a mysqli result will survive a mysqli->close() so I've retrieved the data from the $result into an array and returned that. You might want to test this.
这应该适合你。我不确定mysqli结果会在mysqli-> close()中存活,所以我将$ result中的数据检索到数组中并返回。你可能想测试一下。
I also wonder why you're opening the connection and closing it again around each query. I'd normally open the connection and leave it open until the script has finished all its queries.
我也想知道你为什么打开连接并在每个查询周围再次关闭它。我通常打开连接并保持打开状态,直到脚本完成所有查询。
function query($query) {
// no real need for this - the variables can be used directly
$database = $this->options['database'];
$host = $this->options['host'];
$username = $this->options['username'];
$password = $this->options['password'];
// instantiate the mysqli object. Select the database at the same time.
$mysqli = new mysqli($host, $username, $password, $database);
if ($mysqli->connect_error) {
die($mysqli->connect_error);
}
$result = $mysqli->query($query);
if ($result !== false) {
// retrieve data
$data = array();
while ($data[] = $result->fetch_assoc()) {}
// release the result set and close the connection
$result->free();
$mysqli->close();
return $data;
} else {
syslog(LOG_ERROR, "SQL error:".$mysqli->error);
$mysqli->close();
return false;
}
}
#1
0
This should work for you. I'm not sure that a mysqli result will survive a mysqli->close() so I've retrieved the data from the $result into an array and returned that. You might want to test this.
这应该适合你。我不确定mysqli结果会在mysqli-> close()中存活,所以我将$ result中的数据检索到数组中并返回。你可能想测试一下。
I also wonder why you're opening the connection and closing it again around each query. I'd normally open the connection and leave it open until the script has finished all its queries.
我也想知道你为什么打开连接并在每个查询周围再次关闭它。我通常打开连接并保持打开状态,直到脚本完成所有查询。
function query($query) {
// no real need for this - the variables can be used directly
$database = $this->options['database'];
$host = $this->options['host'];
$username = $this->options['username'];
$password = $this->options['password'];
// instantiate the mysqli object. Select the database at the same time.
$mysqli = new mysqli($host, $username, $password, $database);
if ($mysqli->connect_error) {
die($mysqli->connect_error);
}
$result = $mysqli->query($query);
if ($result !== false) {
// retrieve data
$data = array();
while ($data[] = $result->fetch_assoc()) {}
// release the result set and close the connection
$result->free();
$mysqli->close();
return $data;
} else {
syslog(LOG_ERROR, "SQL error:".$mysqli->error);
$mysqli->close();
return false;
}
}