检查wordpress查询是否成功


Check if wordpress query was successfull or not

我正在执行一个查询,如下所示。

global $wpdb;
$table_name = $wpdb->prefix . 'prousers';
$results = $wpdb->get_results("INSERT into $table_name 
                          (name,gender,mailid,empid,address,phone)
                           VALUES(
                           '".$InputName."','".$gender."','".$InputEmail."',
                           '".$emp_id."','".$address."','".$phn_num."'
                           )", OBJECT );

一切都很顺利。我只是想验证它,所以我做了以下工作:

if($results) { 
    echo'success';
} else {
    echo "error";
}

值已正确插入到表中,但它显示失败消息(它打印"错误"(。

这是为什么呢?

我建议你使用$wpdb->insert($table,$data,$format(;

然后,您将获得$wpdb->insert_id写入条件的详细信息。

您需要

$wpdb->insert();而不是$wpdb->get_result();为:

$wpdb->insert( "INSERT into $table_name (name,gender,mailid,empid,address,phone) VALUES('".$InputName."','".$gender."','".$InputEmail."', '".$emp_id."','".$address."','".$phn_num."')", OBJECT );

在此之后,使用它来获取最后一个 ID:

$lastid = $wpdb->insert_id;

并检查查询是否执行为:

if($lastid > 0) { 
    echo'success'; 
} 
else { 
    echo "error"; 
}

你可以用 get_results(( 方法使用 die 函数,

 global $wpdb;
        $table_name = $wpdb->prefix . 'prousers';
                $results = $wpdb->get_results( "INSERT into $table_name 
                          (name,gender,mailid,empid,address,phone)
                          VALUES('".$InputName."','".$gender."','".$InputEmail."',
                        '".$emp_id."','".$address."','".$phn_num."')", OBJECT ) or die('Error while query run');