-1

I have a table of users and I'm trying to run a query to get the last user_id added to the table. I get an error. Here's the code:

    $connect = mysqli_connect("localhost","adhude","windows","photodb");

    // Check connection


    if(!$connect){
      die("connection failed :"+ mysqli_connect_errno());
    }else{


      function NewUser(){
        $sql = "INSERT INTO user (User_Name, Password) VALUES  ('".$_POST["Email"]."','".$_POST["psw"]."')";

        $result=mysqli_query($GLOBALS['connect'],$sql);

        if($result){


          echo "<script> alert('Records added successfully')</script>";
          GetUserId();
        }else {
          echo "<script> alert('Records not added ')</script>";
        }

      }


      //function to get the last added user id
      function GetUserId(){
        $sql="SELECT * FROM user ORDER BY User_Id DESC LIMIT 1";
        $result=mysqli_query($GLOBALS['connect'],$sql);
        if (!$result) {
          echo 'Could not run query: ' . mysql_error();
          exit;
          }


        $arrayResult = mysql_fetch_array($result);

        $Latest_User=$arrayResult['User_Id'];



      }

I'm getting the following error

Fatal error: Uncaught Error: Call to undefined function mysql_fetch_array() in C:\xampp\htdocs\web2\php\signup_process.php:165 Stack trace: #0 C:\xampp\htdocs\web2\php\signup_process.php(148): GetUserId() #1 C:\xampp\htdocs\web2\php\signup_process.php(203): NewUser() #2 C:\xampp\htdocs\web2\php\signup_process.php(214): SignUp() #3 C:\xampp\htdocs\web2\pages\signup.php(14): include('C:\xampp\htdocs...') #4 {main} thrown in C:\xampp\htdocs\web2\php\signup_process.php on line 165

165 has the following code : $arrayResult = mysql_fetch_array($result);

Patrick Q
  • 6,373
  • 2
  • 25
  • 34
shihan
  • 31
  • 1
  • 3
  • You have a typo. This line `$arrayResult = mysql_fetch_array($result);` should be `$arrayResult = mysqli_fetch_array($result);`. Notice the letter i after mysql? – Dave Apr 30 '18 at 17:45

1 Answers1

1

You are using msqli so mysql_fetch_array should be mysqli_fetch_array. Also you could just select MAX(User_ID) from users;.

Also you should use prepared statements to avoid sql injection.

Echtniet
  • 200
  • 2
  • 11