PHP-MySQL中SELECT的参数值编码 [英] Coding of parameter-value for SELECT in PHP-MySQL

查看:66
本文介绍了PHP-MySQL中SELECT的参数值编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的 Alt A 是来自 php-mysql 教程的声明.它可以正常工作.
我发现 id 值相当模糊并测试了 alt B.这也有效!
alt A 的 id 值有什么意义?

Alt A below is a statement from a php-mysql tutorial. It works as it should.
I found the id-value rather obfuscated and tested alt B. This also worked!
What is the point with the id-value of alt A?

MySQL 5.0.51,PHP 5.2.6

MySQL 5.0.51, PHP 5.2.6

// Alt A :  
$sql = "SELECT * FROM example WHERE id = '".$q."'";  
// Alt B :  
$sql = "SELECT * FROM example WHERE id = $q";  

推荐答案

使用Alt A"中的示例有两个原因.首先,如果字符串用单引号 '' 括起来,则字符串中将使用变量的名称而不是它的值.

Theres two reasons to use the example in 'Alt A'. First is if the string is enclosed in single quotes '', the variable's name will be used in the string instead of it's value.

$id = 7;
'SELECT * FROM table WHERE id = $id' //works out to: WHERE id = $id
"SELECT * FROM table WHERE id = $id" //works out to: WHERE id = 7

其次,将字符串与函数调用的结果结合起来很有用.

Secondly, it's useful to combine strings with the results of a function call.

"SELECT * FROM table WHERE id = '".getPrimaryId()."'"

这篇关于PHP-MySQL中SELECT的参数值编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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