Mysqli查询仅适用于本地主机,不适用于Web服务器
我已将一些旧查询更改为Mysqli框架以提高性能.在本地主机上一切正常,但是当我将其上传到网络服务器时,它什么也没输出.连接后,我检查是否有错误,没有任何错误.我还检查了已安装的php模块并启用了mysqli.
I have changed some of my old queries to the Mysqli framework to improve performance. Everything works fine on localhost but when i upload it to the webserver it outputs nothing. After connecting I check for errors and there are none. I also checked the php modules installed and mysqli is enabled.
我确信它会创建与数据库的连接,因为不会显示任何错误. (当我更改数据库名称字符串时,它给出了错误)
I am certain that it creates a connection to the database as no errors are displayed. (when i changed the database name string it gave the error)
网络服务器上的查询没有任何输出,如下所示:
There is no output from the query on the webserver, which looks like this:
$mysqli = new mysqli("server", "user", "password");
if (mysqli_connect_errno()) {
printf("Can't connect Errorcode: %s\n", mysqli_connect_error());
exit;
}
// Query used
$query = "SELECT name FROM users WHERE id = ?";
if ($stmt = $mysqli->prepare("$query"))
{
// Specify parameters to replace '?'
$stmt->bind_param("d", $id);
$stmt->execute();
// bind variables to prepared statement
$stmt->bind_result($_userName);
while ($stmt->fetch())
{
echo $_userName;
}
$stmt->close();
}
}
//close connection
$mysqli->close();
正如我所说的那样,这段代码可以在我的本地服务器上完美地工作,只是不在网上.检查错误日志,没有任何内容,因此一切都指向一个良好的连接.所有的表也都存在,等等.任何人有任何想法,因为这使我陷入困境!另外,如果我可以使用此功能,我所有其他查询是否仍可以使用?还是需要让它们也使用mysqli框架?预先感谢.
As I said this code works perfectly on my localserver just not online. Checked the error logs and there is nothing so everything points to a good connection. All the tables exists as well etc. Anyone any ideas because this one has me stuck! Also, if i get this working, will all my other queries still work? Or will i need to make them use the mysqli framework as well? Thanks in advance.
好吧,我已经做了一些调试"来看看发生了什么.我正在查询的表中有与查询匹配的数据.我做了以下检查prepare语句:
Ok Ive done some 'debugging' to see what is happening. There is data in the table i'm querying that matches the query. I did the following to check the prepare statement:
echo "debug 1";
if ($stmt = $mysqli->prepare("$shiftQuery"))
{
echo "debug 2";
printf("Error: %s.\n", $stmt->error);
etc...
}
因此,基本上,它应该在准备该语句之前输出"debug 1"(这样做).然后,它应该输出"debug 2"和发生的任何错误.
So basically it should output 'debug 1' before the statment is prepared (Which it does). Then after it should output 'debug 2' and any errors that occured.
问题出在这里,因为它没有到达IF语句中的debug 2行.由于连接细节很好,所以我真的看不到为什么不会创建$ mysqli对象.那给任何人任何进一步的想法???
The problem is here somewhere as it doesnt reach the debug 2 line in the IF statement. Since the connection details are fine, i cant really see why the $mysqli object wouldnt be created. That give anyone any further ideas???
谢谢
每次调用mysqli/stmt方法都可能失败.您应该检查每一个.
尝试使用error_reporting(E_ALL),也许display_error = On
Each call to a mysqli/stmt method can fail. You should check each and every one.
Try it with error_reporting(E_ALL) and maybe display_error=On
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
// passing database name as fourth parameter
$mysqli = new mysqli("server", "user", "password", 'dbname');
if (mysqli_connect_errno()) {
printf("Can't connect Errorcode: %s\n", mysqli_connect_error());
exit;
}
// Query used
$query = "SELECT name FROM users WHERE id = ?";
if ( false===($stmt=$mysqli->prepare("$query")) ) {
echo 'mysqli::prepare failed: ', htmlspecialchars($mysqli->error);
die;
}
// Specify parameters to replace '?'
$rc = $stmt->bind_param("d", $id);
if ( !$rc ) {
echo 'bind_param failed: ', htmlspecialchars($stmt->error);
die;
}
echo '<pre>Debug: execute()</pre>';
$rc = $stmt->execute();
if ( !$rc ) {
echo 'execute failed: ', htmlspecialchars($stmt->error);
die;
}
echo '<pre>Debug: bind_result()</pre>';
// bind variables to prepared statement
$rc = $stmt->bind_result($_userName);
if ( !$rc ) {
echo 'bind_result failed: ', htmlspecialchars($stmt->error);
die;
}
echo '<pre>Debug: fetch()</pre>';
while ($stmt->fetch())
{
echo 'username: ', $_userName;
}
echo '<pre>Debug: stmt close()</pre>';
$stmt->close();
echo '<pre>Debug: mysqli close()</pre>';
$mysqli->close();