[英]PHP/MySQL query character limit?
So I've got a relatively long query as follows: 所以我有一个相对较长的查询,如下所示:
SELECT (
(CASE Methanethiol
WHEN -1 THEN 0
ELSE Methanethiol
END)
+
...
+
(CASE nHeptanethiol
WHEN -1 THEN 0
ELSE nHeptanethiol
END)
)
FROM condensates.mercaptans
WHERE (
(CASE Methanethiol
WHEN -1 THEN 0
ELSE Methanethiol
END)
+
...
+
(CASE nHeptanethiol
WHEN -1 THEN 0
ELSE nHeptanethiol
END)
) IS NOT NULL
The problem is that the query works perfectly fine in MySQL admin, but PHP seems to choke on it when I add more then 4 columns and gives me a NULL result. 问题是查询在MySQL admin中工作得很好,但是当我添加多于4列并给我一个NULL结果时,PHP似乎感到窒息。 Any tips? 有小费吗? Also, am I missing some easy way to simply set the NOT NULL condition for the entire SELECT parameter rather than copying it out again? 另外,我是否缺少一些简单的方法来简单地为整个SELECT参数设置NOT NULL条件,而不是再次将其复制出来?
EDIT: As requested, the PHP that calls this query is as follows... 编辑:根据要求,调用此查询的PHP如下...
First function call is: 第一个函数调用是:
$mr = avg(query($property, 'mercaptans', $dates['mostRec']), $property); $ mr = avg(query($ property,'mercaptans',$ dates ['mostRec']),$ property);
Where query and avg are defined as: 其中query和avg定义为:
function avg($query, $colName){
$iter=0;
$sum=0;
while($row = mysql_fetch_array($query)) {
if($row[0]!==NULL){
$iter++;
$sum += ($row[$colName]==-1) ? 0 : $row[$colName];
}
}
mysql_free_result($query);
if ($iter==0)
return '-';
else {
$avg = ($sum / $iter);
if(lessThanMDL($avg, $colName))
return 'ND';
else
return $avg;
}
}
function query($selectWhat, $fromTable, $sampleIDs,$orderBySampIDAsc='false'){
$query = "SELECT ";
$query .= mysql_real_escape_string($selectWhat);
$query .= " FROM ";
$query .= mysql_real_escape_string($fromTable);
if(count($sampleIDs) >= 1) {
$query .= " WHERE (";
$iter=0;
while($iter < count($sampleIDs)-1){
$query .= "(SampleID=" . >mysql_real_escape_string($sampleIDs[$iter]) . ") OR ";
$iter++;
}
$query .= "(SampleID=" . >mysql_real_escape_string($sampleIDs[$iter]) . "))";
$query .= " AND " . mysql_real_escape_string($selectWhat) . " IS NOT NULL";
} else {
$query .= " WHERE SampleID=0"; # always returns nothing
}
if($orderBySampIDAsc=='true')
$query .= " ORDER BY SampleID ASC";
global $condensatesdb;
return mysql_query($query, $condensatesdb);
}
Sorry it's so spaced out - I can't seem to get it formatted otherwise. 抱歉,它是如此隔开-否则我似乎无法格式化它。 Anyway, this code works in a probably close to 30 other queries on the page, but fails just for this one. 无论如何,此代码可以在页面上的近30个其他查询中使用,但仅此一项而失败。
Oh my, that code is a mess. 哦,我的代码太乱了。 Nothing that can't be fixed. 没有什么不能解决的。
$query = "SELECT ";
$query .= mysql_real_escape_string($selectWhat);
This will fail if you have two things in the string, like foo, bar
, and will utterly obliterate your parenthesized clauses, if you're passing those in. 如果字符串中有两件事,例如foo, bar
,这将失败,并且如果将它们传入,则将完全消除括号中的子句。
$query .= " FROM ";
$query .= mysql_real_escape_string($fromTable);
Tables in the FROM clause use different quoting rules than normal data. FROM子句中的表使用的引用规则与普通数据不同。 Don't run it through string escaping. 不要通过字符串转义来运行它。
You probably want to consider switching to a database access layer like PDO that provides prepared statements & placeholders . 您可能想考虑切换到提供预 准备语句和占位符的数据库访问层( 例如PDO) 。
if($orderBySampIDAsc=='true')
PHP has real booleans. PHP具有真正的布尔值。 You should not be attempting to represent a boolean state as a string. 您不应该尝试将布尔状态表示为字符串。
Now, all this being said, I don't see how the query
function you've written can create the SQL that you've provided at the top of your question! 现在,所有这些, 我看不到您编写的query
功能如何创建问题顶部提供的SQL! Can you throw in a print $query
at the bottom of the function, prior to the call to mysql_query
, and see what's actually being generated? 您可以在调用mysql_query
之前在函数底部添加一个print $query
,并查看实际生成的内容吗? Then you can copy & paste it into phpmyadmin and see how sane/insane it really ends up being. 然后,您可以将其复制并粘贴到phpmyadmin中,并查看其真正的理智与理智。
You can rewrite your above query like this using the HAVING keyword: 您可以使用HAVING关键字像这样重写上面的查询:
SELECT
(CASE Methanethiol
WHEN -1 THEN 0
ELSE Methanethiol
END)
+
...
+
(CASE nHeptanethiol
WHEN -1 THEN 0
ELSE nHeptanethiol
END)
AS matches
FROM condensates.mercaptans
HAVING matches > 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.