2012-06-07 19 views
6

mümkün Temelde benim DB iki tablo güncellemek istiyorum böylece ?: İki mysqli

mysqli_query($dblink, "INSERT INTO images (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName')") or die(mysql_error()); 
          mysqli_query($dblink, "INSERT INTO images_history (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name, day, month, year) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName', '$day', '$month', '$year')") or die(mysql_error()); 

gibi iki mysqli_queries olması mı sorgular. Bunu yapmanın daha iyi bir yolu var mı?

+1

2 sorguyu çalıştırmak mümkündür, elbette öyle. –

+0

Bunun mümkün olduğunu söylüyorsunuz, ancak yukarıdakiler çalışmıyor. Sadece görüntülere yerleştirilir, ancak images_history içine değil. – PartisanEntity

+0

belki de sorunlar için belirli bir sorguyu kontrol etmelisiniz, başka bir sorgu ile ilgisi yoktur. –

cevap

23

mysqli_multi_query() ile mümkündür.

Örnek:

<?php 

$mysqli = new mysqli($host, $user, $password, $database); 

// create string of queries separated by ; 
$query = "INSERT INTO images (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName');"; 
$query .= "INSERT INTO images_history (project_id, user_id, image_name, date_created, link_to_file, link_to_thumbnail, given_name, day, month, year) VALUES ('$project_id', '$user_id', '$image_name', '$date_created', '$link_to_file', '$thumbnail', '$ImageName', '$day', '$month', '$year');"; 

// execute query - $result is false if the first query failed 
$result = mysqli_multi_query($mysqli, $query); 

if ($result) { 
    do { 
     // grab the result of the next query 
     if (($result = mysqli_store_result($mysqli)) === false && mysqli_error($mysqli) != '') { 
      echo "Query failed: " . mysqli_error($mysqli); 
     } 
    } while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli)); // while there are more results 
} else { 
    echo "First query failed..." . mysqli_error($mysqli); 
} 

anahtarı olduğunu sen zorunluluk tek bir çağrıda birden fazla sorguyu yürütmek isteyen mysqli_multi_query eğer kullanılması. Güvenlik nedenleriyle, mysqli_query SQL enjeksiyonları önlemek için birden çok sorgu yürütmez. Ayrıca, mysqli_store_result'un davranışını da aklınızda bulundurun. FALSE döndürürse, sorguda sonuç kümesi yoksa (INSERT sorguları yoktur),'u da INSERT'un başarılı olduğu anlamına gelen boş bir dize döndürdüğünü kontrol etmeniz gerekir.

bakınız:
mysqli_multi_query
mysqli_more_results
mysqli_next_result
mysqli_store_result

+0

Mükemmel teşekkürler, bu benim bunu yapmak istediğim yön. – PartisanEntity

+2

Bu eski olduğunu, ancak kim hala mysqli_multi_query kullanarak, kim @ dre010 açıklamak: _For güvenlik nedenleriyle, mysqli_query SQL injections_ önlemek için birden çok sorgu yürütmez.Bu yüzden, sorgunuzu birkaç ayrı sorguya ayırmayı ve hazırladığınız – David

+0

ifadesini kullanmanız gerektiğini düşünüyorum. Bunu adapte etmeye çalışıyorum, diyebilirim ki eğer ilk tabloyu arayarak bir string bulunursa, sonuçları iade et. İlk tabloyu arayarak bulunamazsa ve ikinci tabloyu arayarak bulunmazsa, ikinci tabloya yerleştirin. ' Yani ilk SELECT deyimini çalıştırın, ancak ilk sonuç vermezse yalnızca ikinci koşuyu çalıştırın. mysqli_multi_query içinde kullanmak istediğiniz SELECT deyimini ayırt etmenin bir yolu var mı? –

0

kez ve herkes için! Komut dosyanızın herhangi bir yerinde sınırsız sayıda sorgunun sonuçlarını almak için bu işlevi kullanın.

Fonksiyon: Sadece işleve çoklu sorgu çıktısını geçmek ve her sorguda bulunan tüm sonuçları ve hataları döndürür

.

function loop_multi($result){ 
    //use the global variable $conn in this function 
    global $conn; 
    //an array to store results and return at the end 
    $returned = array("result"=>array(),"error"=>array()); 
    //if first query doesn't return errors 
     if ($result){ 
     //store results of first query in the $returned array 
     $returned["result"][0] = mysqli_store_result($conn); 
     //set a variable to loop and assign following results to the $returned array properly 
     $count = 0; 
     // start doing and keep trying until the while condition below is not met 
     do { 
      //increase the loop count by one 
      $count++; 
      //go to the next result 
      mysqli_next_result($conn); 
      //get mysqli stored result for this query 
      $result = mysqli_store_result($conn); 
      //if this query in the loop doesn't return errors 
      if($result){ 
       //store results of this query in the $returned array 
       $returned["result"][$count] = $result; 
      //if this query in the loop returns errors 
      }else{ 
       //store errors of this query in the $returned array 
       $returned["error"][$count] = mysqli_error($conn); 
      } 
     } 
     // stop if this is false 
     while (mysqli_more_results($conn)); 
     }else{ 
     //if first query returns errors 
     $returned["error"][0] = mysqli_error($conn); 
     } 
    //return the $returned array 
    return $returned; 
    } 

Kullanımı:

$ çıkış sorgu tarafından sipariş edilen 2 diziler "sonuç" ve "hata" içerir

$query = "INSERT INTO table1 (attribute1) VALUES ('value1');"; 
$query .= "INSERT INTO table2 (attribute2) VALUES ('value2');"; 
$query .= "SELECT * FROM table3;"; 

//execute query 
$result = mysqli_multi_query($conn, $query); 
//pass $result to the loop_multi function 
$output = loop_multi($result); 

Çıktı. Örneğin, üçüncü sorguyu çalıştırırken ve sonucunu getirirken herhangi bir hata olup olmadığını denetlemeniz gerekirse, aşağıdakileri yapabilirsiniz:

if(isset($output['error'][2]) && $output['error'][2] !== ""){ 
    echo $output['error'][2]; 
}else{ 
    while($row = $output['result'][2]->fetch_assoc()) { 
    print_r($row); 
    } 
}