user3799541
user3799541

Reputation: 15

SQL: mysqli_query, no output

session_start();
$user=$_SESSION['username'];
$con=mysqli_connect("$host","$username","$password","$db_name");
$sql1="SELECT UserID FROM User WHERE username='$user'";
$query=mysqli_query($con,$sql1);
echo $query;

Im not sure what's the problem with this, I've tried searching around here and tried different ways of writing the syntax and checking for errors in parameter but still no output for the command. What am i missing here?

UPDATE

$sql1="SELECT UserID FROM User WHERE username='$user'";
$query=mysqli_query($sql1,$con);
$row=mysqli_fetch_array($query);
$uid=$row[0];
echo $uid;

As answered by the others, I tried doing this. There is still no output or an output of 0 was given. Where if I tried the query in sql, it shows the right one.

UPDATE 2

It tried the var_dump() command and it returns NULL. Does that mean it can't read the database?

Upvotes: 0

Views: 383

Answers (3)

Yannici
Yannici

Reputation: 736

mysqli_query gives no output. A successfully query returns a resource. You have to handle the resource with mysqli_fetch_object, mysqli_fetch_assoc or mysqli_fetch_array.

Also you gave $con parameter before $sql1 which is wrong. You have to put the connection first, then the query variable.

Example:

<?php

session_start();
$user = $_SESSION['username'];
$con = mysqli_connect("$host", "$username", "$password", "$db_name");
$sql1 = "SELECT UserID FROM User WHERE username='$user'";
$query = mysqli_query($con, $sql1);

$fetch = mysqli_fetch_assoc($query);

var_dump($fetch);

?>

Upvotes: 1

user3799541
user3799541

Reputation: 15

I was able to do some workaround with my problem using mysql syntax. Still not able to determine the problem with mysqli but able to finished my task. Thanks for answering guys.

Upvotes: 0

Zander
Zander

Reputation: 1086

$query will be a "Resource". You can use for example $result = mysqli_fetch_array($query); and then echo $result[0];May there's another way, but that's a simple one!

Upvotes: 0

Related Questions