转换mysql_result到mysqli的
问题描述:
我不是程序员,我知道很少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;
}
答
不,这是不正确的。如果您检查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
你不能混合使用'mysql'函数和'mysqli',但以任何方式我们需要看到'tep_db_query'函数 –