Reputation: 181
How do I use $objects
in this function?
I pass data from database in array form, then I want to show this array in PHP page, but it's printing this word "array".
I want to print it in this format--> [ ['Trident','Internet Explorer 4.0','Win 95+','4','X'], ['Trident','Internet Explorer 5.0','Win 95+','5','C'], ['Trident','Internet Explorer 5.5','Win 95+','5.5','A'] ] The code:
function showing_daily_basket(){
$connect_mysql= @mysql_connect($server,$username,$passwor) or die ("Connection Failed!");
$mysql_db=mysql_select_db("GP15",$connect_mysql) or die ("Could not Connect to Database");
$query = "SELECT * FROM basket_daily_work";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
$objects= array();
while($rows=mysql_fetch_array($result))
{
$objects[]= $rows;
}
exit($objects);
}
and if i paste this var_dump($objects);' after while loop this the result Blockquote
AFTER edit it's return nothing
$connect_mysql= @mysql_connect($server,$username,$passwor) or die ("Connection Failed!");
$mysql_db=mysql_select_db("GP15",$connect_mysql) or die ("Could not Connect to Database");
$query = "SELECT * FROM basket_daily_work";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
$objects= array();
while($rows=mysql_fetch_array($result))
{
$objects[]= $rows;
}
var_dump($objects);
$data_set = "[";
$count_rows = count($objects);
$count = 1;
foreach($objects as $object){
$data_set .= "['". $object['basketID'] ."', '". $object['date'] ."', '". $object['time'] ."', '". $object['flag'] ."']";
if($count != $count_rows){
$data_set .= ",";
$count++;
}
}
$data_set .= "]";
echo $data_set;
Upvotes: 0
Views: 189
Reputation: 1014
Please try print_r($objects).
"print_r — Prints human-readable information about a variable" - Refer to PHP documentation
Upvotes: 1
Reputation: 864
There will be an array within $objects
for each row that was returned in your query. So you can loop through these and then do something with the results.
foreach($objects as $object){
echo $object['column_name'];
}
This code will echo out the value of the column name specified for each row returned.
A good way to see the structure of $objects before doing something with the results is
print "<pre>";
var_dump($objects);
EDIT - Try this
$data_set = "[";
$count_rows = count($objects);
$count = 1;
foreach($objects as $object){
$data_set .= "['". $object['basketID'] ."', '". $object['date'] ."', '". $object['time'] ."', '". $object['flag'] ."']";
if($count != $count_rows){
$data_set .= ",";
$count++;
}
}
$data_set .= "]";
Upvotes: 1