I have a class, where i have a function with the following query.
public function count_weight() {
$id = $_SESSION['id'];
$query = $this->db->prepare("SELECT sum( weight ) FROM `fish` WHERE `user_id` = '".$_SESSION['id']."'");
try {
$query->execute();
} catch(PDOException $e) {
die($e->getMessage());
}
return $query->fetchAll();
}
I want to echo out this result on my index-page. Right now it looks like this:
$weight = $users->count_weight();
echo $weight;
Which obviously doesn't work, it only prints out "arraykg".
Any suggestions?
You can't print an array like that. e.g.
$arr = array(1,2,3);
echo $arr;
is going output the literal text Array, because you're using the array in a string context. If you want to print the CONTENTS of the array, you'll need to do something with it, e.g.
echo implode(',', $arr); // prints: 1,2,3
or
print_r($array); // debug dump out of the array
Try this for example:
$weight = $users->count_weight();
foreach($weight as $w)
{
echo($w['id']):
}
$query->fetchAll(); will return an array. Use var_dump() to analyze the array and adjust your variable declaration accordingly.
echo "<pre>";
print_r($weight);
echo "</pre>";
Assuming $weight actually contains data and is not a resource.
$weight is an array of the row that it retrieved. You'll need to echo out that column
echo $weight['weight'];
This may or may not work because of the query. Try changing the query to this:
$query = $this->db->prepare("SELECT sum( weight ) AS weight FROM `fish` WHERE `user_id` = '".$_SESSION['id']."'");
Related
How can i create an array from the result? I would like to use the array in a mysql IN() query.
//$array = array();
$get_subscategoria = mysqli_query($kapcs, "SELECT kat_id FROM termek_kategoria WHERE kat_parent = '$id'");
if(mysqli_num_rows($get_subscategoria) > 0 )
{
while($sub_kat = mysqli_fetch_array($get_subscategoria))
{
echo $sub_kat['kat_id'];
//$array[] = $sub_kat;
}
}
//print_r( $array );
Now, this code gives back 4 row ID, that works okay. I would like an array with these ID-s, like 1,2,3,4.
Instead of:
while($sub_kat = mysqli_fetch_array($get_subscategoria))
{
echo $sub_kat['kat_id'];
//$array[] = $sub_kat;
}
use:
$array = mysqli_fetch_assoc($get_subscategoria);
while($sub_kat = mysqli_fetch_array($get_subscategoria))
{
$array[] = $sub_kat['kat_id'];
}
echo implode(",",$array);
it give a result like 1,2,3,4
For MySQL, also you can use group_concat, only gives you one record:
SELECT GROUP_CONCAT(kat_id) AS kat_id
FROM termek_kategoria
WHERE kat_parent = '$id'
GROUP BY kat_parent
A very nice person on this site helped me with the following script (and it worked a treat)
<?php
$db = new PDO('mysql:host=HOST;dbname=DATABASE', $user, $pass);
$stmt = $db->prepare('
SELECT
yeast,
rating,
description,
weblink,
image,
sideimage
FROM dowdb_yeast_selector
WHERE
fruit = :fruit
ORDER BY
rating DESC
');
$stmt->bindParam(':fruit',$_POST['fruit'],PDO::PARAM_STR,50);
$stmt->execute();
How do I just echo side image (not as part of a while loop)?
I think (?) I need something like echo '$row[sideimage]' // how simple am I
All of the examples I have looked at so far do not fit my needs :-(
Use PDO::fetchAll, for example;
$stmt->execute();
$arrResults = $stmt->fetchAll();
//$arrResults will be multidimensional
//This will echo the first sideimage
echo $arrResults[0]['sideimage'];
If you want to echo all values of sideimage (ie: all rows), you'd have to iterate through the results;
foreach($arrResults as $arrRow) {
echo $arrRow['sideimage'] . PHP_EOL;
}
Links
pdo::fetchAll
You should use loop pdo::fetch()
while($abc = $stmt->fetch())
{
print_r($abc);
}
If you don't want to use loop try pdo::fetchAll()
$data = $stm->fetchAll();
You could use FETCH_ASSOC
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo $res[0]['sideimage'];
or
foreach($res as $key=>$value) {
$image_val = $value['sideimage'];
}
I'm trying to show list of countries by the use of PHP function.
Here's the database value that I'm retrieving on. (cropped)
Here's the function that I'm retrieving the data.
Code:
public function get_countries() {
$list_country = array();
$query = $this->db->prepare("SELECT name FROM country");
$query->execute();
while($r = $query->fetch(PDO::FETCH_ASSOC)) {
$list_country[] = $r['name'];
}
return $list_country;
}
Then here's the code where I echo all the data.
<?php
while (list($country) = $general->get_countries()) {
echo $country;
}
?>
Unfortunately, it just all echo the same value over and over again.
Any solution for this? Like echo-ing all the data instead of the same data over and over again?
<?php
$country_array = $general->get_countries();
foreach ($country_array as $country) {
echo $country;
}
?>
You don't need the while() as you already fetched all countries with get_countries() function. Moreover, with list() you retrieve only the n array elements where n is the number of variables inside list() function.
So, why are you getting every time the same value?
Because you're calling every time a db function for read country content and every time you're retrieving the same value (the first)
$query = $PDO->prepare("SELECT blah FROM table");
$country = array();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$country[] = $row['blah'];
}
That code worked for me. You can set some exception if the query returns null this would suppress your warnings in the while loop.
Hope that helps
I am having an issue with how I am converting my php array into a JSON object. No matter what I try, I either print everything out as multiple objects or it comes out as null.Wrapping it in pre tags, here is the closest that I got it:
My code:
$content = mysqli_query($dbcon,
"SELECT title, last_name AS lastname
FROM revision, field_last_name
WHERE vid = entity_id;"
);
echo "<pre>";
while($row = mysqli_fetch_array($content))
{
print json_encode($row);
print '<br/>';
}
echo "</pre>";
My output:
{"0":"John Apple","title":"John Apple","1":"Apple","lastname":"Apple"}
{"0":"Kumar Patel","title":"Kumar Patel","1":"Patel","lastname":"Patel"}
{"0":"Michaela Quinn","title":"Michaela Quinn","1":"Quinn","lastname":"Quinn"}
{"0":"Peyton Manning","title":"Peyton Manning, MD","1":"Manning","lastname":"Manning"}
{"0":"John Doe","title":"John Doe","1":"Doe","lastname":"Doe"}
{"0":"Jane Lee","title":"Jane Lee","1":"Lee","lastname":"Lee"}
{"0":"Dan McMan","title":"Dan McMan","1":"McMan","lastname":"McMan"}
{"0":"Yu Win","title":"Yu Win","1":"Win","lastname":"Win"}
My two questions are:
1) Why is there a "0":"John Apple" and a "1":"Apple" when all I want is "title":"John Apple" and "lastname":"Apple" in my object?
2) Why is everything displaying as multiple objects?
Thanks!
---EDIT---
$arr = array()
echo "<pre>";
while($row = mysqli_fetch_assoc($content))
{
$arr[] = $row;
}
print $arr;
echo "</pre>";
field_last_name is your table name? can you distinguish each column name prefix by table name like revision.title in your query and get all data in a single array and then json_encode it?
$content = mysqli_query($dbcon,
"SELECT title, last_name AS lastname
FROM revision, field_last_name
WHERE vid = entity_id;"
);
$arr = array();
echo "<pre>";
while($row = mysqli_fetch_assoc($content))
{
$arr[] = $row;
}
print_r(json_encode($arr));
echo "</pre>";
Change this:
while($row = mysqli_fetch_array($content))
{
print json_encode($row);
print '<br/>';
}
To this:
$row = mysqli_fetch_assoc($content);
json_encode($row);
...because you're printing out multiple objects. If you want a single object which is an array, you need to append the results of mysql_fetch_assoc (see other answer covering field names vs positions) to an array, then json_encode the array in one shot. Example:
$myarray = array();
while($row = mysqli_fetch_assoc($content))
{
$myarray[] = $row;
print '<br/>';
}
print json_encode($myarray);
this is my code:
$scontain = "SELECT id FROM voting";
$qcontain = mysql_query($scontain);
$r_idarray = array();
while ($rcontain = mysql_fetch_array($qcontain))
{
$r_idarray[] = $rcontain['id'];
//let's say there are 5 names here//
}
echo $r_idarray;
I was trying to get the whole content of my table 'voting'. Inside the while loop it successfully prints out the whole content of the table which is under the column 'id' but when I tried to echo it outside the while loop it prints 'Array'? Can anyone knows how to solve this. Thank you in advance...
To print the array you can do:
print_r($r_idarray);
instead of
echo $r_idarray;
You have to use print_r($r_idarray) .
echo is used for printing strings.
use print_r for printing arrays.
Use print_r or var_dump for printing the array
Replace:
echo $r_idarray;
With:
print_r($r_idarray);
Or:
var_dump($r_idarray);
If you want to display whole content
SELECT id FROM voting
To
SELECT * FROM voting
And after that you can use print_r($r_idarray);
You can't "echo" an array, you can only "echo" a string. Of course you will see 'Array'.
In order to print out the contents of an array, you either have to iterate through it's contents, or do a "var_dump($array)".
while($val in $array){
echo $val;
}
or:
print_r($array);
or:
var_dump($array);
One thing youe query should be like this to select all columns
$scontain = "SELECT * FROM voting";
You only have to do this
$i=0;
while ($rcontain = mysql_fetch_array($qcontain))
{
$r_idarray[$i] = $rcontain;
$i++;
}
echo '<pre>';
print_r($r_idarray);
And if you need particular columns from result
$i=0;
while ($rcontain = mysql_fetch_array($qcontain))
{
$r_idarray[$i]['id'] = $rcontain['id'];
$r_idarray[$i]['column1'] = $rcontain['column1'];
$r_idarray[$i]['column2'] = $rcontain['column2'];
$r_idarray[$i]['column3'] = $rcontain['column3'];
$i++;
}
echo '<pre>';
print_r($r_idarray);