How can I query to mysql with ARRAY in condition - php

A have names of rows in array
$arr = array(fir, seco, third);
how can I query mysql like:
$query = "SELECT * FROM $table where fir=0 and seco=0 and third=0";
but using array.
and
$query = "update $table SET fir='$ma', seco='$ma', third='$ma'";
but using array.

For search you can fire below query -
$str = implode(",", $arr);
$query = "SELECT * FROM $table where 0 IN ($str)";
But for update you have to use query what you have written.

easy,
$arr = array(fir, seco, third);
for(int i = 0;i<arr.lenght;i++)
{
$query = $query + " and " + arr[i] + "=" + $ma;
}

This is what I would do...
$fir = $arr[0];
$seco = $arr[1];
$third = $arr[2];
$query = "UPDATE $table SET $fir = '$ma', $seco = '$ma', $third = '$ma'";

Not sure if there is a more optimal answer, but you could use a for loop to create the SQL statement:
<?php
$arr = array(fir, seco, third);
$query = "SELECT * FROM $table where ";
$count = count($arr);
for($i=0;$i<$count;$i++){
$query .= ($i==$count-1) ? "$arr[$i]=0" : "$arr[$i]=0 and ";
}
echo $query;
?>
Echoes SELECT * FROM $table where fir=0 and seco=0 and third=0. You can do the same with the UPDATE SQL statement.
Update
Also, you could implode the array, like in Suresh Kamrushi's answer, and use the following code:
<?php
$arr = array(fir, seco, third);
$str = implode(',',$arr);
$query_one = "SELECT * FROM $table WHERE ($str) = (0,0,0)";
echo $query_one;
?>
The UPDATE query would still need a for loop though, I think.

Related

Display the total of the SUM (price * quantity) from SELECT query [duplicate]

I have a column in a table that I would like to add up and return the sum. I have a loop, but it's not working.
while ($row = mysql_fetch_assoc($result)){
$sum += $row['Value'];
}
echo $sum;
You can completely handle it in the MySQL query:
SELECT SUM(column_name) FROM table_name;
Using PDO (mysql_query is deprecated)
$stmt = $handler->prepare('SELECT SUM(value) AS value_sum FROM codes');
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$sum = $row['value_sum'];
Or using mysqli:
$result = mysqli_query($conn, 'SELECT SUM(value) AS value_sum FROM codes');
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
$query = "SELECT * FROM tableName";
$query_run = mysql_query($query);
$qty= 0;
while ($num = mysql_fetch_assoc ($query_run)) {
$qty += $num['ColumnName'];
}
echo $qty;
Try this:
$sql = mysql_query("SELECT SUM(Value) as total FROM Codes");
$row = mysql_fetch_array($sql);
$sum = $row['total'];
Let us use the following image as an example for the data in our MySQL Database:
Now, as the question mentions, we need to find the sum of a particular column in a table. For example, let us add all the values of column "duration_sec" for the date '09-10-2018' and only status 'off'
For this condition, the following would be the sql query and code:
$sql_qry = "SELECT SUM(duration_sec) AS count
FROM tbl_npt
WHERE date='09-10-2018' AND status='off'";
$duration = $connection->query($sql_qry);
$record = $duration->fetch_array();
$total = $record['count'];
echo $total;
MySQL 5.6 (LAMP) . column_value is the column you want to add up. table_name is the table.
Method #1
$qry = "SELECT column_value AS count
FROM table_name ";
$res = $db->query($qry);
$total = 0;
while ($rec = $db->fetchAssoc($res)) {
$total += $rec['count'];
}
echo "Total: " . $total . "\n";
Method #2
$qry = "SELECT SUM(column_value) AS count
FROM table_name ";
$res = $db->query($qry);
$total = 0;
$rec = $db->fetchAssoc($res);
$total = $rec['count'];
echo "Total: " . $total . "\n";
Method #3 -SQLi
$qry = "SELECT SUM(column_value) AS count
FROM table_name ";
$res = $conn->query($sql);
$total = 0;
$rec = row = $res->fetch_assoc();
$total = $rec['count'];
echo "Total: " . $total . "\n";
Method #4: Depreciated (don't use)
$res = mysql_query('SELECT SUM(column_value) AS count FROM table_name');
$row = mysql_fetch_assoc($res);
$sum = $row['count'];
$row['Value'] is probably a string. Try using intval($row['Value']).
Also, make sure you set $sum = 0 before the loop.
Or, better yet, add SUM(Value) AS Val_Sum to your SQL query.
$result=mysql_query("SELECT SUM(column) AS total_value FROM table name WHERE column='value'");
$result=mysql_result($result,0,0);
Get Sum Of particular row value using PHP MYSQL
"SELECT SUM(filed_name) from table_name"
$sql = "SELECT SUM(Value) FROM Codes";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
sum = $row['SUM(price)'];
}
echo sum;

