PHP意外的文件结束错误

时间:2015-01-03 17:14:21

标签: php

我知道这个问题被多次询问,我对这个错误进行了非常多的搜索,但我没有在这个网站和互联网上找到任何有用的东西。你能帮我找出我的错误吗?

  

“解析错误:语法错误,第31行的C:\ wamp \ www \ Project2 \ members \ download.php中的文件意外结束”

这是我的download.php文件:

<html>
<body>
<title>Download your friend's uploads</title>
<?php
session_start();
$username =$_SESSION["uname"];
?>

<?php

 $con = mysqli_connect("localhost", "root", "", "webpage");
 if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();

 $query = "SELECT imagesnotes,username From userdata where sharedpeople='$username";
 $res = mysqli_query($con,$query);
 $res or die('Error, query failed');
 if(mysqli_num_rows($res) == 0){
 echo "<br>Database is empty </br>";
 } 
 else{
 while(list($id,$name) = mysql_fetch_array($res)){
 echo '<br><a href="download.php?id=<?php=$id;?>"><?php=$name;?></a></br>';
 }
 }

 mysqli_close($con);

?>
</body>
</html>

我正在考虑1小时,我找不到错误的解决方案。

3 个答案:

答案 0 :(得分:1)

此行没有匹配的}

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();

然后,您的程序的整个正文 in if,用于处理错误。

如果您缩进代码,您会发现这些错误更容易找到。

答案 1 :(得分:0)

缺少结算}修改后的代码:

<html>
<body>
<title>Download your friend's uploads</title>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php

 $con = mysqli_connect("localhost", "root", "", "webpage");
 if (mysqli_connect_errno()) 
 {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();

   $query = "SELECT imagesnotes,username From userdata where sharedpeople='$username";
   $res = mysqli_query($con,$query);
   $res or die('Error, query failed');
   if(mysqli_num_rows($res) == 0)
   {
    echo "<br>Database is empty </br>";
   } 
   else
   {
     while(list($id,$name) = mysql_fetch_array($res))
     {
      echo '<br><a href="download.php?id=<?php=$id;?>"><?php=$name;?></a></br>';
     }
}}

 mysqli_close($con);

?>

</html>

答案 2 :(得分:-1)

在以下一行

$query = "SELECT imagesnotes,username From userdata where sharedpeople='$username";

删除'或更好地使用

$query = "SELECT imagesnotes,username From userdata where sharedpeople='$username'";

正确地逃避它。

你仍然需要阅读:How can I prevent SQL injection in PHP?


在以下一行

if (mysqli_connect_errno()) {

删除{或使用

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

并且可能在错误消息之后添加退出。