2015-07-11 52 views
3

我不是程序員,我知道很少PHP,但我試圖修復一個店內自從一個代碼是給在PHP 5.4以下錯誤:轉換mysql_result到mysqli的

mysql_result( ):提供的參數不是一個有效的MySQL結果資源

這是代碼:

$products = $cart->get_products(); 
    for ($i=0, $n=sizeof($products); $i<$n; $i++) { 
    $id_produto = (INT)$products[$i]['id']; 
    $sql = tep_db_query("SELECT p.manufacturers_id,m.manufacturers_cep,m.manufacturers_name FROM products p 
    LEFT JOIN manufacturers m ON m.manufacturers_id = p.manufacturers_id 
    WHERE p.products_id = '$id_produto'")OR DIE(mysql_error()); 
    $id_fabricante = mysql_result($sql,'0','manufacturers_id'); 
    $cep_fabricante = mysql_result($sql,'0','manufacturers_cep'); 
    $nome_fabricante = mysql_result($sql,'0','manufacturers_name'); 

    $id_fabricantes[$id_fabricante]['peso'] += $products[$i]['quantity']*$products[$i]['weight']; 
    $id_fabricantes[$id_fabricante]['cep'] = $cep_fabricante; 
    $id_fabricantes[$id_fabricante]['nome'] = $nome_fabricante; 

    } 

我試圖改變它,有沒有更多的錯誤,但它仍然不是加工。這是做到這一點的正確方法嗎?

$products = $cart->get_products(); 
for ($i=0, $n=sizeof($products); $i<$n; $i++) { 
$id_produto = (INT)$products[$i]['id']; 
$sql = tep_db_query("SELECT p.manufacturers_id,m.manufacturers_cep,m.manufacturers_name FROM products p 
LEFT JOIN manufacturers m ON m.manufacturers_id = p.manufacturers_id 
WHERE p.products_id = '$id_produto'")OR DIE(mysql_error()); 

$row = mysqli_fetch_assoc($sql); 
$id_fabricante = $row['manufacturers_id']; 

$row = mysqli_fetch_assoc($sql); 
$cep_fabricante = $row['manufacturers_cep']; 

$row = mysqli_fetch_assoc($sql); 
$nome_fabricante = $row['manufacturers_name']; 

$id_fabricantes[$id_fabricante]['peso'] += $products[$i]['quantity']*$products[$i]['weight']; 
$id_fabricantes[$id_fabricante]['cep'] = $cep_fabricante; 
$id_fabricantes[$id_fabricante]['nome'] = $nome_fabricante; 

} 
+0

你不能混合使用'mysql'函數和'mysqli',但以任何方式我們需要看到'tep_db_query'函數 –

回答

1

不,這是不正確的。如果您檢查manual,您會看到第二個參數是要在結果集中提取的行。在您的原始示例中,您僅從第一行提取數據 - 0 - 沒有別的。

在你的mysqli代碼中,你在每次賦值之前取一個新行,這樣數據將是來自不同行的不同字段值的混合。

正確的方法是這樣的:

// fetch the first row in the result set 
$row = mysqli_fetch_assoc($sql); 

$id_fabricante = $row['manufacturers_id']; 
$cep_fabricante = $row['manufacturers_cep']; 
$nome_fabricante = $row['manufacturers_name']; 

除此之外,你將需要添加的錯誤處理,以確保有一排。

您還應該嘗試避免在循環中運行sql查詢。您可能可以使用例如mysql的IN子句在1個查詢中獲取所有行,然後您可以遍歷該結果集。

+1

很棒!很多!這就像一個魅力! :D 謝謝你的提示,但恐怕我不能在編程中走得太遠。我只需要解決這個問題。 ;) – Patty