在PHP PDO中的bindValue中使用$ id之前,我需要使用(int)$ id吗? [英] Do I need to use (int)$id before I use $id in bindValue in Php PDO

查看:82
本文介绍了在PHP PDO中的bindValue中使用$ id之前,我需要使用(int)$ id吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Php数据对象,我不确定的一件事是,在查询中使用变量之前,我是否必须验证该变量是否为整数.例如,像这样:

I just started using Php Data Objects and one thing I'm not sure about is do I have to validate that some variable is an integer before using it in the query. For example, like this:

$id = (int)$_POST['id']; // is this required    

$query = $pdo->prepare("SELECT * FROM `articles` WHERE `id` = ?");

$query->bindValue(1, $id);

$query->execute();

推荐答案

否,由于两个原因,它不是必需的:

No it's not required for two reasons:

  1. 您要让PDO知道要在数据库中查询列ID. PDO不会解析$_POST['id']中的任何内容.

bindValue的第二个值自动转换为字符串(或您可能要选择的任何类型). 此处 int $data_type = PDO::PARAM_STR

The second value of bindValue is automatically casted to a string (or of any type you might want to select). Here int $data_type = PDO::PARAM_STR

这篇关于在PHP PDO中的bindValue中使用$ id之前,我需要使用(int)$ id吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