2013-02-05 13 views
5

के साथ त्रुटि मैंने बहु_क्विरी का उपयोग करने का प्रयास किया है, लेकिन मुझे सख्त मानक संदेश पॉप-अप करना जारी रहता है।सख्त मानकों: mysqli_neult_result() mysqli_multi_query

$querystring = "INSERT INTO responses VALUES('1', '2', '3', '4'); INSERT INTO responses VALUES('1', '2', '3', '4')"; 

if (mysqli_multi_query($db, $querystring)) { 
    do { 
     if ($result = mysqli_store_result($db)) { 
      // 
     } 
    } while (mysqli_next_result($db)); 
} 
echo "end"; 

त्रुटि संदेश मैं मिलता है:

सख्त मानकों: mysqli_next_result(): कोई अगले परिणाम सेट है। कृपया, फोन mysqli_more_results()/mysqli :: more_results() इस समारोह/विधि

मैं जोड़ने और -; को दूर करने की कोशिश की, लेकिन कोई भाग्यशाली रहे हैं कॉल करने के लिए है कि क्या जांच करने के लिए।

+2

"मैं जोड़ने की कोशिश की है और निकालना" आप भी करने की कोशिश की ... hhmmmm। iduno 'mysqli :: more_results()' के लिए एक चेक जोड़ रहा है? – PeeHaa

+0

यह बहुत बेकार चेतावनी है। असल में यह आपको यूग्लियर कोड लिखने के लिए कहता है। इस चेतावनी के पीछे क्या कारण है? – Calmarius

उत्तर

19

जबकि पाइपोडिगिन ने $ querystring के भीतर त्रुटि को सही किया और समस्या को कम किया, वास्तविक समाधान कठोर मानकों त्रुटि के संबंध में प्रदान नहीं किया गया था।

मैं सरबेट की सलाह से असहमत हूं, डीओ WHILE से बदलना आवश्यक नहीं है।

आपको प्राप्त होने वाले सख्त मानक संदेश काफी जानकारीपूर्ण हैं। का पालन करने के लिए, इस का उपयोग करें:

do{} while(mysqli_more_results($db) && mysqli_next_result($db)); 

फिर, कोई जरूरत नहीं है आप एक सशर्त बाहर निकलने के बारे में या लूप के अंदर तोड़ने के लिए है क्योंकि, जबकि हालत में कोई त्रुटि की पहली आवृत्ति पर पाश टूट जाएगा। * ध्यान दें, यदि पहली क्वेरी में कोई त्रुटि है तो डू-टाइम से पहले अगर कथन लूप में प्रवेश से इंकार कर देगा।

आपके उदाहरण में, आप केवल INSERT क्वेरी चला रहे हैं, इसलिए आपको प्रक्रिया के लिए कोई परिणाम सेट नहीं मिलेगा। यदि आप गिनना चाहते हैं कि आपने कितनी पंक्तियां जोड़ दी हैं, तो mysqli_affected_rows() का उपयोग करें।

अपने प्रश्न के लिए एक पूर्ण समाधान के रूप में:

if(mysqli_multi_query($db,$querystring)){ 
    do{ 
     $cumulative_rows+=mysqli_affected_rows($db); 
    } while(mysqli_more_results($db) && mysqli_next_result($db)); 
} 
if($error_mess=mysqli_error($db)){echo "Error: $error_mess";} 
echo "Cumulative Affected Rows: $cumulative_rows"; 

आउटपुट:

// if no errors 
Cumulative Affected Rows: 2 

// if error on second query 
Error: [something] 
Cumulative Affected Rows: 1 

// if error on first query 
Error: [something] 
Cumulative Affected Rows: 0 

देर संपादित करें:

के बाद से mysqli के लिए नए लोगों पर पहुंचने रहे हैं इस प्रो ost, मैं multi_query() का उपयोग करके परिणाम सेट के बिना/बिना प्रश्नों को संभालने के लिए एक सामान्य लेकिन मजबूत स्निपेट प्रदान करूंगा और यह दिखाने के लिए एक सुविधा जोड़ूं कि सरणी में कौन सी क्वेरी को संभाला जा रहा है ...

क्लासिक "यदि() {डीओ {} जबकि}" सिंटेक्स:

