skip to Main Content

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

2

Answers


  1. 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"]);
    }
    
    Login or Signup to reply.
  2. You mast to run mysqli_query function for receive result from DB:

    $username = mysqli_real_escape_string($conn, $_POST['username']);
    
    $query = "SELECT * FROM `admin` WHERE `username`='$username'";
    
    $result = mysqli_query($mysqli, $query);
    
    $row = mysqli_fetch_assoc($result);
    
    print_r($row);
    

    PHP MySQL sandbox here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search