How to use mysql_result with Mysqli?

Asked

Viewed 1,572 times

2

I’d like to know how to use the mysql_result in the mysqli I’m not getting it, I was looking at the site php.net and the only thing I saw was a 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");
}
  • It is impossible! this function does not exist, needs to work differently

  • I saw this mysli_data_seek rray but n intendi very well it

  • this would be the new method that is equal to mysql_result?

  • You can use it like this => Select with Prepared statements Mysqli

  • in my case then I would have to use the bind that Voce spoke on the topic?

  • Yes, this avoids sql Injection

  • Would it look like this in mine? $list = array(); $i = 0; while($query->fetch()){ $list[$i]['Description'] = $Description; $list[$i]['name'] = $name; $list[$i]['price'] = $price; $list[$i]['img'] = $img; $i++; Here would enter the print that has inside my if? and also I forget my if? }

Show 2 more comments

1 answer

2


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

With Prepared statements you need to define the parameters types sent in the query, mysqli_stmt_bind_param() does that, mysqli_stmt_execute() executes the query in the bank, mysqli_stmt_get_result() get the seat reset and mysqli_fetch_assoc() takes this infomation and returns 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>';
}
  • another Erg that came up in my case I would have to show the name of the respective as well as the img and the Description so type inside the echo that I want to exivir the name I would put echo $Row['name'];?

  • @Leonardocosta

  • but I did so and can not call the product and agr tried otherwise and does not call the product tbm type there in my getch in the url it is passing the right id but it does not call the price img the description of the product

  • opss sorry for the comment kkkkkkk had a small error connected with the bank was not calling sorry

  • now was obg by the answer rray

Browser other questions tagged

You are not signed in. Login or sign up in order to post.