if(mysqli_multi_query($mysqli,implode(';',$queries))){ 
    do{ 
     echo "<br><br>",key($queries),": ",current($queries); // display key:value @ pointer 
     if($result=mysqli_store_result($mysqli)){ // if a result set 
      while($rows=mysqli_fetch_assoc($result)){ 
       echo "<br>Col = {$rows["Col"]}"; 
      } 
      mysqli_free_result($result); 
     } 
     echo "<br>Rows = ",mysqli_affected_rows($mysqli); // acts like num_rows on SELECTs 
    } while(next($queries) && mysqli_more_results($mysqli) && mysqli_next_result($mysqli)); 
} 
if($mysqli_error=mysqli_error($mysqli)){ 
    echo "<br><br>",key($queries),": ",current($queries),"Syntax Error:<br>$mysqli_error"; // display array pointer key:value 
} 
//if you want to use the snippet again... 
$mysqli_error=null; // clear variables 
reset($queries); // reset pointer 

Reinvented व्हील "जबकि {}" सिंटेक्स नहीं (... उन लोगों के लिए जो कर बाद परीक्षण की तरह छोरों):

while((isset($multi_query) && (next($queries) && mysqli_more_results($mysqli) && mysqli_next_result($mysqli))) || (!isset($multi_query) && $multi_query=mysqli_multi_query($mysqli,implode(';',$queries)))){ 
    echo "<br><br>",key($queries),": ",current($queries); // display array pointer key:value 
    if($result=mysqli_store_result($mysqli)){ 
     while($rows=mysqli_fetch_assoc($result)){ 
      echo "<br>Col = {$rows["Col"]}"; 
     } 
     mysqli_free_result($result); 
    } 
    echo "<br>Rows = ",mysqli_affected_rows($mysqli); // acts like num_rows on SELECTs 
} 
if($mysqli_error=mysqli_error($mysqli)){ 
    echo "<br><br>",key($queries),": ",current($queries),"Syntax Error:<br>$mysqli_error"; // display array pointer key:value 
} 
//if you want to use the snippet again... 
$multi_query=$mysqli_error=null; // clear variables 
reset($queries); // reset pointer 

तो, या तो निम्नलिखित प्रश्नों में एक ही उत्पादन की पेशकश करेगा दिया स्निपेट:

क्वेरी सरणी:

$queries[]="SELECT * FROM `TEST`"; 
$queries[]="INSERT INTO `TEST` (Col) VALUES ('string1'),('string2')"; 
$queries[]="SELECT * FROM `TEST`"; 
$queries[]="DELETE FROM `TEST` WHERE Col LIKE 'string%'"; 

आउटपुट:

0: SELECT * FROM `TEST` 
Rows = 0 

1: INSERT INTO `TEST` (Col) VALUES ('string1'),('string2') 
Rows = 2 

2: SELECT * FROM `TEST` 
Col = string1 
Col = string2 
Rows = 2 

3: DELETE FROM `TEST` WHERE Col LIKE 'string%' 
Rows = 2 

अपनी आवश्यकताओं के अनुसार मेरी के टुकड़े को संशोधित करें। यदि आप एक बग खोजते हैं तो एक टिप्पणी छोड़ दें।

1

आपको यह चेतावनी क्यों मिलती है, इसलिए आप do ... while लूप का उपयोग करते हैं जो कमांड ब्लॉक चलाने के बाद स्थिति का मूल्यांकन करता है। तो जब कोई और नतीजा नहीं होता है, तो लूप की सामग्री एक अतिरिक्त समय तक चलती है, जिससे वह चेतावनी मिलती है।

while ($mysql->next_result()) का उपयोग करके ... do लूप को इसे ठीक करना चाहिए। (एक सामान्य नोट पर: आपके द्वारा पोस्ट-टेस्ट लूप का उपयोग करना डेटाबेस प्रोग्रामिंग में काफी असामान्य है)

यदि कोड कविता है, तो मैं शेक्सपियर बनने की कोशिश कर रहा हूं!

0

(ओपी की तरफ से उत्तर पोस्ट)

हल:

$querystring = "INSERT INTO responses VALUES('1', '2', '3', '4'); INSERT INTO responses VALUES('1', '2', '3', '4')"; 

if (mysqli_multi_query($db, $querystring)) { 
do { 
    if (!mysqli_more_results($db)) { 
    exit(); 
    } 
} while (mysqli_next_result($db)); 
} 
echo "end"; 
संबंधित मुद्दे