9

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) {
   // Gets the results from the query
   $results = mysql_query($query, $this->connection);

   // Loops through the queried results as an multi-dimensional array
   while($rows = mysql_fetch_array($results, MYSQL_ASSOC)) {
      // Push results to single array
      $rs[] = $rows;
   }

   // Return results as array
   return $rs;
}

This is how I call the function

$rs = $dbh->mysqlQuery($query);

But executing a INSERT query the $rs returns nothing. Does my function need help or is this the default behavior? Any tips would be helpful as well.

halfer
  • 19,824
  • 17
  • 99
  • 186
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383

6 Answers6

40

INSERT just returns true or false. to actually return something useful, you will need a SELECT or similar query. there is no result to fetch with INSERT.

helloandre
  • 10,541
  • 8
  • 47
  • 64
8

From the php documentation:

Return Values For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

Rodrigo García
  • 1,357
  • 15
  • 26
Ricardo
  • 291
  • 2
  • 11
3

Although this is a very old thread, it is still relevant. And to those who stumble upon this (like I did), don't give up! There are options. In fact, see this answer on SO

Also, for sqli and pdo, see this

Essentially, an insert statement followed by one of the functions listed in those answers will give you the ID of the last record. The function is used like this:

$LastID = mysql_insert_id();

after the INSERT statement.

Chiwda
  • 1,233
  • 7
  • 30
  • 52
0

From php.net http://us2.php.net/mysql_query

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

0

Generally, when devs are building home functions to manipulates queries, they use two methods. One, "query", used for SELECTs and co, and one, "exec", other INSERTs, UPDATEs and others (like in PDO extension).

If you want to keep your function, add a IF statement around the loop checking the type of $results (with a is_resource() for example)

swordofpain
  • 149
  • 4
0

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.

<?php 
    public function insert($employee){ 

        $sqlQuery = "INSERT INTO employee(user_id,name,address,city) VALUES(:user_id,:name,:address,:city) RETURNING employee_id"; 

        $statement = $this->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

shamsieh76
  • 123
  • 1
  • 7
  • Just going to point out for future viewers: The query in this answer uses the `RETURNING` clauses which is a SQL Server feature, not available in MySQL which is specified in the question above. – Issung Nov 24 '22 at 03:48