For some reason, my PHP displays only the first value from the database, how to solve it?

$mysql = mysqli_connect($c['host'], $c['user'], $c['pass'], $c['db']); $sql=mysqli_query($mysql,"SELECT * FROM dle_users WHERE user_id = '".$_COOKIE['dle_user_id']."'"); while($row=mysqli_fetch_array($sql)){ $pro = mysqli_connect($c['host'], $c['user'], $c['pass'], $c['db']); $prod=mysqli_query($pro, "SELECT * FROM dle_products "); while($pdo = mysqli_fetch_array($prod, MYSQLI_BOTH)){ if(file_exists('buy/mc/'.$pdo['item_id'].'.png')){ $img = 'buy/mc/'.$pdo['item_id'].'.png'; } else { $img = 'buy/mc/unknown.png'; } $image = '<img src="'.$img.'" alt="'.$pdo['name'].'">'; echo' <i class="fa fa-info" aria-hidden="true" title="'.$pdo['description'].'"></i> '.$pdo['name'].'<br /> '.$image.'<br /> '.$pdo['price'].' '.$valet.' за '.$pdo['amount'].' шт'; } mysqli_close($pro); } mysqli_close($mysql); 

The code seems to be correct, but all the same it displays only the first value from the database

  • mysqli_query($mysql,"SELECT * FROM dle_users WHERE user_id = '".$_COOKIE['dle_user_id']."'"); You can not substitute the values ​​that came from the user directly to the request. It is necessary to use prepared queries. $pro = mysqli_connect($c['host'], $c['user'], $c['pass'], $c['db']); to make the second connection in this case is senseless and merciless. while($row=mysqli_fetch_array($sql)){ value of $row not used anywhere. Check for the existence of the user is carried out a little differently. mysqli_close($pro); It is not necessary to close the connection (except in rare cases). - ArchDemon
  • Corrected all his bugs - Nikita Cherepko
  • But, all the same, only the first entry is output - Nikita Cherepko
  • Update your question with the corrected code. mysqli_num_rows($prod); value of mysqli_num_rows($prod); . What does it matter? - ArchDemon
  • one
    because user_id is only one in the database, most likely, and there is no cycle to extract from $ prod - Jean-Claude

1 answer 1

mysqli_query($mysql,"SELECT * FROM dle_users WHERE user_id = '".$_COOKIE['dle_user_id']."'"); You can not substitute the values ​​that came from the user directly to the request. It is necessary to use prepared queries. $pro = mysqli_connect($c['host'], $c['user'], $c['pass'], $c['db']); to make the second connection in this case is senseless and merciless. while($row=mysqli_fetch_array($sql)){ value of $ row is not used anywhere. Check for the existence of the user is carried out a little differently. mysqli_close($pro); it is not necessary to close the connection (except in rare cases)