4

I am trying to create a simple echo from a SELECT from DB.

It echos blank always.

Here is the code:

<?php
$username = "xxxx";
$password = "xxxx";
$hostname = "xxxx";

$conn = mysqli_connect($hostname, $username, $password, "xxxx");

if(! $conn ) {
    die('Could not connect: ' . mysqli_error());
}

$user = $_GET['user'];
$pass = $_GET['pass'];

$sql = "SELECT id FROM 'users' WHERE user='$user' AND pass='$pass'";
$retval = mysqli_query($conn, $sql);



echo($retval);


mysqli_close($conn);
?>

It would be greatly appreciated if someone could help and tell me what I am doing incorrectly :)

1
  • 1
    Keep in mind that passing variables like this will lead to SQL injection Commented Dec 31, 2017 at 20:40

4 Answers 4

2

If it worked, you didn't get ID back, but a "mysqli_result" object.

Try print_r($retval) to see what you really got.

If it's a mysqli_result object, you need another step to get the data.

while ($row = $retval->fetch_assoc()) {
    echo $row['id'];
}
Sign up to request clarification or add additional context in comments.

Comments

2

According to the manual:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries, mysqli_query() will return TRUE.

Moreover, please note that a query that runs but returns zero results is still considered a "successful query" since the query did run in the database and an empty result set is a legitimate response. This means the query will still return a mysqli_result object.

As I understood the code so far, if you want to echo results returned you could use mysqli_fetch_array(), so your code would become:

/* numeric array */
$row = mysqli_fetch_array($retval, MYSQLI_NUM);

And you can echo it as:

echo $row[0];

or

/* associative array */
$row = mysqli_fetch_array($retval, MYSQLI_ASSOC);

And then you could echo as:

echo $row['id'];

Furthermore, you can use mysqli_fetch_assoc() to loop over your results or for fetching single row instance purposes, an example is:

while ($row = $result->fetch_assoc()) {
    echo $row['id'];
}

Returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set's columns or NULL if there are no more rows in the resultset.

And if you want to check if your query has found any rows, you can use mysqli_num_rows

Which works as the following:

/* determine number of rows result set */
$row_cnt = mysqli_num_rows($retval);

Comments

1

Do you know if you are connected to the database or not? You could try changing a value to see if the error comes up or not.

If thats not the issue this should work better:

$host = 'xxxx';
$user = 'xxxx';
$pass = 'xxxx';
$db = 'xxxx';
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);

$sql = "SELECT id FROM 'users' WHERE user='$user' AND pass='$pass'";
$result = $mysqli->query($sql);

while($row = $result->fetch_assoc()) {
   $whateverString = $row['cakes'];
}
echo $whateverString;

Now lets say that you had a value in the mysql row called cakes that is equals to 5 then it will fetch that specific string from the row if the user and pass is correct.

So the value above will output "5" if the mysql value 'cakes' is 5.

Comments

0

Thanks for your answers, people! I ended up with this:

    $q=mysqli_query($con,"SELECT id FROM `users` WHERE user='$username' and pass='$password'");

while ($row=mysqli_fetch_object($q)){
 $data[]=$row;
}

if(json_encode($data)== '[]'){
echo 'error';
}else {
echo json_encode($data);
}
?>

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.