SQL search using PHP arrays

How to search multiple fields in a MySQL database with a PHP array.
For example, search fields like place_name, admin_name1, and admin_name2 using an array of ["Cambridge","Massachusetts","US"] with using the wildcard %
Below is the exact structure of database
Imagining your array index names are the same as your table column names:
$query = "select * from tableName";
$arrCount = count($arrName);
$i=1;
if($arrCount) > 0){
$query .= " where";
foreach($arrName as $key->$val){
$query .= $key." LIKE '%".$val."%'";
if($i <= $arrCount)
$query .= " AND";
$i++;
}
}
Then run the query!
$array = ["Cambridge","Massachusetts","US"];
$list = implode(",", $array);
Then select:
... WHERE place_name IN($list) OR admin_name1 IN($list) OR admin_name2 IN($list)
Try this
$fields = array("place_name", "admin_name1", "admin_name2");
$kwd = array("Cambridge","Massachusetts","US");
$condition = array();
$n = 0;
foreach($fields as $field){
$condition[] .= $field." LIKE '%".$kwd[$n]."%'";
$n++;
}
$condition = implode(" AND ", $condition);
echo $qry = "SELECT * FROM `table` WHERE ( ".$condition." )";

passing array of values in sql select statement of where condition

$sql = "select id from table_name ";
$result = mysql_query($sql);
$data = array();
while($row = mysql_fetch_assoc($result))
{
$data[] = $row[id];
}
/* $data contains id's fetched from sql query from db.now i want to pass this id's(array of values) in $data array one by one to below select query in where condition and obtain desired result for each id.My question is how to pass an array of values to the below select statement I dont know how to do this.Any help is greatly appreciated.*/
$query = "select * from table where id1 = $data[] ";
$query = "select * from table where `id1` in (" . implode(', ', $data) . ")";
You should use the cross database function in Moodle called get_in_or_equal()
list($where, $params) = $DB->get_in_or_equal($data, SQL_PARAMS_NAMED);
$sql = "SELECT *
FROM {table}
WHERE $id {$where}"
$records = $DB->get_records_sql($sql, $params);
You can use the IN clause.
When you are totally sure you only have numeric values in your $data array. You can do the following:
$query = "select * from table where id1 IN(" . implode(',', $data) . ")";
You can use this:
$comma_separated = implode(",", $data);
if ($comma_separated != "")
$query = "select * from table where id1 IN($comma_separated)";

update query from array

