1

Not done any php database work for years, fairly simple question I'm sure for anyone with experience, below is my code it should take all the results from the database and display these to a table with links:

<?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>

So the line it's failing on is

while($query2=mysql_fetch_array($query1))

The error message is:

Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in C:\xampp\htdocs\welcome.php on line 27

1
  • 1
    You're mixing both MySQLi (mysqli_query) and MySQL (mysql_fetch_array) syntax. MySQL functions are deprecated in PHP 5.5, and removed in PHP 7, so please don't use it. Commented Apr 4, 2017 at 22:37

3 Answers 3

1

Instead of mysql_fetch_array you need to use 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>

Note: I also changed your variable name to a more sensible one.

Sign up to request clarification or add additional context in comments.

Comments

1

your resource is mysqli and you try to handle with mysql_fetch_array. try to use mysqli_fetch_array. i think this will eliminate your problem

$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))
}

And never forget to check for errors on the query

Comments

1

The error lies in the mismatch of mysql and mysqli connections. As you are using mysqli_query, you should use the mysqli. Replacing the following line with error line should do the work:

mysqli_fetch_array($db,$query1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.