0

I'm trying to get data from a custom table in a wordpress database, so I can show different stuff depending on the product visited (I use a variable for that) it's a shop.

So far I have:

$sku = $wpdb->esc_like($product->get_sku())."%";
$sql = $wpdb->prepare('SELECT * FROM `table` WHERE `sku` LIKE "'.$sku.'"');
$result = $wpdb->get_results($sql);
var_dump($result);

And I get:

array(0) {
}

If I put a test sku instead of the variable, it returns results fine:

$sql = "SELECT * FROM `table` WHERE `sku` LIKE '%DCAT19%' LIMIT 1";
$result = $wpdb->get_results($sql);
echo $sql;
var_dump($result); 

I get:

SELECT * FROM `table` WHERE `sku` LIKE '%DCAT19%' LIMIT 1 

array(1) {
  [0]=>
  object(stdClass)#7675 (24) {
    ["id"]=>
    string(1) "1"
    ["sku"]=>
    string(17) "DCAT19MDIXHUEFL-L"
    ["fieldcount"]=>
    string(1) "4"
...etc

But I need the variable to work, so it can change depending on the product visited.

NOW, if I echo the result from $sql, when using a variable, that is:

$sql = $wpdb->prepare('SELECT * FROM `table` WHERE `sku` LIKE "'.$sku.'"');

It gives a looong string instead of % (!!!):

SELECT * FROM `table` WHERE `sku` LIKE "DCSS20MBJOF01HB{0ff8bf1af1220a9d0556b6de44274e60e96a206d5cac40b1b55605b27fe76b4a}"

I have no idea what happens!! I think that's breaking the query, maybe? No idea what to do next.

I've also tried (with variable):

  • Double escape the %
  • Quote the table and column names
  • Calling the variable with %s
  • Using sprintf

SAME RESULTS... array in zero and/or looong string instead of a %.

So, if someone could please let me know, what I'm doing wrong? Thanks a lot in advance.

4
  • 1
    That's not how wpdb->prepare works, the whole point of prepare is to avoid joining strings and variables together. If you look at your PHP error log you should be seeing warnings that prepare wasn't given enough arguments. If you use prepare correctly, you should not need esc_like
    – Tom J Nowell
    Commented Jul 14, 2021 at 19:43
  • I've also tried: $wpdb->prepare("SELECT * FROM table WHERE sku LIKE %s", $sku) and result is still array (0), would you please elaborate which arguments I'm missing? Thanks
    – compil3r
    Commented Jul 14, 2021 at 23:50
  • "It gives a looong string instead of %" - that long string is normal and it's called placeholder escape string, and it will be changed back to % when wpdb sends the parsed query to MySQL. So now that you've corrected the prepare() syntax, with that DCSS20MBJOF01HB being the $sku value, does SELECT * FROM `table` WHERE `sku` LIKE 'DCSS20MBJOF01HB%' LIMIT 1 actually return any rows? And by that, I'm saying that if you get an empty array, then it's probably because there really are no rows matching the value of $sku.
    – Sally CJ
    Commented Jul 15, 2021 at 9:47
  • Thank you guys. It turns out the prepare syntax was wrong, and the data on the table was incomplete, due to a truncated csv import. Fixed both things, and now is working, thanks again!
    – compil3r
    Commented Jul 16, 2021 at 1:17

1 Answer 1

0

Turns out prepare had bad syntax, and my table was incomplete due to a bad csv import. So, lesson learned, check the manual for syntax, and check (double check!) your tables. Thanks to Tom and Sally for their comments, they helped me to re-think what was I doing wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.