Hallo,
weiß vielleicht jemand, wie ich unter Benutzung der $wpdb-Class einen möglichen SQL-Fehler abfangen kann?
Nehmen wir an, ich benutze folgende Funktion:
PHP-Code:
function block_ip($ip) {
global $wpdb;
$query = "INSERT IGNORE INTO wp_blocked_ips (ip) VALUES ('$ip')";
return $wpdb->get_results($query);
}
Die rufe ich dann so auf:
PHP-Code:
$blocked = block_ip('123.456.789.012');
if ($blocked) {
echo "Everything went fine.";
} else {
echo "An error has occured!";
}
Das Problem ist nur, dass
$blocked immer TRUE ist, auch wenn ich die Abfrage oben so ändere, dass ein Fehler herauskommen muss.
Ich habe auch bereits andere Implementationen gesehen, wie z.B.
PHP-Code:
if (count($items) > 0) { ... }
oder
PHP-Code:
if (!(empty($results))) { ... }
wobei
$items bzw.
$results das query-Ergebnis darstellen.
Irgendwie wird da aber nur überprüft, ob überhaupt ein Ergebnis vorliegt, nicht, ob ein Fehler aufgetreten ist.
Hier möchte ich jedoch einen Unterschied machen, weil ich den User darauf hinweisen will, ob nur kein Ergebnis vorlag oder aber ein Fehler (so dass er nachschauen kann, ob die Tabelle überhaupt existiert etc.).
Weiß jemand, wie das mit der $wpbd-Class funktioniert?
Die
Function Reference sagt dazu:
Zitat:
|
If there are any query results, the function will return an integer corresponding to the number of rows affected and the query results will cached for use by other wpdb functions. If there are no results, the function will return (int) 0. If there is a MySQL error, the function will return FALSE. (Note: since both 0 and FALSE can be returned, make sure you use the correct comparison operator: equality == vs. identicality ===).
|
Was mache ich bei der Abfrage falsch?
Vielen Dank im Voraus!