0

I am a beginner in PHP. I am trying to run the following code in my php file but it is fetching me no result although data is present with same entry in my SQL table.

<?php
echo "this is the start";
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "PW3";
$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    echo "Unable to connect to databse";

    die("Connection failed: " . mysqli_connect_error());
}
else {
    echo "Connected";
    if(!empty($_POST["username"]) && !empty($_POST["password"])){

        $username=$_POST['username']; 
        //echo "$username";
        $sql="SELECT fullname FROM `users` WHERE username=ashish";
        $result1=mysqli_query($conn,$sql);
        print_r($result1);
        // Mysql_num_row is counting table row
        $count=mysql_num_rows($result1);
        echo "$count";
    }
}
echo "<br>this is the end";

?>
halfer
  • 19,824
  • 17
  • 99
  • 186
CodeHunter
  • 2,017
  • 2
  • 21
  • 47
  • 1
    Change WHERE username=ashish" to WHERE username='ashish'" and see if that helps. Otherwise post the error message being returned. –  Feb 17 '17 at 23:29
  • @jeff: I get result for $result1 but still no result for `echo "$count"`. Also, if I want to give like this: `$sql="SELECT fullname FROM users WHERE username=$username";` it again won't fetch any result here. – CodeHunter Feb 17 '17 at 23:32
  • @ashishkumar you mean that you get nothing after that `print_r`? Or maybe you get "$count" or 0 or something? – Albert221 Feb 17 '17 at 23:33
  • @Albert221: it doesnt display the result at all. neither 0 nor anything else. – CodeHunter Feb 17 '17 at 23:34
  • I don't understand. Do you receive any output at all? Do you receive output of `print_r` or don't you get anything? – Albert221 Feb 17 '17 at 23:35
  • Using '$username' works. Thanks for the suggestion! – CodeHunter Feb 17 '17 at 23:36
  • @Albert221: I do gt the output now using '$username' as below: `mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 0 [type] => 0 )` – CodeHunter Feb 17 '17 at 23:36

1 Answers1

1

You're not quoting "ashish", so the database will try to parse it as a keyword and fail. You should be able to see that using mysql_error() or whatever it's called in PHP :-)

vegivamp
  • 26
  • 2