0

I have a small problem using the Where clause for PHP/MySQL in the code shown below:

<?php
$con=mysqli_connect("host","username","password","database");
//Note that I have replaced my parametres just for this question
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Request Session ID
$id = $_SESSION['uid'];
echo "Session ID = ' . $id;
echo "<br>";
//Request Username
echo "Username = " . mysqli_query($con,"SELECT username FROM 'users' WHERE id = 4");

?>

The output of this is (after logging in on my separate login page):

Session ID = 4

Username =

So it is evident that there is an issue with the where clause. There is no issue with the connection parametres as far as I am aware. When I run the MySQL command in PHPmyadmin I get the expected result of 'Admin'. My inputs are correctly named.

I have no idea what is causing this and I can't find any similar problems on the forum. Any help would be appreciated. Thanks.

UPDATE

I have adapted the below answers to make this code:

<?php
// Only run this script if the sendRequest is from my flash application
if ($_POST['sendRequest'] == "parse") {
//conection:
$con=mysqli_connect("mysqlXX.000webhost.com","a4935911_***","***","a4935911_***");
$id = $_SESSION['uid'];
$getuname = mysqli_fetch_assoc(mysqli_query($con, "SELECT username FROM users WHERE id = $id"));
$uname = $getuname ['username'];

// Print 1 var to flash
print "var1=The username of this user is $uname.";

}

?>

Which is triggered by my flash application. This works fine if I do not use session ID variable and just use e.g. 4 as my id value but I need to use the session ID for this. Any ideas what is up with this?

2 Answers 2

2

You shouldn't surround table names with ordinary single quotes ('); use backticks instead (or, if you don't use reserved words, none). So:

SELECT username FROM `users` WHERE id = 4

or

SELECT username FROM users WHERE id = 4

Moreover, mysqli_query doesn't return the result in the field, but a mysqli_result object. You'll have to use a fetch command to get the result(s), e.g.

$result = mysqli_query($con,"SELECT username FROM 'users' WHERE id = 4");
$row = mysqli_fetch_assoc($result);
echo 'Username = ' . $row['username'];
Sign up to request clarification or add additional context in comments.

3 Comments

That has done something. I believe I'm getting an output now but there is an issue as follows: Catchable fatal error: Object of class mysqli_result could not be converted to string in /home/a4935911/public_html/usersOnline.php on line 116
@user2867893 I wasn't finished answering, look at the updated answer.
Alright, I see. That's what I tried when I originally made this script but I got issues with that so I changed to this. I'm not very good at PHP so I don't know what is intended by these: Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in /home/a4935911/public_html/usersOnline.php on line 115 Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /home/a4935911/public_html/usersOnline.php on line 116
0

You should use the following code:-

//conection:
$con=mysqli_connect("host","username","password","database");

if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$getuname = mysqli_fetch_assoc(mysqli_query($con, "SELECT username FROM users WHERE id = 4"));
$username = $getuname ['username'];

 //Request Username
 echo "Username = " .$username ;

3 Comments

YEAH! That's it. I wasn't aware that mysqli_query doesn't actually return the value I need as you and @MarcelKorpel point out. Thank you both for your answers.
I could actually use with a bit of further help on this as shown in my update please.
What you get when you echo session uid?

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.