2012-06-07 16 views
6

यह संभव दो mysqli_queries इतना ?:दो 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()); 

की तरह है करने के लिए मूल रूप से मैं अपने DB में दो तालिकाओं को अपडेट करना चाहते है। क्या ऐसा करने के लिए इससे अच्छा तरीका है?

+1

संभव 2 प्रश्नों को चलाने के लिए, अच्छी तरह से निश्चित रूप से यह है। –

+0

आप कहते हैं कि यह संभव है लेकिन उपर्युक्त काम नहीं कर रहा है। यह केवल छवियों में डालने वाला है लेकिन images_history में नहीं। – PartisanEntity

+0

अच्छी तरह से आपको मुद्दों के लिए विशेष क्वेरी की जांच करनी चाहिए, इसका कोई अन्य प्रश्न नहीं है। –

उत्तर

23

mysqli_multi_query() के साथ यह संभव है।

उदाहरण:

<?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); 
} 

कुंजी है कि आप चाहिए उपयोग mysqli_multi_query यदि आप एक ही कॉल में एक से अधिक क्वेरी निष्पादित करने के लिए चाहते हैं। सुरक्षा कारणों से, mysqli_query एसक्यूएल इंजेक्शन को रोकने के लिए कई प्रश्न निष्पादित नहीं करेगा।

mysqli_store_result के व्यवहार को भी ध्यान में रखें। यह FALSE देता है यदि क्वेरी का कोई परिणाम सेट नहीं है (जो INSERT क्वेरी नहीं करता है) तो आपको यह देखने के लिए mysqli_error भी जांचना होगा कि यह एक खाली स्ट्रिंग देता है जिसका अर्थ है INSERT सफल था।

देखें:
mysqli_multi_query
mysqli_more_results
mysqli_next_result
mysqli_store_result

+0

बिल्कुल सही धन्यवाद, यह वही दिशा है जिसे मैं इसे लेना चाहता हूं। – PartisanEntity

+2

मैं यह पुराना है, लेकिन जो भी अभी भी mysqli_multi_query का उपयोग कर रहा है, @ dre010 के रूप में समझाएं: _ सुरक्षा कारणों से, mysqli_query SQL इंजेक्शन_ को रोकने के लिए एकाधिक क्वेरी निष्पादित नहीं करेगा।तो मुझे लगता है कि आपको अपनी क्वेरी को कई अलग-अलग क्वेरी में अलग करने और तैयार कथन – David

+0

का उपयोग करने पर विचार करना चाहिए, मैं इसके बारे में उलझन में हूं। मैं इसे अनुकूलित करने की कोशिश कर रहा हूं ताकि मैं कह सकूं, 'यदि पहली तालिका खोजकर एक स्ट्रिंग पाई जाती है, तो परिणाम लौटाएं। यदि यह पहली तालिका खोजकर नहीं मिलता है, और यह दूसरी तालिका को खोजकर नहीं मिलता है, तो उसे दूसरी तालिका में डालें। ' तो पहले चयन कथन चलाएं, लेकिन अगर पहले कोई परिणाम न लौटाता है तो केवल दूसरा चलाएं। क्या SELECT कथन के बीच अंतर करने का कोई तरीका है जिसे आप mysqli_multi_query के भीतर उपयोग करना चाहते हैं? –

0

एक बार और सभी के लिए! अपनी स्क्रिप्ट में कहीं भी असीमित संख्या में प्रश्नों के परिणाम प्राप्त करने के लिए इस फ़ंक्शन का उपयोग करें।

फंक्शन:

आप बस कार्य करने के लिए बहु क्वेरी के उत्पादन में गुजरती हैं और यह सब परिणाम और प्रत्येक प्रश्न में पाया त्रुटियों देता है।

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; 
    } 

उपयोग:

$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); 

आउटपुट

$ उत्पादन 2 सरणियों "परिणाम" और "त्रुटि" क्वेरी द्वारा आदेश दिया भी शामिल है। उदाहरण के लिए, आप जब तीसरे क्वेरी को निष्पादित करता है, तो किसी भी त्रुटि हुआ है की जाँच करें और उसके परिणाम लाने के लिए की जरूरत है, तो आप कर सकते हैं:

if(isset($output['error'][2]) && $output['error'][2] !== ""){ 
    echo $output['error'][2]; 
}else{ 
    while($row = $output['result'][2]->fetch_assoc()) { 
    print_r($row); 
    } 
} 
संबंधित मुद्दे