php - Convert mysql_result to mysqli -


i'm no programmer , know little php, i'm trying fix code in oscommerce store giving following error on php 5.4:

mysql_result(): supplied argument not valid mysql result resource

this code:

$products = $cart->get_products();     ($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 products p     left join manufacturers m on m.manufacturers_id = p.manufacturers_id     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;      } 

i tried change , there no more errors, it's still not working. correct way it?

$products = $cart->get_products(); ($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 products p left join manufacturers m on m.manufacturers_id = p.manufacturers_id 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;  } 

no, not correct. if check manual, see second parameter row fetch in result set. in original example, fetching data first row - 0 - , nothing else.

in mysqli code, fetch new row before every assignment data mix of values of different fields different rows.

the correct way like:

// fetch first row in result set $row = mysqli_fetch_assoc($sql);  $id_fabricante = $row['manufacturers_id']; $cep_fabricante = $row['manufacturers_cep']; $nome_fabricante = $row['manufacturers_name']; 

apart need add error handling make sure there row.

you should try avoid running sql queries in loop. rows in 1 query using example mysql's in clause , loop on result set.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -