mysqli_query works in phpmyadmin but not in php

2019-09-20 02:38发布

I have looked for an answer for ages now, lots of similar questions but found no solutions yet...

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;

The database connection works fine, I am able to input data through php.

Any suggestions? (Anyone's help is greatly appreciated).

标签: php mysql mysqli
1条回答
在下西门庆
2楼-- · 2019-09-20 03:16

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>'; 
} 
查看更多
登录 后发表回答