使用php运行查询时出错

时间:2018-04-30 17:39:16

标签: php mysql xampp

我有一个用户表,我正在尝试运行查询以将最后一个user_id添加到表中。我收到一个错误。这是代码:

    $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'];



      }

我收到以下错误

  

致命错误:未捕获错误:调用未定义的函数   C:\ xampp \ htdocs \ web2 \ php \ signup_process.php中的mysql_fetch_array():165   堆栈跟踪:#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}引入   第165行的C:\ xampp \ htdocs \ web2 \ php \ signup_process.php

165具有以下代码:$arrayResult = mysql_fetch_array($result);

1 个答案:

答案 0 :(得分:1)

您使用的是msqli,因此mysql_fetch_array应为mysqli_fetch_array。你也可以select MAX(User_ID) from users;

此外,您应该使用预准备语句来避免sql注入。