-1

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

Joel
  • 1
  • 2
  • 1
    `$id` is a MySQLi resource, you need to fetch it first. http://php.net/manual/en/mysqli-result.fetch-assoc.php – Qirel Dec 04 '16 at 18:22
  • 2
    And `or die(mysql_error());` won't give you anything, `mysql_` doesn't mix with `mysqli_` – Qirel Dec 04 '16 at 18:23
  • 1
    If `$username` isn't going to be static in the future you should use parameterized queries. http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – chris85 Dec 04 '16 at 18:24
  • Possible duplicate of [Can I mix MySQL APIs in PHP?](http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Peter O. Dec 05 '16 at 07:18

1 Answers1

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>'; 
} 
Diab
  • 142
  • 7