How to use mysql_result with MySQLi?

2

I would like to know how to use mysql_result in mysqli I'm not getting it, I was looking at the php.net site and the only thing I saw was mysqli_data_seek if anyone can help me ...

Code I'd like to pass to mysqli :

$sql = mysqli_query($link, "SELECT * FROM produtos WHERE id_produto = $id");
if($id!=""){
     $descricao = mysql_result($sql,0,"descricao");
     $nome = mysql_result($sql,0,"nome");
     $preco = mysql_result($sql,0,"preco");
     $img = mysql_result($sql,0,"img");
}
    
asked by anonymous 18.12.2015 / 01:59

1 answer

2

There is no function called mysqli_result() , use mysqli_fetch_*() instead, this eliminates all of these variable assignments. Avoid sql injections using prepared statements.

With prepared statements you need to define the types of parameters sent in the query, mysqli_stmt_bind_param() does this, mysqli_stmt_execute() executes the query in the database, mysqli_stmt_get_result() gets the database response and mysqli_fetch_assoc() takes that information and returns it in the form of an associative array, this code is in the procedural style.

$sql = "SELECT * FROM produtos WHERE id_produto = ?";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, 'i', $id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result)){
    echo $row['nome'] .' - '. $row['descricao'] .' - '. $row['preco']. '<br>';
}
    
18.12.2015 / 02:13