MySQL query in PHP with two SQL statements?

I have the following $sql statement in a PHP script:
is it possible to include a Truncate command on a tables TABLE1,TABLE2 within this bit of code prior to running the INSERT statement?  I basically need to empty some tables before I INSERT.  How do I best do this?

$sql = "INSERT INTO simages (sImage_Name) VALUES ('".$record[$primary_key]."_".$record[$mls_key]."')";

Open in new window

pda4meAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I wonder why you want to truncate (delete) 2 tables just before you insert 1 record.

function setupRecord($resource,$record,$mls_key) {
      global $primary_key;
      global $photos_process;
      global $conn;
      
      
      if (checkDuplicates($resource,$record[$primary_key]))
          $ret = updateRecord($resource,$record);
      else {
          $ret = insertRecord($resource,$record);

            $sql = "truncate table table1";
            h_query($sql, $conn);             

            $sql = "truncate table table2";
            h_query($sql, $conn);             

            $sql = "INSERT INTO simages (sImage_Name) VALUES ('".$record[$primary_key]."_".$record[$mls_key]."')";
            h_query($sql, $conn);             

    }      
}

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I would rather run the statments apart ... why do you want to run this on a "single" $sql?
0
 
Dave BaldwinFixer of ProblemsCommented:
The PHP/MySQL driver won't allow you to combine queries into one single statement.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
pda4meAuthor Commented:
I am not 100% where to call the new query...I am a newbie with this:

Ideally I would like to create a new function at the very top to empty the tables before the rest of the script runs, can you show me how to do that?

function setupRecord($resource,$record,$mls_key) {
      global $primary_key;
      global $photos_process;
      global $conn;
      
      
      if (checkDuplicates($resource,$record[$primary_key]))
          $ret = updateRecord($resource,$record);
      else {
          $ret = insertRecord($resource,$record);
            $sql = "INSERT INTO simages (sImage_Name) VALUES ('".$record[$primary_key]."_".$record[$mls_key]."')";
          
            
            h_query($sql, $conn);             

    }      
}
0
 
Dave BaldwinFixer of ProblemsCommented:
0
 
pda4meAuthor Commented:
Perfect, thanks for the help guys!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.