0

I'm facing a strange accidental assignment error on the line if ($row = $result->fetch_array(MYSQLI_ASSOC)){ The code's output is always FALSE so I'm unable to query my database. I'm not sure why it is happening. How can I fix it?

Thanks in advance :)

$con = mysqli_connect("localhost:3306", "apple", "apple", "apple");
if (!$con) {
    die("cannot connect: " . mysqli_error());
}
 else {
    if (isset($_GET['id'])){
        $id = $_GET['id'];
        $btnNum = $_GET['btnNum'];
        mysqli_select_db($con, "apple");
        $query = "SELECT * FROM resume where resume_id ='" . $id . "' AND button_match ='" . $btnNum . "'";
        $result = mysqli_query($con, $query) or die('Error, query failed');
        if ($row = $result->fetch_array(MYSQLI_ASSOC)){
            $name = $row['resume_title'];
            $type = $row['file_type'];
            $content = $row['resume_data']; //content of file
            $size = $row['file_size']; //file size
            header('Content-Type:"' . $type . '"');
            header('Content-Disposition: attachment; filename="' . $name . '"');
            echo $content;
        }  
    }
}
Scott
  • 1,863
  • 2
  • 24
  • 43
  • Check the connection & query for errors. – Sougata Bose Aug 13 '15 at 08:15
  • If you connect directly to your database using ``mysql`` and run the same query, what do you see? I.e. run ``mysql -P 3306 -u apple --password="apple" apple <<< "SELECT * FROM resume WHERE resume_id = X AND button_match = 'Y'"`` for appropriate X and Y. – arvidj Aug 13 '15 at 08:15
  • I mean if it returs false then query has to be invalid. Check it. Maybe you have SQL errors – Robert Aug 13 '15 at 08:15
  • is `btnNum` set up as a numeric or a string type in the database? – Scott Aug 13 '15 at 08:18
  • 1
    Also do note, that your code is exploitable by [SQL-injection](https://en.wikipedia.org/wiki/SQL_injection). – arvidj Aug 13 '15 at 08:19
  • I'm getting a " MySQL returned an empty result set" message but my table does contain data – Darrion Rockxmylife Aug 13 '15 at 08:23
  • 1
    your table may contain data but you have also "where" clause which limits data to some conditions. Print this query and execute it directly on DB then you will see if it returns results – Robert Aug 13 '15 at 08:25

1 Answers1

0

Speaking of assignment errors, there are NO assignment errors in this expression.

fetch_array() returns null when there are no [more] results to fetch.

Speaking of your particular problem, check your query, database credentials, typos and such. Then double-check.

Also, unrelated but lot more important than this simple typo: How can I prevent SQL-injection in PHP?
Also, bear in mind mysqli or die, does it have to die?

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345