Thursday, September 16, 2010

Run multiple SQL query in PHP when using SQL variables

Some times you may need to run mulitple query in PHP to obtain the query result you want. eg

SELECT `column` INTO @column FROM table LIMIT 1;SET @query = CONCAT("SELECT DISTINCT table2.",@column,"  FROM table2");PREPARE stmt FROM @query;EXECUTE stmt;

Here is an example function

function doMultiQuery{if(mysqli_multi_query($con, $query)){  do {     if ($result = mysqli_store_result($con)) {           if(mysqli_num_rows($result)>0)           {                 return $result;           }           @mysqli_free_result($con);     }   } while (@mysqli_next_result($con));}}

No comments:

Post a Comment