PHP MySQL INSERT return value with one query execution

前端 未结 6 1072
深忆病人
深忆病人 2021-01-05 04:42

Is there anything returned from MySQL/PHP on a INSERT query being executed? Here is my function which I have in a CLASS.

function mysqlQuery($query) {
   //          


        
6条回答
  •  太阳男子
    2021-01-05 05:35

    In the example below, I add to my insert clause the "returning" along with the primary key of my table, then after the execute, I do a fetch getting an array with the value of the last inserted id.

    prepare($sqlQuery); 
    
            $a ="2002-03-11 12:01AM" ; 
    
            $statement->bindParam(":user_id", $employee->getUserId(), PDO::PARAM_INT); 
            $statement->bindParam(":name", $employee->getName(), PDO::PARAM_STR); 
            $statement->bindParam(":address", $employee->getAddress(), PDO::PARAM_STR); 
            $statement->bindParam(":city", $employee->getCity(), PDO::PARAM_STR); 
            $statement->execute(); 
    
            $result = $statement->fetch(PDO::FETCH_ASSOC); 
            return $result["employee_id"]; 
    
        } 
    ?>
    

    Source

提交回复
热议问题