看不到解析错误

时间:2013-08-19 16:28:31

标签: php sql mysqli

使用我的第一个与SQL交互的PHP脚本。我很近,我闻到了它!

我正在尝试在表格中返回MAX日期并收到以下消息:日期应为:

  

解析错误:语法错误,意外的T_VARIABLE   /Applications/XAMPP/xamppfiles/htdocs/tslocal/themes/myname/views/reports/get_vote_date.php   第25行

第25行在下面的脚本中为$result = mysqli_query($dbc, $query);

这是剧本,我一直盯着它,直到我的眼睛流血,但不确定它是“错的”,因为我是新手:

<?php # script get_vote_date

// This file contains the db info
// This file establishes a mysql connection, connects to the db and then gets the most recent vote date for a particular page (incident_id).

DEFINE ('DB_USER', 'myname');
DEFINE ('DB_PASSWORD', 'somepass123');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'sitename');


// make the db connection
$dbc = @mysqli_connect('DB_HOST','DB_USER','DB_PASSWORD','DB_NAME')
    OR die ('Could not connect to mysql: ' . mysqli_connect_error());

// Set the encoding
mysqli_set_charset($dbc, 'utf8');

// set the query variable
$query = 'SELECT MAX(rating_date) 
          FROM rating
          WHERE incident_id = $incident_id;'

//connect and run the query
$result = mysqli_query($dbc, $query);

 echo $result;

?>

如果它是任何值,这里是我试图从中提取数据的表的屏幕: enter image description here

1 个答案:

答案 0 :(得分:4)

真正的错误 - 缺少(实际上错位)分号 - 在前面的陈述中:

$query = 'SELECT MAX(rating_date) 
          FROM rating
          WHERE incident_id = $incident_id;' // <-- TODO: Put the ; outside of the string literal

//connect and run the query
$result = mysqli_query($dbc, $query); 

对于PHP,它在语法上看起来像

$query = "string" $result = mysqli_query(...); 

这当然是一个解析错误。请注意,字符串文字中的分号对PHP没有特殊含义 - 它终止语句(尽管它会产生SQL错误:))。

不幸的是,PHP没有看到你对第22行的意图并且仅在第25行检测到错误。