Monday, January 17, 2022

[FIXED] MYSQL select query does not give any output

Issue

My SQL query is not working. Below is my code and note that in that table query worked fine and gives output. But in PHP by using mysqli_num_rows(), mysqli_fetch_assoc() and mysqli_fetch_array() all doesn't works for me. My DB connection is :

$conn = mysqli_connect($servername, $username, $password, $db);

Note : My DB Connectivity is fine.

$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
$query = mysqli_query($conn, "SELECT * FROM `admin` WHERE `username`='$username'");

This query results true in PHPMyAdmin and returns false in PHP with the above functions. Can anyone answer is I made a mistake?

And I am tried that query to execute in following methods :

$row = mysqli_fetch_assoc(); // Results No Data
$data = mysqli_fetch_array($query); // Results No Data
$num = mysqli_num_rows($query); // Results 0 Data

Solution

you may execute the query before use it :

<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$conn = mysqli_connect("localhost", "my_user", "my_password", "world");

$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC";

$result = mysqli_query($conn , $query);

/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
  printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}


Answered By - William Langlois

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.