问题描述
我已经寻找年龄的答案,很多类似的问题,但没有找到解决方案...
I have looked for an answer for ages now, lots of similar questions but found no solutions yet...
无论如何,我想要做的就是使用mysqli_query从数据库中获取用户的ID,当我在phpmyadmin中使用它时,查询似乎可以工作,但是当我将其用作php脚本的一部分时,查询不起作用.
Anyway,all I am trying to do is get the id of a user from the database using a mysqli_query, the query seems to work when I use it in phpmyadmin but doesn't when I use it as part of a php script.
$username = "bob";
$db = mysqli_connect("localhost", "username", "password", "user_data");
$sql1 = "select id from user_information where username='$username'";
$id = mysqli_query($db, $sql1) or die(mysql_error());
echo $id;
数据库连接工作正常,我能够通过php输入数据.
The database connection works fine, I am able to input data through php.
有什么建议吗? (非常感谢任何人的帮助.)
Any suggestions? (Anyone's help is greatly appreciated).
推荐答案
您无法从mysqli_query打印结果,它是mysqli_resource,并且要转储错误,您需要将mysql_error()更改为mysqli_error()
you can't print the result from mysqli_query, it is mysqli_resource and for dumping the error you need to change mysql_error() to mysqli_error()
$username = "bob";
$db = mysqli_connect("localhost", "username", "password", "user_data");
$sql1 = "select id from user_information where username='$username'";
$result = mysqli_query($db, $sql1) or die(mysqli_error());
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo $row['id'].'<br>';
}
这篇关于mysqli_query在phpmyadmin中有效,但在php中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!