php显示mysql数据库的结果

时间:2017-04-04 22:32:31

标签: php html mysql sql while-loop

多年来没有做任何php数据库工作,相当简单的问题我确信任何有经验的人,下面是我的代码它应该从数据库获取所有结果并将这些显示到带有链接的表中:

import com.sun.javafx.scene.control.skin.TextFieldSkin;
import javafx.scene.control.TextField;

public class TextFieldCaretControlSkin extends TextFieldSkin {
    public TextFieldCaretControlSkin(TextField textField, double scale) {
        super(textField);

        setScale(scale);
    }

    private void setScale(double scale) {
        caretPath.setScaleX(scale);
        caretPath.setScaleY(scale);
    }
}

所以它失败的线是

<?php
include('session.php');
?>
<html">
<head>
<title>Welcome </title>
</head> 
<body>
  <h1>Welcome <?php echo $login_session; ?></h1> 

  <form method="post" action="upload.php" enctype="multipart/form-data">
<p>File:</p>
<input type="file" name="Filename"> 
<p>Description:</p>
<textarea rows="10" cols="35" name="Description"></textarea>
<br/>
<input TYPE="submit" name="upload" value="Submit"/>
</form>

<hr/>
<p>Uploaded Files</p>

<?php
//include('config.php');
$query1=mysqli_query($db,"SELECT filepath,filename,description FROM `filedetails`");
echo "<table><tr><td>filepath</td><td>filename</td><td></td><td></td>";
while($query2=mysql_fetch_array($query1))
{
echo "<tr><td>".$query2['filepath']."</td>";
echo "<td>".$query2['filename']."</td>";
echo "<td><a href='edit.php?id=".$query2['id']."'>Edit</a></td>";
echo "<td><a href='delete.php?id=".$query2['id']."'>x</a></td><tr>";
}
?>
<h2><a href = "logout.php">Sign Out</a></h2>
</body>
</html>

错误消息是:

警告:mysql_fetch_array()要求参数1为资源,第27行的C:\ xampp \ htdocs \ welcome.php中给出的对象

3 个答案:

答案 0 :(得分:1)

而不是mysql_fetch_array,您需要使用mysqli_fetch_array

<?php
    include('session.php');
?>
<html">
    <head>
        <title>Welcome </title>
    </head> 
    <body>
        <h1>Welcome <?php echo $login_session; ?></h1> 

        <form method="post" action="upload.php" enctype="multipart/form-data">
            <p>File:</p>
            <input type="file" name="Filename"> 
            <p>Description:</p>
            <textarea rows="10" cols="35" name="Description"></textarea>
            <br/>
            <input TYPE="submit" name="upload" value="Submit"/>
        </form>

        <hr/>
        <p>Uploaded Files</p>

        <?php
            //include('config.php');
            $query1=mysqli_query($db,"SELECT filepath,filename,description FROM `filedetails`");
            echo "<table><tr><td>filepath</td><td>filename</td><td></td><td></td>";
            while($row=mysqli_fetch_array($query1))
            {
                echo "<tr><td>".$row['filepath']."</td>";
                echo "<td>".$row['filename']."</td>";
                echo "<td><a href='edit.php?id=".$row['id']."'>Edit</a></td>";
                echo "<td><a href='delete.php?id=".$row['id']."'>x</a></td><tr>";
            }
        ?>
        <h2><a href = "logout.php">Sign Out</a></h2>
    </body>
</html>

注意:我还将变量名称更改为更明智的名称。

答案 1 :(得分:1)

您的资源是mysqli,您尝试使用mysql_fetch_array处理。尝试使用mysqli_fetch_array。我认为这将消除你的问题

$resource = mysqli_query($connection, $queryString);
if(!mysqli_errno($connection)) {
  while($resultArray = mysqli_fetch_array($resource)){
    print_r($resultArray); // pseudo output for your table
  }
} else {
  echo "error\n"
  print_r(mysqli_error($connection))
}

永远不要忘记检查查询中的错误

答案 2 :(得分:1)

错误在于mysql和mysqli连接不匹配。当你使用mysqli_query时,你应该使用mysqli。用错误行替换以下行应该可以完成工作:

mysqli_fetch_array($db,$query1);