简体   繁体   English

PHP,如何检查查询是否成功运行,否则重定向到其他页面而没有显示错误

[英]PHP, how can I check if the query runs successfully else redirect to other page with out showing error

this is the create function as i have added the database class. 这是创建函数,因为我已经添加了数据库类。

id------int 
name----var
fname---var       
phone---number 

i will run here the query which is not valid the (phone) value should be number so i will insert text to have wrong query. 我将在此处运行无效的查询,(电话)值应为数字,因此我将插入文本以进行错误的查询。

public function create(){
$sql = "Insert into tbl_one (name,fname,phone) values('nameN','nameF',error)";
if($database->query($sql)){
 $this->id = $database->insert_id();
} else {
redirect_to('index.php');
}

now here how i can make if the query is false it should redirect me to one page, currently it will show the mysql_error and will give detail why it's wrong but i dont want it i want it just check the query if false redirect me to other page. 现在在这里我该如何使查询为假,它应该将我重定向到一页,当前它将显示mysql_error并提供详细信息为什么它是错误的,但是我不想要它,只要检查查询是否为假即可将我重定向到其他页面页。 do i need another function or that where i should get the hint to move on 我需要其他功能还是在该处获得继续前进的提示?

you have to replace error with 0 您必须将错误替换为0

public function create(){
$sql = "Insert into tbl_one (name,fname,phone) values('nameN','nameF',0)";
if($database->query($sql)){
 $this->id = $database->insert_id();
} else {
redirect_to('index.php');
}

use exception handling 使用异常处理

try {

    $sql = "Insert into tbl_one (name,fname,phone) values('nameN','nameF',error)";
    $database->query($sql);
    $this->id = $database->insert_id();

}catch(Exception $e){
    //log exception...$e

    redirect_to('index.php');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM