为什么我得到:语法错误意外 'endwhile'?
Why do I get: Syntax error unexpected 'endwhile'?
这是我的代码:
index.php:
<?php
session_start();
require_once "db_connect.php";
$sql = "SELECT forum_id, forum_name FROM froum_table";
if($query = $db->prepare($sql)) {
$query->execute();
$query->bind_result($f_id, $f_name);
$query->store_result();
} else {
echo $db->error;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charest="utf-8">
<title>my2</title>
</head>
<body>
<div id="container">
<table align="center" width="80%">
<?php
if($query->num_rows !== 0):
while($row = $query->fetch()):
?>
<tr>
<td><a href="froum.php?id=<?php echo $f_id; ?>"><?php echo $f_name; ?></a></td>
</tr>
<?php endwhile; endif;?>
</table>
</div>
</body>
</html>
db_connect.php:
<?php
$db = new mysqli ("localhost","root".""."fourm") or die ("ERROR! withe connection");
?>
我遇到了这些错误:
No database selected
Notice: Trying to get property of non-object in C:\xampp\htdocs\scripts\my2\index.php on line 29
Fatal error: Call to a member function fetch() on a non-object in C:\xampp\htdocs\scripts\my2\index.php on line 30
这里编码我的数据库
CREATE TABLE IF NOT EXISTS `forum_tabl` (
`forum_id` int(11) NOT NULL,
`forum_name` varchar(100) NOT NULL,
`forum_description` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `forum_tabl`
--
INSERT INTO `forum_tabl` (`forum_id`, `forum_name`, `forum_description`) VALUES
(1, 'web design', 'a forum about web design');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `forum_tabl`
--
ALTER TABLE `forum_tabl`
ADD PRIMARY KEY (`forum_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `forum_tabl`
--
ALTER TABLE `forum_tabl`
MODIFY `forum_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
此答案来自第一次修订:
https://whosebug.com/revisions/28546869/1
您的代码中有一些错误。
1。错误的替代语法
if($query->num_rows !==0);
//^
//...
while($row = $query->fetch());
//^ Currently your loop executes this empty statement
只需将分号更改为逗号,如下所示:
while($row = $query->fetch()):
//^ See here alternative syntax
//...
if($query->num_rows !==0):
//^
有关替代语法的更多信息,请参阅手册:http://php.net/manual/en/control-structures.alternative-syntax.php
2。拼写错误
您在 SQL 查询中拼错了 FORM
,只需将其更改为:FROM
另外我觉得froum_id
应该是:forum_id
在你的 html 中:
<td><a herf ...
//^^^^ Should be 'href'
来自@Jay Blanchard 的评论:
You misspelled forum_tabl
我想你想写:forum_table
3。错误bind_result()
调用
$query->bind_result($f_id. $f_name);
//^ Replace '.' with ','
4。顺序错误
$query->bind_result($f_id, $f_name);
//^^^^^^^^^^^ This comes after the execution
$query->execute;
//^ Missing '()'
5。缺少引号
这里你忘了双引号:
<table align="center" width=80%">
应该是:
<table align="center" width="80%">
//^
6.错误的连接调用
据我所知,您将逗号与点混合使用:
<?php $db = new mysqli ("localhost","root".""."fourm") or die ("ERROR! withe connection"); ?>
//^ ^ Should be commas
所以使用这样的东西:
<?php
$db = new mysqli ("localhost", "root","", "fourm");
if ($db->connect_error) {
die('Connect Error (' . $db->connect_errno . ') '
. $db->connect_error);
}
?>
(一如既往,我怀疑你想写:forum
而不是 fourm
)
所以最后你的代码应该是这样的:
<?php
session_start();
require_once "db_connect.php";
$sql = "SELECT forum_id, forum_name FROM froum_table";
if($query = $db->prepare($sql)) {
$query->execute();
$query->bind_result($f_id, $f_name);
} else {
echo $db->error;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charest="utf-8">
<title>my2</title>
</head>
<body>
<div id="container">
<table align="center" width="80%">
<?php
if($query->num_rows !== 0):
while($row = $query->fetch()):
?>
<tr>
<td><a href="froum.php?id=<?php echo $f_id; ?>"><?php echo $f_name; ?></a></td>
</tr>
<?php endwhile; endif;?>
</table>
</div>
</body>
</html>
这是我的代码:
index.php:
<?php
session_start();
require_once "db_connect.php";
$sql = "SELECT forum_id, forum_name FROM froum_table";
if($query = $db->prepare($sql)) {
$query->execute();
$query->bind_result($f_id, $f_name);
$query->store_result();
} else {
echo $db->error;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charest="utf-8">
<title>my2</title>
</head>
<body>
<div id="container">
<table align="center" width="80%">
<?php
if($query->num_rows !== 0):
while($row = $query->fetch()):
?>
<tr>
<td><a href="froum.php?id=<?php echo $f_id; ?>"><?php echo $f_name; ?></a></td>
</tr>
<?php endwhile; endif;?>
</table>
</div>
</body>
</html>
db_connect.php:
<?php
$db = new mysqli ("localhost","root".""."fourm") or die ("ERROR! withe connection");
?>
我遇到了这些错误:
No database selected
Notice: Trying to get property of non-object in C:\xampp\htdocs\scripts\my2\index.php on line 29
Fatal error: Call to a member function fetch() on a non-object in C:\xampp\htdocs\scripts\my2\index.php on line 30
这里编码我的数据库
CREATE TABLE IF NOT EXISTS `forum_tabl` (
`forum_id` int(11) NOT NULL,
`forum_name` varchar(100) NOT NULL,
`forum_description` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `forum_tabl`
--
INSERT INTO `forum_tabl` (`forum_id`, `forum_name`, `forum_description`) VALUES
(1, 'web design', 'a forum about web design');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `forum_tabl`
--
ALTER TABLE `forum_tabl`
ADD PRIMARY KEY (`forum_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `forum_tabl`
--
ALTER TABLE `forum_tabl`
MODIFY `forum_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
此答案来自第一次修订:
https://whosebug.com/revisions/28546869/1
您的代码中有一些错误。
1。错误的替代语法
if($query->num_rows !==0);
//^
//...
while($row = $query->fetch());
//^ Currently your loop executes this empty statement
只需将分号更改为逗号,如下所示:
while($row = $query->fetch()):
//^ See here alternative syntax
//...
if($query->num_rows !==0):
//^
有关替代语法的更多信息,请参阅手册:http://php.net/manual/en/control-structures.alternative-syntax.php
2。拼写错误
您在 SQL 查询中拼错了 FORM
,只需将其更改为:FROM
另外我觉得froum_id
应该是:forum_id
在你的 html 中:
<td><a herf ...
//^^^^ Should be 'href'
来自@Jay Blanchard 的评论:
You misspelled forum_tabl
我想你想写:forum_table
3。错误bind_result()
调用
$query->bind_result($f_id. $f_name);
//^ Replace '.' with ','
4。顺序错误
$query->bind_result($f_id, $f_name);
//^^^^^^^^^^^ This comes after the execution
$query->execute;
//^ Missing '()'
5。缺少引号
这里你忘了双引号:
<table align="center" width=80%">
应该是:
<table align="center" width="80%">
//^
6.错误的连接调用
据我所知,您将逗号与点混合使用:
<?php $db = new mysqli ("localhost","root".""."fourm") or die ("ERROR! withe connection"); ?>
//^ ^ Should be commas
所以使用这样的东西:
<?php
$db = new mysqli ("localhost", "root","", "fourm");
if ($db->connect_error) {
die('Connect Error (' . $db->connect_errno . ') '
. $db->connect_error);
}
?>
(一如既往,我怀疑你想写:forum
而不是 fourm
)
所以最后你的代码应该是这样的:
<?php
session_start();
require_once "db_connect.php";
$sql = "SELECT forum_id, forum_name FROM froum_table";
if($query = $db->prepare($sql)) {
$query->execute();
$query->bind_result($f_id, $f_name);
} else {
echo $db->error;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charest="utf-8">
<title>my2</title>
</head>
<body>
<div id="container">
<table align="center" width="80%">
<?php
if($query->num_rows !== 0):
while($row = $query->fetch()):
?>
<tr>
<td><a href="froum.php?id=<?php echo $f_id; ?>"><?php echo $f_name; ?></a></td>
</tr>
<?php endwhile; endif;?>
</table>
</div>
</body>
</html>