-2

I keep getting this error, I have looked at a question raised by a previous poster with this issue and the advice handed out to them has been of no help in resolving my issue, had it done so I certainly would not have raised this query so please dont redirect! I am developing a project where the user registers their details name age, email etc but I want them to have the capability of editing the data It is fine up loding the data to the db, but on trying to retrieve it for the user to view I get the error mysql_fetch_array() expects parameter 1 to be resource, object given

Here is my code

<?php
require_once'connect.php';

    $FirstName = $_POST['fName'];
    $UserName = $_POST['uName'];
    $Age = $_POST['age'];
    $Password = $_POST['password'];
    $Email = $_POST['email'];

    $sql = "INSERT INTO `user` (`First_Name`,`UserName`,`Age`,`Password`,`Email`) 
        VALUES ('$FirstName','$UserName','$Age','$Password','$Email')";

    if(! mysqli_query($con, $sql)){
        die("Echo ".mysqli_error($sql));
    }else{
        header('Location: BankDetails_Form.php');
    }

So that uploads the data to the db

Now the code that I use to retrieve it is:

<?php
    include 'connect.php';

    if(isset($_POST['view'])){

        $query = "SELECT `First_Name`,`Username`,`Age`,`Password`,`Email` FROM `user`";
        $result = mysqli_query($con, $query)
         or die('Error querying database');

         while ($row = mysql_fetch_array($result)){
             echo $row['First_Name'].''.$row['Username'].''.$row['Age'].$row['Password'].$row['Email'];
         }
    }
?>

Can anyone possibly point me in the right direction as to why I'm getting that error, again I realise this question has been raised but the previous solutions did not help me.

Marc Delisle
  • 8,879
  • 3
  • 29
  • 29
Steve73NI
  • 17
  • 2
  • 9
  • Apologies I thought it had appeared. – Steve73NI Sep 16 '13 at 12:23
  • 3
    Use [`mysqli_fetch_array()`](http://php.net/mysqli-fetch-array), not [`mysql_fetch_array()`](http://php.net/mysql-fetch-array). They are different. – BlitZ Sep 16 '13 at 12:24
  • `UserName` OR `Username`??? – Ashwini Agarwal Sep 16 '13 at 12:25
  • Escape the $_POST values you're passing to the SQL statement; or better yet, switch to MySQLi/PDO with prepared statements/bind variables – Mark Baker Sep 16 '13 at 12:28
  • Corrupt thanks for spotting that, it works now, I guess this is what this forum is here for, experts to spot the mistakes made by us beginners lol. – Steve73NI Sep 16 '13 at 12:31
  • Not actually if a valid error appeared. It says mysql_fetch_array needds resource, but OBJECT given. You can ask yoruself "how did I end up with object passed to mysql_fetch_array" – Royal Bg Sep 16 '13 at 12:38
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Sep 16 '13 at 22:59
  • Does this answer your question? [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Dharman Feb 02 '20 at 14:09

1 Answers1

6

Use the mysqli_fetch_array instad of mysql_fetch_array.

You are using the mysqli_* and to fetch the data you are using the mysql_fetch_array.

That's why it is giving the error.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75