i want to update query from array.the problem is when i ran the code, the updated data in the database just the first data..
example..
the array consist of integer type = [1,2]
when i ran the code, the data in the database just ran the first data (only 1)..how can i fix this?
PHP file
$query_edit_paket = "select nama_menu from menu where id_menu = 149 ";
$query_exec_edit_paket = mysql_query($query_edit_paket) or die(mysql_error());
$nama_menu_query = reset(mysql_fetch_assoc($query_exec_edit_paket));
$_fbexclude = mysql_query("select id_paket from detail_paket where menu_paket = '$nama_menu_query' ");
$fbexcludearray = array();
while ($row = mysql_fetch_assoc($_fbexclude)) {
$fbexcludearray[] = $row['id_paket'];
}
$excludes = implode(',', $fbexcludearray);
echo $excludes;
for($i=0;$i<count($excludes);$i++)
{
if($excludes != "")
{
$strSQL = "update paket set ketersediaan_paket ='kosong' ";
$strSQL .="WHERE id_paket = '$excludes' ";
$objQuery = mysql_query($strSQL);
}
}
thanks!!
Try using IN
change
$strSQL .="WHERE id_paket = '$excludes' ";
to
$foo = '';
for($i=0; $i < count($fbexcludearray); $i++){
$foo.= $i == 0? $fbexcludearray[$i]: ','.$fbexcludearray[$i];
}
$strSQL .="WHERE id_paket IN ($foo) ";
If you were to print your $strSQL you would see that your query would be something like this
update paket set ketersediaan_paket ='kosong' WHERE id_paket = '1,2'
Which is not the query you want. What you want is more something like this
update paket set ketersediaan_paket ='kosong' WHERE id_paket = '1' OR WHERE id_paket = '2'
So you need to restructure your for loop or change your query.

Get sum of MySQL column in PHP

I have a column in a table that I would like to add up and return the sum. I have a loop, but it's not working.
while ($row = mysql_fetch_assoc($result)){
$sum += $row['Value'];
}
echo $sum;
You can completely handle it in the MySQL query:
SELECT SUM(column_name) FROM table_name;
Using PDO (mysql_query is deprecated)
$stmt = $handler->prepare('SELECT SUM(value) AS value_sum FROM codes');
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$sum = $row['value_sum'];
Or using mysqli:
$result = mysqli_query($conn, 'SELECT SUM(value) AS value_sum FROM codes');
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
$query = "SELECT * FROM tableName";
$query_run = mysql_query($query);
$qty= 0;
while ($num = mysql_fetch_assoc ($query_run)) {
$qty += $num['ColumnName'];
}
echo $qty;
Try this:
$sql = mysql_query("SELECT SUM(Value) as total FROM Codes");
$row = mysql_fetch_array($sql);
$sum = $row['total'];
Let us use the following image as an example for the data in our MySQL Database:
Now, as the question mentions, we need to find the sum of a particular column in a table. For example, let us add all the values of column "duration_sec" for the date '09-10-2018' and only status 'off'
For this condition, the following would be the sql query and code:
$sql_qry = "SELECT SUM(duration_sec) AS count
FROM tbl_npt
WHERE date='09-10-2018' AND status='off'";
$duration = $connection->query($sql_qry);
$record = $duration->fetch_array();
$total = $record['count'];
echo $total;
MySQL 5.6 (LAMP) . column_value is the column you want to add up. table_name is the table.
Method #1
$qry = "SELECT column_value AS count
FROM table_name ";
$res = $db->query($qry);
$total = 0;
while ($rec = $db->fetchAssoc($res)) {
$total += $rec['count'];
}
echo "Total: " . $total . "\n";
Method #2
$qry = "SELECT SUM(column_value) AS count
FROM table_name ";
$res = $db->query($qry);
$total = 0;
$rec = $db->fetchAssoc($res);
$total = $rec['count'];
echo "Total: " . $total . "\n";
Method #3 -SQLi
$qry = "SELECT SUM(column_value) AS count
FROM table_name ";
$res = $conn->query($sql);
$total = 0;
$rec = row = $res->fetch_assoc();
$total = $rec['count'];
echo "Total: " . $total . "\n";
Method #4: Depreciated (don't use)
$res = mysql_query('SELECT SUM(column_value) AS count FROM table_name');
$row = mysql_fetch_assoc($res);
$sum = $row['count'];
$row['Value'] is probably a string. Try using intval($row['Value']).
Also, make sure you set $sum = 0 before the loop.
Or, better yet, add SUM(Value) AS Val_Sum to your SQL query.
$result=mysql_query("SELECT SUM(column) AS total_value FROM table name WHERE column='value'");
$result=mysql_result($result,0,0);
Get Sum Of particular row value using PHP MYSQL
"SELECT SUM(filed_name) from table_name"
$sql = "SELECT SUM(Value) FROM Codes";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
sum = $row['SUM(price)'];
}
echo sum;

Categories