简体   繁体   English

将MySQL查询分为$ query和$ result有什么价值?

[英]Any value of separating a MySQL query into $query and $result?

I always see MySQL queries structured like this, even with the simplest of queries: 我总是看到这样的MySQL查询结构,即使使用最简单的查询也是如此:

$query = "SELECT * FROM table_name WHERE id = '1'";
$result = mysql_query($query);

Is there any value, time or otherwise in doing the above over a simple one line query such as? 通过简单的单行查询进行上述操作是否有任何价值,时间或其他方式?

$result = mysql_query("SELECT * FROM table_name WHERE id = '1'";

I've always used the former, as when I started using PHP I thought it best to simply copy everyone else, however now that I'm working on a fairly large (relative to my other work) application, I could cut out a significant amount of arbitrary coding if there's really no difference. 我一直使用前者,因为当我开始使用PHP时,我认为最好是简单地复制其他所有人,但是由于我正在开发一个相当大的(相对于我的其他工作)应用程序,因此我可以削减大量数量,如果真的没有区别的话。

对于第一种方法,如果发生错误,更容易记录SQL(并且更容易发现错字,例如缺少括号)。

Generally, I like separating them because I can easily do print $query; 通常,我喜欢将它们分开,因为我可以轻松地print $query; when I'm trying to figure out why my query won't work. 当我试图弄清楚为什么我的查询不起作用时。 It's mostly a matter of personal preference. 这主要是个人喜好问题。

Actually there is no difference at all except for one thing. 实际上,除了一件事之外,没有任何区别。 If you put your query in seperate variable then you can reuse that. 如果将查询放在单独的变量中,则可以重复使用该变量。 If any changes occur in query you just need to edit the query in variable 如果查询中发生任何更改,您只需要在变量中编辑查询

您始终可以创建一个自定义函数来接受查询并运行它,并传递其他可选参数,以防您想返回查询或记录查询以跟踪任何问题等。

Is there any value, time or otherwise in doing the above over a simple one line query such as? 通过简单的单行查询进行上述操作是否有任何价值,时间或其他方式?

sure, as MSpreij said, you can examine your query in case of error. 当然,正如MSpreij所说,如果出现错误,您可以检查查询。

I always see MySQL queries structured like this, even with the simplest of queries: 我总是看到这样的MySQL查询结构,即使使用最简单的查询也是如此:

$query = "SELECT * FROM table_name WHERE id = '1'";
$result = mysql_query($query) or trigger_error(mysql_error()." ".$query);

this simple construct will save you a dozen whines in stackoverflow "what does it mean mysql_fetch_array(): supplied argument is not a valid MySQL result " 这个简单的构造将为您节省大量的stackoverflow“这意味着mysql_fetch_array(): supplied argument is not a valid MySQL result

notice the additional part. 注意其他部分。 This is called programming . 这称为编程
It will print your query automatically , in case of error, along with mysql error message. 如果出现错误,它将自动打印查询以及mysql错误消息。 Or log it into error log in a live server with appropriate settings. 或使用适当的设置将其记录到活动服务器中的错误日志中。

Without writing print $query; 不写print $query; manually. 手动。

However, both methods are ugly as hell. 但是,这两种方法都很丑陋。

One should never use raw API functions at all. 一个人绝对不要使用原始API函数。

It's ugly, unmaintainable, bloated and repetitive. 这是丑陋的,难以维护的,肿的和重复的。

One should develop a library , a function to make a call more intelligent and less repetitive 应该开发一个库 ,一种使通话更智能,减少重复的功能

$data = mydb_get_row("SELECT * FROM table_name WHERE id = 1");

having all the processing inside. 内部有所有处理。 with error handling, query logging, parameter checking and api functions calling. 错误处理,查询日志记录,参数检查和api函数调用。
yet as a true one-liner, not a silly one you were trying to achieve. 但是,作为一个真正的单一公司,而不是您想要实现的愚蠢的公司。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM