为什么我会收到“Unexpected T_AS”在 PHP PDO 请求中?
我收到以下错误...
解析错误:语法错误,第 98 行 ....\index.php 中出现意外的 T_AS
以下脚本第 98 行 ....\index.php 中出现意外的 T_AS...
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=db', 'user', 'pw');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->beginTransaction();
$stmt = $db->prepare("SELECT * FROM tablename");
$stmt->execute();
while($db->fetch(PDO_FETCH_ASSOC) as $row) {
$id= $row['id'];
$name= $row['name'];
}
$db->commit();
}
catch (PDOException $e)
{
$db->rollback();
echo "There was a system error.<br>".$e->getMessage();
}
?>
知道是什么引发了错误吗?我检查了是否缺少分号、逗号和作品,但一无所获!
I receive the following error...
Parse error: syntax error, unexpected T_AS in ....\index.php on line 98
for the following script...
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=db', 'user', 'pw');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->beginTransaction();
$stmt = $db->prepare("SELECT * FROM tablename");
$stmt->execute();
while($db->fetch(PDO_FETCH_ASSOC) as $row) {
$id= $row['id'];
$name= $row['name'];
}
$db->commit();
}
catch (PDOException $e)
{
$db->rollback();
echo "There was a system error.<br>".$e->getMessage();
}
?>
Any idea what is throwing the error? I have checked for missing semicolons, commas, and the works but got nothing!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
T_AS
是 PHP 解释器中as
的标记。当尝试解析代码的语法时,这是意外的。as
仅在foreach
循环中有效,并且您使用的是while
。将
while
循环更改为foreach
循环。更新
这是一个运行时错误 - PDO 对象没有名为
fetch()
的方法。您是否在正确的对象上调用fetch()
?查看文档。
正如 Wrikken 在评论中所述,它将是您的
$stmt
对象的方法。T_AS
is the token foras
in the PHP interpreter. It was unexpected when trying to parse your code's syntax.as
is only valid in aforeach
loop, and you are using awhile
.Change your
while
loop to aforeach
loop.Update
This is a run time error - the PDO object has no method called
fetch()
. Are you callingfetch()
on the right object?Check out the documentation.
As Wrikken states in the comments, it will be a method of your
$stmt
object.因为您在“while”循环中使用“as”关键字,这是无效的。将“while”更改为“foreach”,然后就可以开始了。
Because you are using the 'as' keyword in a 'while' loop, which is not valid. Change the 'while' to 'foreach' and you are good to go.