Digikid13
Digikid13

Reputation: 68

Find a specific value in a database php

I have tried different methods and none of them seem to work. When I try the command in the MySQL console it works fine, but when I try it in this PHP script it sends me back an empty string.

$search = $mysqli->prepare("SELECT item_path FROM items WHERE item_title = ?");

while (array_key_exists($i, $parts)) {
    // An array that contains (word1, word2, word3)
    $data = $parts[$i];

    // Wraps data in "" for mysqli query
    $data = "\"" . $data . "\"";

    // Binding here still doesn't work
    $search->bind_param("s", $data);

    // This should execute the above prepare and give me the data
    // from the way I think it work's this execute() should use the updated $data
    $search->execute();
    $search->bind_result($img_path);
    $search->fetch();

    // This returns an empty string
    echo $img_path;
    echo "<img src= \"", $img_path, "\" >";

    $i++;
}

If I go into the MySQL console and run:

SELECT item_path FROM items WHERE item_title = "word1"

I get the item_path I am expecting but for some reason it my script it returns an empty string.

Thanks in advance for helping (if you do)!

EDIT:

    // Wraps data in "" for mysqli query
    $data2 = "\"" . $data . "\"";

    // Binding here still doesn't work even after changing the var
    $search->bind_param("s", $data2);

I tired to change the bind to another variable, but still I get an empty string. I'm not sure if it's cause I'm wraping the data incorrectly or if there is another reason. I have been searching for something else like this to happen and I have came up with nothing.

Upvotes: 0

Views: 406

Answers (1)

h2ooooooo
h2ooooooo

Reputation: 39542

You bind your statement outside of the clause, and you never use the $data that you changed.

$search = $mysqli->prepare("SELECT item_path FROM items WHERE item_title = ?");

while (array_key_exists($i, $parts)) {
    // An array that contains (word1, word2, word3)
    $data = $parts[$i];

    // Wraps data in "" for mysqli query
    $data = "\"" . $data . "\"";

    // Bind it here instead!
    $search->bind_param("s", $data);

    // This should execute the above prepare and give me the data
    $search->execute();
    $search->bind_result($img_path);
    $search->fetch();

    // This returns an empty string
    echo $img_path;
    echo "<img src= \"", $img_path, "\" >";

    $i++;
}

Upvotes: 1

Related Questions