0
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?

11
  • where is the code of output or echo? Commented Jul 3, 2014 at 6:57
  • Actually you do not any code for display output Commented Jul 3, 2014 at 6:57
  • @Sadikhasan oh sorry, forgot to indicate in the post. I did use echo. Commented Jul 3, 2014 at 6:58
  • It's required to fetch data once query is executed successfully. Commented Jul 3, 2014 at 6:59
  • @RajendraYadav I used echo, updated the post. Commented Jul 3, 2014 at 7:00

3 Answers 3

1

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

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Please see my updated post, I tried what you said but still no output was given.
0

$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!

6 Comments

i tried using that, but still there's no output. Is there a mistake with my query?
Try echoing your query and see if it's printing what you want. May the variable inside the simplequotes are doing something. Take a try.
This is what I got from echoing it SELECT UserID FROM User WHERE username='1234' Is there a mistake in the query?
Query seems to be OK, if the filed UserID is exactly typed like that and the table is User (all beign case sensitive)
I typed them all correctly, what would it mean if var_dump() gets null?
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.