mysqli_query works in phpmyadmin but not in php

2019-09-20 03:02发布

问题:

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).

回答1:

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>'; 
} 


标签: php mysql mysqli