PHP 中的基本 SQL Select 语句格式
我有一个可搜索的众议院和参议院数据库,我只想制作一个可以搜索该数据库的简单网页。唯一的问题是,虽然我可以轻松编写 SQL select 语句,但如何正确格式化它们以便在 PHP 中使用?
例如,这是我按州选择参议员的单选按钮:
$sql = "";
if ($_POST['pkChamber'] == "Senate") {
if ($_POST['pkParty'] == "Y") {
$sql = SELECT * FROM senateinfo
WHERE state = (Variable = "stname")
ORDER BY last_name, first_name');
}
else
{
$sql = SELECT * FROM senateinfo
WHERE state = (Variable = "stname")
ORDER BY last_name, first_name
}
}
I have a searchable database of the House and Senate and I just want to make a simple web page that can search this database. The only problem is, while I'm comfortable writing SQL select statements, how do I properly format them for use in PHP?
For example, here's my radio button to select Senators by state:
$sql = "";
if ($_POST['pkChamber'] == "Senate") {
if ($_POST['pkParty'] == "Y") {
$sql = SELECT * FROM senateinfo
WHERE state = (Variable = "stname")
ORDER BY last_name, first_name');
}
else
{
$sql = SELECT * FROM senateinfo
WHERE state = (Variable = "stname")
ORDER BY last_name, first_name
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定你在要求什么,但我有一个动态构建 WHERE 语句的可靠且安全的方法的好例子:
希望这会有所帮助。
I am not sure what you're asking for, But I have a good example of reliable and safe way for building WHERE statement dynamically:
Hope this helps.
你的查询看起来不错。我认为您只需要了解 PHP 中字符串解析的一些细节即可。
当您使用双引号 (") 括住字符串时,PHP 实际上会尝试解析它,查找变量和/或其他要首先处理的 php 代码。类似这样:
PHP 会用 $state 替换该变量中定义的内容对于在该类中实例化的任何用户的 id 也是如此(此外,您不必将简单变量包装在 {} 中。它确实有助于提高可读性,但仅对类方法/变量是必需的。)
如果您使用单引号 (') 来括起一个字符串,PHP 只是将其视为正常的。 对于上面的查询,我建议将其括在单引号中,如下所示:
如果稍后要在此查询中使用变量,那么您将需要。 这样,当您所做
的只是粘贴查询时,PHP 就不会错误地认为您试图错误地连接字符串。
Your query seems fine. I think you just need to understand some of the finer points of string parsing in PHP.
When you use double quotations (") to enclose a string, PHP actually will try to parse it looking for variables and/or other php code to process first. Something like this:
PHP will substitute out $state for whatever is defined in that variable and the same for the id of whatever user is instantiated in that class. (Also, you don't have to wrap your simple variables in {}. It does help with readability but is only required for class methods/variables.)
If you use single quotes (') to enclose a string, PHP simply treats it like normal. For your above query, I would suggest enclosing it in single quotes like this:
If you want to use variables later on in this query, then you will need to escape the double quotations that are in there like this:
This way, PHP doesn't error out thinking you were trying to concatenate strings incorrectly when all you were doing was pasting a query.
您需要一次专注于一个问题。
尽量避免在 PHP 中编写 SQL,直到您清楚地掌握 PHP 中的字符串以及如何将变量注入到这些字符串中。所以:
阅读 PHP 中的字符串引用(双引号与单引号,是的,HEREDOC)
阅读 PHP 中字符串中的变量(请注意,如果它没有 $ 美元符号,则它是一个常量,而不是一个字符串变量。从应该使用它们的 $strings 和 $variables 开始,而不是 CONSTANT,只有在没有其他可用的情况下,它们才会转为字符串。)
阅读 PHP 中的绑定 SQL。其他任何事情都会引导您走上SQL 注入之路。如果您的 PHP SQL 中仅使用裸字符串,那么当您最终将 Web 脚本部署到严酷无情的 Internet 时,您就会面临失败。它充满了随时准备利用 SQL 注入脚本的鲨鱼。
这是我每天用来绑定 SQL 的代码示例,以自定义函数为中心,使其变得简单:
我可以为您提供一个用于创建绑定 SQL 的函数,就像稍后那样(当我实际上在计算机而不是移动设备上时)如果你有兴趣的话。
You need to focus on one issue at a time.
Try to avoid writing SQL in PHP until you've a clear handle on strings in PHP, and how to inject variables into those strings. So:
Read up on string quoting in PHP (double quotes vs. Single quotes, and yes, HEREDOC)
Read up on variables in strings in PHP (note that if it doesn't have a $ dollar sign, it's a CONSTANT, not a string variable. Start off right with $strings and $variables where they're supposed to be used, not CONSTANTs, which only fall back to turn into strings if nothing else is available.)
Read up on binding SQL in PHP. Anything else will lead you down the path of SQL injection. If there are only naked strings used in your PHP SQL, then you are setting yourself up for failure when you finally deploy your web scripts to the harsh and unforgiving Internet. It's full of sharks ready to take advantage of SQL injection prone scripts.
Here is an example of code I use daily to bind SQL, centered around a custom function that makes it easy:
I can get you a function for creating bound SQL simply like that later (when I'm actually at a computer instead of mobile) if you're interested.
我使用 HEREDOC 来编写重要的查询:
Heredocs 的功能就像您执行了常规双引号一样字符串,但好处是没有转义内部引号。变量插值按预期工作,您也可以对文本进行缩进,因此您的查询看起来格式很好
I use HEREDOCs for writing out non-trivial queries:
Heredocs function as if you'd done a regular double-quoted string, but with the bonus of not having escape internal quotes. Variable interpolation works as expected, and you can do indentation on the text as well, so your query looks nicely formatted
我总是这样做,以保持它看起来漂亮。
I always do mine like this to keep it looking nice.