The while loop in the function while() function in php
Hello everyone.
I apologize for such a stupid question:
how can I put a while loop in a function if the loop accesses the database?
For example:
<?php
$all = mysqli_query($connect, "SELECT * FROM article") or die("Ошибка");
function all_list(){
while($row = mysqli_fetch_assoc($all)){
echo $row['id'];
}
}
echo all_list();
?>
0
2 answers
Pass $all as a function parameter:
function all_list($all){}
And call the function with passing the parameter
echo all_list($all);
1
Author: Get, 2014-11-23 19:10:14
In general, you can not pass any parameters to mysqli_fetch_assoc(), it already knows what to pull out.
It is better to write this way (if you really want to with the parameter:
<?php
function all_list($connect, $table){
$rows = array();
$all = mysqli_query($connect, "SELECT * FROM ".$table) or die("Ошибка");**
while($rows[] = mysqli_fetch_assoc($all)){
//echo $row['id']; //нехорошо :-)
}
return $rows;
}
var_dump(all_list($connection, 'article'));
0
Author: StTv, 2014-11-24 08:08:13