php 数据库查询返回码,Php Mysqli查询不返回任何值或错误[重复]

在您开发这个系统时,应该尽早而不是晚些采用最佳实践—在本例中,我指的是

sql injection

唉,上面的代码很脆弱。我的猜测是嵌入变量周围缺少引号-

code_ticket = $order

~如果

$order

是字符串,则需要引号。也就是说,很容易给这个注射上肮脏的东西,所以

prepared statement

会是前进的方向。我很快重写了你的代码,告诉你如何使用

try/catch

布洛克和

准备好的声明

希望能解决问题,使代码更安全。

if( $link && $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST["order"] ) ){

try{

$order = $_POST['order'];

/* basic query with placeholder for variable */

$sql = 'select `shipping_status` from `orders` where `code_ticket` = ?';

/* create the prepared statement object */

$stmt = $link->prepare( $sql );

/* if the query failed raise an exception to indicate failure */

if( !$stmt ) throw new Exception( 'Failed to prepare sql' );

/* so far so good. Bind placeholder to a variable */

$stmt->bind_param( 's', $order );

/* execute the query */

$result = $stmt->execute();

/* deal with recordset */

if( !$result ) throw new Exception( 'No results: Order not placed' );

else {

/* bind column data to an output variable */

$stmt->bind_result( $status );

/* fetch the records */

$stmt->fetch();

/* do something with output variable */

printf( 'Shipping Status: %s', $status );

$stmt->free_result();

$stmt->close();

}

}catch( Exception $e ){

exit( $e->getMessage() );

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值