1
I am facing problems when trying to pass the result of mysqli_query by globals, because I can only print on screen data from the first row of the table. Follows the code:
function of
class
select:
public function select($dataset_name,$entire = True){
$table = $this->get_table();
$db_connection = $this->get_db_connection();
if ($entire == True) {
$GLOBALS[$dataset_name] = mysqli_query($db_connection,"SELECT * FROM {$table}");
}else {
$array = func_get_args();
unset($array [0]);
unset($array [1]);
$columns = implode(",", $array);
$GLOBALS[$dataset_name] = mysqli_query($db_connection,"SELECT {$columns} FROM {$table}");
}
}
function responsible for printing the result (PS: It is unfinished, because missing format the data in a table)
<?php
function list_dataset($dataset_name){
$num_columns = mysqli_field_count($GLOBALS['connection']) - 1;
$count = 0;
while ($array = mysqli_fetch_array($GLOBALS[$dataset_name])) {
while ($count <= $num_columns ) {
echo $array[$count].'</br>';
$count = $count + 1;
}
}
}
?>
The result of inserting
print_r(array_keys($GLOBALS)); exit;
before the functionmysqli_fetch_array()
is:
Array ( [0] => _GET [1] => _POST [2] => _COOKIE [3] => _FILES [4] => GLOBALS [5] => db_connection [6] => db_action [7] => mysqli [8] => connection [9] => con [10] => teste [11] => select )
Other information:
$GLOBALS['connection']
and$GLOBALS[$dataset_name]
coincide, as only a connection was made.The function
list_dataset()
has to suit anyselect
, otherwise it loses its meaning completely.I call it that:
$db_connection = new db_connection; $db_connection->set_all('localhost','usuário','senha','db'); $db_connection->open(); $db_action = new db_action; $db_action->set_all('tabela_usuarios'); $db_action->select('teste'); $db_action->list_dataset('teste');
Doubt: What am I doing wrong? Am I putting some part of the code in the wrong place?
The code is susceptible to inconsistencies. Example, in the same routine has
$GLOBALS['connection']
and$GLOBALS[$dataset_name]
. If$dataset_name
do not match'connection'
may be a cause of the error. But there may be other miscellaneous reasons. To be more objective, try a breakpoint before callingmysqli_field_count()
. Do so:print_r(array_keys($GLOBALS)); exit;
and put in question the result of this test.– Daniel Omine
@Danielomine ready, I added the result! As for connection conflicts, only has the
'connection'
really! Thank you, from now on!– Rafaelfdib
It would be good if you show the sample code in which you are calling the functions for testing.
– Bacco
Rafael, there is no need to put "SOLVED" in the title of the question. You could post the solution as an answer, but as the question is closed, the most that can be done is you follow the yellow box guidelines so that the question is reopened and then yes post the solution as an answer.
– user28595
@diegofm right, thanks for the warning. I no longer see what to make the question clearer. If I can’t see where I’m going wrong, please let me know. I put the solution in the question, trying to make it as clear as possible and to show that the initial hypothesis was wrong. I don’t want the topic to be useless to the community, I know the error is foolish, but maybe it helps someone with similar error.
– Rafaelfdib
Or leave the solution comment. If you think the solution can help someone in the future, edit the question to make it clearer. And then try to get it reopened.
– Jorge B.
@Jorgeb. Let’s see if it was clear, then I put in the answers the solution. Thank you!
– Rafaelfdib