nickyb nickyb - 1 month ago 7
PHP Question

WPDB get_row like string returning null?

I'm having a problem comparing $url and the url in the database. For some reason this always returns null, but if I drop the url it works fine. I also copied the url directly out of the database so it's an exact match.

Any suggestions on how to query the url string for a match?

$row = $wpdb->get_row( "SELECT * FROM my_table WHERE service = $package_id AND url LIKE $url", ARRAY_A );

Answer

I believe the problem is with $url, in that with the LIKE operator you have to surround the value with % signs to make it match somewhere in the string. So try this instead:

$row = $wpdb->get_row( "SELECT * FROM my_table WHERE service = $package_id AND url LIKE '%$url%'", ARRAY_A );

Comments