为什么我得到:语法错误意外' endwhile'?

时间:2015-02-16 17:18:01

标签: php mysqli syntax-error

这是我的代码:

的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");

?>

我收到了这些错误:

  

未选择数据库

     

注意:尝试在第29行的C:\ xampp \ htdocs \ scripts \ my2 \ index.php中获取非对象的属性

     

致命错误:在第30行的C:\ xampp \ htdocs \ scripts \ my2 \ index.php中的非对象上调用成员函数fetch()

这里编码我的数据库

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 */;

1 个答案:

答案 0 :(得分:4)

这个答案来自第一次修订:

https://stackoverflow.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的评论:

  

你拼错了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>