i have this code :
$result = mysqli_query($conn, "SELECT SUM(angsuran) FROM `laporan` WHERE id_mustahik=".$detail_campaigner->id_mustahik."");
$row = mysqli_fetch_assoc($result);
print_r($row);
when i run this code, the result is :
Array ( [SUM(angsuran)] => 30000 )
But i want it to display just "30000" number, without "Array ( [SUM(angsuran)] => ) ".
How can i do that ?
add alias in your query like
$result = mysqli_query($conn, "SELECT SUM(angsuran) as sumtotal FROM `laporan` WHERE id_mustahik=".$detail_campaigner->id_mustahik."");
$row = mysqli_fetch_assoc($result);
echo $row['sumtotal']; //outputs 30000
I have 2 ways
Use array php
$result = mysqli_query($conn, "SELECT SUM(angsuran) FROM `laporan` WHERE id_mustahik=".$detail_campaigner->id_mustahik."");
$row = mysqli_fetch_assoc($result);
echo $row["SUM(angsuran)"]; // out 30000
Modify sql
$result = mysqli_query($conn, "SELECT SUM(angsuran) as sum FROM `laporan` WHERE id_mustahik=".$detail_campaigner->id_mustahik."");
$row = mysqli_fetch_assoc($result);
echo $row["sum"]; // out 30000
Related
$sql = "SELECT COUNT(*) jumlah FROM pasien WHERE DAYNAME(created_at) = 'Tuesday'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
var_dump($result); var_dump($row);
Above is my code. I tried this on phpmyadmin and it return 1. When i run this on php returns NULL.
Edit :
I use mysqli_error to see what's wrong with it and the result :
Can someone help me what's the problem with this code? Im trying to store the fetched data to an array and i want to based on the values of that array. Im getting an error of Array to string conversion. The datatype value of an array is string
Here's the code.
$sql3 ="SELECT DISTINCT subj_descr FROM subj_enrolled WHERE enroll_ref = '$ref'";
$results = mysqli_query($con, $sql3);
$data = array();
while($row = mysqli_fetch_array($results)){
$data[] = array($row['subj_descr']);
}
$sql ="SELECT * FROM notification WHERE subj_descr IN ({implode(',', $data})";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
Remove array inside your while loop:
$sql3 ="SELECT DISTINCT subj_descr FROM subj_enrolled WHERE enroll_ref = '$ref'";
$results = mysqli_query($con, $sql3);
$data = array();
while($row = mysqli_fetch_array($results)){
$data[] = $row['subj_descr'];
}
$sql ="SELECT * FROM notification WHERE subj_descr IN ({implode(',', $data})";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
create a new variable and implode in it.
Try this
$implodeAray = implode(",", $data);
$sql ="SELECT * FROM notification WHERE subj_descr IN ($implodeAray)";
You are creating a multidimensional array and so change this statement
$data[] = array($row['subj_descr']);
to
$data[] = $row['subj_descr'];
As SQL IN statement always used a single dimensional array so also make change in query where clause.
I have changed all, please try below code:
<?php
$sql3 ="SELECT DISTINCT subj_descr FROM subj_enrolled WHERE enroll_ref = '$ref'";
$results = mysqli_query($con, $sql3);
$data = array();
while($row = mysqli_fetch_array($results)){
$data[] = $row['subj_descr'];
}
$dataStr = implode(',', $data);
$sql ="SELECT * FROM notification WHERE subj_descr IN (".$dataStr.")";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
?>
You stored the element of your array inside another array while looping.
Do this:
$sql3 ='SELECT DISTINCT subj_descr
FROM subj_enrolled
WHERE enroll_ref = "$ref"';
$results = mysqli_query($con, $sql3);
$data = array();
while($row = mysqli_fetch_array($results)){
//Your error was here
//Each elements is escaped for security reasons
$data[] = mysqli_escape_string($con,$row['subj_descr']);
}
//This implodes and puts a single quote around each element
$dataIn= '\'' . implode( '\', \'', $data ) . '\'';
$sql ="SELECT * FROM notification
WHERE subj_descr IN ($dataIn)";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
I just dont understand:
I want to get the name of the image from MySQL table like so:
$q = "SELECT legend FROM ps_image_lang WHERE id_image=27";
$res = mysqli_query($con, $q);
print_r($res);
But in the browser window i get such output:
mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 3 [type] => 0 )
Why do i get such output and how should i do it properly ?
You forgot to fetch your result, here you go:
$q = "SELECT legend FROM ps_image_lang WHERE id_image=27";
$res = mysqli_query($con, $q);
$row = mysqli_fetch_assoc($res);
print_r($row);
You can't just only use $res. It will return resource. You must process the resource.
$q = "SELECT legend FROM ps_image_lang WHERE id_image=27";
$res = mysqli_query($con, $q);
if(!$res){
$row=mysql_fetch_row($rest);
print_r($row);
} else
echo "No row is fetched";
Okay guys, sorry, forgot to transform it to an array. Soooooooooo the solution is:
function res_to_array($res) {
//$con = con();
$count = 0;
$res_array = array();
while($row = mysqli_fetch_assoc($res)) {
$res_array[$count] = $row;
$count++;
}
return $res_array;
}
$q = "SELECT legend FROM ps_image_lang WHERE id_image=27";
$res = mysqli_query($con, $q);
$res = res_to_array($res);
print_r($res);
And now i get the contents. Sorry again for asking reaaaly basic questions. :) Or... Like Tomasz Turkowski said: just mysqli_fetch_assoc(); Again, sorry sorry soryy.
I wish to find the count of certain items from database. i used this code
$sql=" SELECT count(*) from request WHERE status = '0'";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
echo "<pre>";
print_r($row);
echo "</pre>";
}
}
i am getting this array in row
Array
(
[count(1)] => 1
)
To fetch value from this array i used
$total = $row[0];
echo $total;
but did not get any result. how can i fetch value from this array
You need to use:
$row = mysqli_fetch_row($result);
echo $row[0];
or change your query to:
$sql=" SELECT count(*) as `num` from request WHERE status = '0'";
and use:
$row = mysqli_fetch_assoc($result));
echo $row['num'];
I think you can not need if condition in your code. You can do this
$sql=" SELECT * from request WHERE status = '0'";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($result))
{
echo "<pre>";
print_r($row);
echo "</pre>";
}
i have this table
Column_1 Column_2
1 value1
2 value1
3 value2
My php query is
$query = "SELECT * FROM `table` WHERE `Column_1` = 'value1' ";
print_r($query);
This returns only the 1st row. I am looking to display row 1 and 2. When I run the SQL in phpmyadmin it returns row 1 and 2. However, the php script only returns row 1... I also did an
echo count($query);
But it returns only 1. What am i doing wrong?
$query = "SELECT * FROM `table` WHERE `Column_2` = 'value1' ";
$res = mysql_query($query);
if(mysql_num_rows($res)!=0) {
while($rowData = mysql_fetch_array($res)) {
var_dump($rowData);
}
}
Use mysql_num_rows to count number of results.
Use mysql_fetch_array or mysql_fetch_assoc to fetch data.
$query = "SELECT * FROM `table` WHERE `Column_1` = 'value1' ";
$res = mysql_query($query);
while($row = mysql_fetch_assoc())
print_r($row);
You need add fetch in cycle.
Use mysql_fetch_array() function
$query = "SELECT * FROM `table` WHERE `Column_1` = 'value1' ";
$res = mysql_query($query);
while($row = mysql_fetch_array($res))
{
echo $row['Column_1'];
echo $row['value_1'];
}