Php 查询错误:错误在哪里?
我得到以下错误:Php 查询错误:错误在哪里?,php,mysql,Php,Mysql,我得到以下错误: [07-Mar-2011 04:52:31] exception 'Exception' in /home1/mexautos/public_html/kiubbo/data/model.php:89 Stack trace: #0 /home1/mexautos/public_html/kiubbo/data/article.php(276): Model::execSQl2('update articles...') #1 /home1/mexautos/public_ht
[07-Mar-2011 04:52:31] exception 'Exception' in /home1/mexautos/public_html/kiubbo/data/model.php:89
Stack trace:
#0 /home1/mexautos/public_html/kiubbo/data/article.php(276): Model::execSQl2('update articles...')
#1 /home1/mexautos/public_html/kiubbo/data/article.php(111): Article->save()
#2 /home1/mexautos/public_html/kiubbo/pages/frontpage.php(21): Article->calculateRanking()
#3 /home1/mexautos/public_html/kiubbo/pages/frontpage.php(27): FrontPage->updateRanking()
#4 /home1/mexautos/public_html/kiubbo/index.php(15): FrontPage->showTopArticles('')
#5 {main}
这是一行:$lastid=parent::execSql2($query)
以下是代码,如果有人能帮我找到错误所在:
function save() {
/*
Here we do either a create or
update operation depending
on the value of the id field.
Zero means create, non-zero
update
*/
if(!get_magic_quotes_gpc())
{
$this->title = addslashes($this->title);
$this->description = addslashes($this->description);
}
try
{
$db = parent::getConnection();
if($this->id == 0 )
{
$query = 'insert into articles (modified, username, url, title, description, points )';
$query .= " values ('$this->getModified()', '$this->username', '$this->url', '$this->title', '$this->description', '$this->points' )";
}
else if($this->id != 0)
{
$query = "update articles set modified = CURRENT_TIMESTAMP, username = '$this->username', url = '$this->url', title = '$this->title', description = '$this->description', points = '$this->points', ranking = '$this->ranking' where id = '$this->id' ";
}
$lastid = parent::execSql2($query);
if($this->id == 0 )
$this->id = $lastid;
}
catch(Exception $e){
error_log($e);
}
}
您必须将变量包含在
{}
标记中;像这样
$query .= " values ('{$this->getModified()}', '{$this->username}', '{$this->url}', '{$this->title}', '{$this->description}', '{$this->points}' )";
花括号让PHP知道不要将文本视为文字。请注意,这只适用于双引号字符串。(编辑代码;我忘记了每个值的单引号)
回显字符串可能会有所帮助,这样您就可以检查将要执行的操作。我认为您需要使引发异常的内容更具信息性。乍一看,你是在逃避争论吗?回显查询并尝试在没有php的情况下运行。与其尝试在代码中发现错误,不如让execSQL2方法实际抛出一个带有SQL错误消息的异常。谢谢,我已经更正了代码,正如Pekka所说,我将得到一个真正的错误抛出。你好,卡洛斯
$query .= " values ('{$this->getModified()}', '{$this->username}', '{$this->url}', '{$this->title}', '{$this->description}', '{$this->points}' )";