Bango Skank
Bango Skank

Reputation: 73

Unable to make this prepared select statement work

I'm having some real problems with this prepared statement and I have no idea why. I wonder if anyone could help me out. I know that I am sending the right information, and I know that the information exists where I am looking for it, but the prepared statement simply returns nothing at all.

I am calling the PHP file like this

$.ajax({
            type: "POST",
            url: "include/main.php",
            data: {func:'LoadThisFile',thefile:NewItemFilename},
            success: function(server_response)
            {
                if(server_response)
                {
                    Doing stuff....
                }
                return;
            }
        });

and this is the prepared statement

$stmt = $mysqli->prepare("SELECT `FileData` FROM `DataTable` WHERE `Filename`=?");
$stmt->bind_param("s", $tTheFilename);
$tTheFilename = $ThisFileName;
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
echo $result;
$stmt->close();
$mysqli->close();

I have checked and double checked:
The variable NewItemFilename that is sent to the prepared statement contains exactly what I expect it to contain.

The database and the table both exist, as does the data I am trying to return, in fact if I use exactly the same statement inside phpMyAdmin, along with the filename I am trying to return, I get exactly the result I want from the prepared statement.

By using the echo command I can tell that the database connection is being made, and that there appear to be no errors thrown up within the statement itself. Everything seems to work just find except that nothing is being returned.

Can anyone tell me what I am doing wrong?

many thanks, BS

Upvotes: 0

Views: 134

Answers (2)

KDS
KDS

Reputation: 137

Execution of results has to be stored.

Try $stmt->store_result(); before $stmt->fetch();

Upvotes: 1

koral
koral

Reputation: 2945

You didn't show how do you initialize $tTheFilename. Assumin it's content is proper you should bind params with

$stmt->bindParams(1, $tTheFilename);

http://php.net/manual/de/pdostatement.bindparam.php

Upvotes: 0

Related Questions