2016-09-04 64 views
0

這是我的代碼鎖定表之後,我得到警告

 $ar = ($_POST['ar']); 
     $query = "lock table stock write"; 
     $res = mysqli_query ($db, $query); 

     /*******Count******/ 
     $query = "SELECT SUM(quantita_vendita) AS somma FROM stock"; 
     $res = mysqli_query ($db, $query); 
     $row = mysqli_fetch_array($res); 
     $somma = $row['somma']; 

      if($ar<=$somma){ 
       $query = "SELECT * FROM user, info WHERE info.id_user = user.id and user.email = '{$_SESSION['email']}'"; 
       $res = mysqli_query ($db, $query); 

         if(mysqli_num_rows($res) > 0) { 
           $row = mysqli_fetch_assoc($res); 
           $azioni = $row['azioni']; 
           $denaro = $row['denaro']; 
           $id = $row['id']; 
           mysqli_free_result($res); 
         } 

如果不插入鎖,它的工作原理。 相反當我插入鎖時,我有一個警告(mysqli_num_rows()預計參數1被mysqli_result,布爾中給出..)爲if(mysqli_num_rows($res) > 0)

回答

2

當您使用表鎖定,你必須鎖定你將在這屆會議的任何查詢中使用的所有表(或再次解鎖),見LOCK TABLES and UNLOCK TABLES Syntax

需要鎖的會話必須在單個LOCK TABLES語句中獲取它需要的所有鎖。當這樣獲得的鎖被保持時,會話只能訪問鎖定的表。

mysql> LOCK TABLES t1 READ; 
mysql> SELECT COUNT(*) FROM t1; 
+----------+ 
| COUNT(*) | 
+----------+ 
|  3 | 
+----------+ 
mysql> SELECT COUNT(*) FROM t2; 
ERROR 1100 (HY000): Table 't2' was not locked with LOCK TABLES 

所以你的情況,查詢使用表user和:例如,在下面的語句序列,因爲它是在LOCK TABLES語句不鎖定爲試圖訪問T2發生錯誤info失敗。爲解決這個問題,使用例如

lock tables stock write, user write, info write; 

你有你的錯誤消息,因爲查詢失敗,從而$res是假的,而不是一個mysqli_result -object,因此mysqli_num_rows($res)不起作用。在使用結果之前,您應該始終檢查查詢是否失敗,例如使用

if ($res = mysqli_query($db, $query)) { 
    if (mysqli_num_rows($res) > 0) {... } 
} else { 
    printf("Error: %s\n", mysqli_error($db)); 
} 
+0

解決,謝謝! – Peppifg

0

根據手動http://php.net/manual/en/mysqli.query.php

mysqli_query返回FALSE失敗。對於成功的SELECT,SHOW,DESCRIBE或 EXPLAIN查詢,mysqli_query()將返回一個mysqli_result對象。對於 其他成功的查詢mysqli_query()將返回TRUE。

所以不是if(mysqli_num_rows($res) > 0)你應該使用if ($res)

+0

我試過了,但它不給我$ denaro – Peppifg

+0

這意味着$ res是false並且你的查詢失敗。嘗試找到mysqli_errno錯誤@Peppifg –

+0

我試圖直接查詢數據庫,查詢工作 – Peppifg

相關問題