更改 mysql 查询后出现 500 内部服务器错误
昨天,我更改了数据库架构,并因此更改了应用程序中的数据库查询,从那时起,我一直收到 500 个内部服务器错误。我不认为问题在于查询,因为我可以从我的模型运行它,并从我的控制器打印数组。问题似乎出在构建模板时。
下面是我的代码,
MODEL
public function searchCandidates($type=null, $gender=null, $age=null)
{
if($age != "0") {
$age = implode(", %",$age);
}
$sql = 'SELECT `candidates`.`candidate_id`,
`candidates`.`first_name`,
`candidates`.`surname`,
`candidates`.`DOB`,
`candidates`.`gender`,
`candidates`.`talent`,
`candidates`.`location`,
`candidates`.`availability`,
`candidates`.`youtube_showreel_1`,
`candidates`.`youtube_showreel_2`,
`candidates`.`youtube_showreel_3`,
`candidates`.`youtube_showreel_4`,
`candidates`.`youtube_showreel_5`,
`candidates`.`youtube_showreel_desc_1`,
`candidates`.`youtube_showreel_desc_2`,
`candidates`.`youtube_showreel_desc_3`,
`candidates`.`youtube_showreel_desc_4`,
`candidates`.`youtube_showreel_desc_5`,
`candidates`.`date_created`,
DATE_FORMAT(NOW(), "%Y") - DATE_FORMAT(`candidates`.`DOB`, "%Y") - (DATE_FORMAT(NOW(), "00-%m-%d") < DATE_FORMAT(`candidates`.`DOB`, "00-%m-%d")) as `age`,
`candidate_assets`.`url`,
`candidate_assets`.`asset_size`
FROM `candidates`
LEFT JOIN `candidate_assets` ON `candidate_assets`.`candidates_candidate_id` = `candidates`.`candidate_id`
WHERE `candidates`.`availability` = "yes"';
//is there a certain criteria
if($type != "0") { $sql .= 'AND `candidates`.`talent` = '.$type; }
if($age != "0") { $sql .= 'AND `candidates`.`playing_age` LIKE "%'.$age.'%"';}
if($gender != false){ $sql .= 'AND `candidates`.`gender` = '.$gender; }
$sql .= ' GROUP BY `candidates`.`candidate_id`';
$query = $this->db->query($sql);
//die(print_r($query->result_array()));
//die($this->db->last_query());
return $query->result_array();
}
CONTROLLER
$data['results'] =
$this->candidates_model->searchCandidates($this->input->post('type'), $this->input->post('gender'), $this->input->post('age'));
$this->session->set_userdata('search', $data['results']);
//die(print_r($this->session->userdata('search')));
$this->template->build('search/results', $data);
我的错误日志中的错误如下所示,
[2011 年 12 月 9 日星期五 10:28:49] [错误] [客户端 xx.xx.xx.xxx] 格式错误 标题来自 脚本。错误标头=Bs%3A2%3A%2259%22%3Bs%3A10%3A%:index.php,引荐来源:http://urbantalent.factoryagency.co.uk/search
Yesterday I changed my database schema, and consequently a database query in my application, since then I have been receiving 500 Internal Server Errors. I do not think that the problem is the query, as I can run it from my model, and print the array from my controller. The problem seems to be when building the template.
Below is my code,
MODEL
public function searchCandidates($type=null, $gender=null, $age=null)
{
if($age != "0") {
$age = implode(", %",$age);
}
$sql = 'SELECT `candidates`.`candidate_id`,
`candidates`.`first_name`,
`candidates`.`surname`,
`candidates`.`DOB`,
`candidates`.`gender`,
`candidates`.`talent`,
`candidates`.`location`,
`candidates`.`availability`,
`candidates`.`youtube_showreel_1`,
`candidates`.`youtube_showreel_2`,
`candidates`.`youtube_showreel_3`,
`candidates`.`youtube_showreel_4`,
`candidates`.`youtube_showreel_5`,
`candidates`.`youtube_showreel_desc_1`,
`candidates`.`youtube_showreel_desc_2`,
`candidates`.`youtube_showreel_desc_3`,
`candidates`.`youtube_showreel_desc_4`,
`candidates`.`youtube_showreel_desc_5`,
`candidates`.`date_created`,
DATE_FORMAT(NOW(), "%Y") - DATE_FORMAT(`candidates`.`DOB`, "%Y") - (DATE_FORMAT(NOW(), "00-%m-%d") < DATE_FORMAT(`candidates`.`DOB`, "00-%m-%d")) as `age`,
`candidate_assets`.`url`,
`candidate_assets`.`asset_size`
FROM `candidates`
LEFT JOIN `candidate_assets` ON `candidate_assets`.`candidates_candidate_id` = `candidates`.`candidate_id`
WHERE `candidates`.`availability` = "yes"';
//is there a certain criteria
if($type != "0") { $sql .= 'AND `candidates`.`talent` = '.$type; }
if($age != "0") { $sql .= 'AND `candidates`.`playing_age` LIKE "%'.$age.'%"';}
if($gender != false){ $sql .= 'AND `candidates`.`gender` = '.$gender; }
$sql .= ' GROUP BY `candidates`.`candidate_id`';
$query = $this->db->query($sql);
//die(print_r($query->result_array()));
//die($this->db->last_query());
return $query->result_array();
}
CONTROLLER
$data['results'] =
$this->candidates_model->searchCandidates($this->input->post('type'), $this->input->post('gender'), $this->input->post('age'));
$this->session->set_userdata('search', $data['results']);
//die(print_r($this->session->userdata('search')));
$this->template->build('search/results', $data);
The error in my error logs looks like this,
[Fri Dec 09 10:28:49 2011] [error] [client xx.xx.xx.xxx] malformed
header from
script. Bad header=Bs%3A2%3A%2259%22%3Bs%3A10%3A%: index.php, referer: http://urbantalent.factoryagency.co.uk/search
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最好的猜测是您试图将太多内容放入 ci_session cookie 中。
那里的一些值可能没有正确编码,并且看起来不像有效的标头。 set-cookie HTTP 标头将是错误的,因此您会收到“格式错误的标头”错误。
首先限制此 cookie 中的内容。如果可以的话,也许禁用/根本不使用它(暂时在调试代码时)
然后修复动态 SQL - 每个“AND”前面需要空格
。性别可能也应该用双引号引起来。
编辑:
是的,现在我更加确信了。
sico87 - 这是从您的网站实时发布的(见下文 - 抱歉字符串很长)。
这个cookie的大小接近8K。 HTTP 标头的大小有一定的限制。从 Apache 的内存来看,它是 8K。
所以我最好的猜测(是的,猜测!)是,在某些情况下,您在 cookie 中放入了超过 8K 的内容,并且此请求失败。解决方案是不使用 cookie 来存储/传输大量数据。
My best guess is that you are trying to put too many things into the ci_session cookie.
Some of the values there might not be encoded properly and don't look like a valid header. set-cookie HTTP header will then be wrong hence you get the "malformed header" error.
Start by limiting WHAT goes into this cookie. Perhaps disable/don't use it at all if you can (temporarily while you debug your code)
Then fix the dynamic SQL - you need space in front of each "AND"
.gender should probably also be wrapped in double quotes.
EDIT:
Yes, now I am even more convinced.
sico87 - This is live from your site (see below - sorry for a very long string).
The size of this cookie is close to 8K. There are certain limits on the size of HTTP headers. And from memory for Apache it is 8K.
So my best guess (yes, guess!) is that under certain circumstances you put more than 8K in the cookie and this request fails. The solution is to not use cookies to store/transfer large volumes of data.
首先,echo() SQL语句(然后die(),避免遇到错误),并在SQL工具中尝试一下,看看会发生什么。我认为,这是一个有错误的 SQL 语句,或者只是 SQL 引擎由于某种原因不喜欢它 - 所以你应该尝试获取详细的错误消息,而不是“500 内部服务器错误”。
First of all, echo() the SQL statement (then die(), avoid to run into the error), and try it in a SQL tool, to see what happens. I think, it's a buggy SQL statement, or just the SQL engine does not like it for some reason - so you should try to get a detailed error message, instead of the "500 Internal Server error".
也许你需要将其更改
为
Maybe you need to change this
to