PHP sql 查询语法 [英] PHP sql query syntax

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

问题描述

注意到 sql 查询语法中的一个小问题,它是如何进行的:

Noticed a small issue in the syntax of a sql query, here's how it goes:

$email = "name_lastname@server.com";

$query  = "Select * From Users Where email=".$email;

这不起作用,查询已经过测试并且工作正常,但是这基本上演变为:

This does not work, the query has been tested and works fine, however this essentially evolves to :

Select * FROM Users WHERE email=name_lastname@server.com ;

产生空结果.

为了以正确的方式执行它,我在 $email 变量的语法中添加了一个扭曲,本质上是:

To execute it the right way, I add a twist to the syntax of my $email variable, essentially as:

$email = "\"name_lastname@server.com\"";

一旦我在字符串变量中指定了引号,即当它按预期执行并产生所需结果时.

Once I specify quotations within the string variable, that is when it executes as expected yielding the desired result.

我不确定这是否是接近我的查询执行语法的最美观的方式,我确实认为有替代方案.感谢那些对此有所了解的人

I am not sure if this is the most aesthetic way to go about approaching my syntax for query execution, and I do think there are alternatives. Grateful to those who shed a light on this

推荐答案

试试这个:

$query  = "Select * From Users Where email='$email'";

或者:

$query  = sprintf("Select * From Users Where email='%s'", $email);

或者:

许多其他方式....

这篇关于PHP sql 查询语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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