I need to compare to arrays (mysqli_fetch_array) from the same table fetching all columns.
I'm for-looping through and comparing all results.
$rowA = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM table WHERE ID = '$ID'"));
Performing UPDATE
$rowB = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM table WHERE ID = '$ID'"));
$len = count($rowA);
for($i = 0; $i < $len; $i++){
if($rowA[$i] != $rowB[$i]){
$msg .= [COLUMN NAME] . ' => ' . $rowB[$i];
}
}
How do I get the name of the column that differs?
I've tried array_keys() but the array it outputs isn't useful in ways I can come up with.
So I got this to work for me.
$arr = array_diff_assoc($rowA, $rowB);
$key = array_keys($arr);
$len = count($arr);
for($i = 0; $i < $len; $i++){
$msg .= $key[$i] . ' => ' . $rowB[$key[$i]];
}
Note: Added MYSQLI_ASSOC as resulttype for the mysqli_fetch_array function.
Related
I have several variables, for example:
$q1
$q2
$q3
...
$q50
I would like to combine them info one variable who puts them info the VALUE of an INSERT INTO. Maybe a for loop?
INSERT INTO $table_name ($column_names) VALUES($q1, $q2, $q3)
So it might look like this
INSERT INTO $table_name ($column_names) VALUES($combined_variables)
This way, i could just manually add another variable to the $q51 and it would populate VALUE automaticly.
Is this possible? Maybe somehing like this (but this does not work)
$combined_variables = '';
for( $i = 1; $i <= 50 $i++ ) {
$combined_variables .= 'q' . $i . ', ';
}
$combined_variables = substr($combined_variables, 0, -2); //minus 2 to remove the last space and comma
This should work for you:
(Here I start with $q1 and assign it to the array until the next $qX is not set)
<?php
$combined_variables = [];
$count = 1;
while(isset(${"q" . $count})){
$combined_variables[] = ${"q" . $count};
$count++;
}
?>
So as an example:
$q1 = 5;
$q2 = 2;
You would end up with following array:
Array ( [0] => 5 [1] => 2 )
And then you can simply use it in the query like this:
"INSERT INTO $table_name ($column_names) VALUES(" . "'" . implode("','", $combined_variables) . "'" . ")"
You can use variable variables like this:
$combined_variables = array();
for ($i = 1; $i <= 50 $i++) {
$var = 'q' . $i;
$combined_variables[] = ${$var};
}
$combined_variables = implode(', ', $combined_variables);
However, if you can use an array instead of 50 variables you'd have a much easier job.
This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 6 months ago.
Good Day!
Any suggestions on how can I merged and print 3 arrays in PHP.
I retrieved data from database and put it in ARRAY.
1st Array: $date[] = $row['date'];
2nd Array: $requestor[] = $row['requestor'];
3rd Array: $die[] = $row['die'];
then I use foreach to print the retrieved data stored from each Array that meets the condition.
foreach($date as $item_date){
echo $item_date;
}
foreach($requestor as $item_requestor){
echo $item_date;
}
foreach($die as $item_die){
echo $item_requestor;
}
But the result of this code is like this:
date1
date2
date3
requestor1
requestor2
requestor3
die1
die2
die3
My goal is this one:
date1 - requestor1 - die1
date2 - requestor2 - die2
date3 - requestor3 - die3
Any Idea oon how can I achieved this output.
TIA
You have to do it manually count on loop like this
$count = count($date)-1;
then loop through this
for ( $i=0;$i <= $count; $i++ ) {
$arrayGenerate[$i] = array(
'row1' => $data[$i].'-'.$requestor[$i].'-'.$die[$i]
);
}
like this
Try with -
for($i = 0; $i <= count($date); $i ++) {
echo $date[$i]." - ".$requestor[$i]." - ".$die[$i];
}
Assuming the all 3 arrays count/size is same & you want it in this fashion
<?php
$a = array('1','2','3');
$b = array('4','5','6');
$c = array('7','8','9');
for($i=0;$i<count($a);$i++)
{
echo $merged_arr_str = $a[$i] . " - " . $b[$i] . " - ". $c[$i] . " <br/>";
}
?>
You can try this
Example One:-
$date = array('date1','date2','date3','date4');
$requestor = array('requestor1','requestor2','requestor3','requestor4');
$die = array('die1','die2','die3','die4');
$count = max(count($date), count($requestor), count($die));
$newarray = array();
for($i=0; $i < $count; $i++) {
//Demo1
if (isset($date[$i])) $newarray[] = $date[$i];
if (isset($requestor[$i])) $newarray[] = $requestor[$i];
if (isset($die[$i])) $newarray[] = $die[$i];
//Demo2
//echo $ouput = $date[$i].'-'.$requestor[$i].'-'.$die[$i];
}
//array merge output
var_dump($newarray);
Example Two
$date = array('date1','date2','date3','date4');
$requestor = array('requestor1','requestor2','requestor3','requestor4');
$die = array('die1','die2','die3','die4');
$arrays = array($date, $requestor, $die);
array_unshift($arrays, null);
$n = call_user_func_array('array_merge', call_user_func_array('array_map', $arrays));
print_r($n);
for($i = 0; $i < count($date); $i ++) {
echo $date[$i]." - ".$requestor[$i]." - ".$die[$i];
}
this loop will iterat untill last element of date array.if you want to put equal to sign then you have to initialize $i with 1.
exm. if $i = 0 then $i < count($date)
OR
if $i = 1 then $i <= count($date)
I am trying to query a database with a new 'WHERE (AND)' clause on each loop. The new clause is simply the number of the loop.
I need to save the count of rows returned per loop in an array which I am struggling with.
If for instance there is two loops, the first loop returns 2 rows, the second 3. I want the array to essentially be:
$portfolio_list = array(2,3);
Here is as far as I have managed to get, and no surprise its not working correctly.
Any help would be great!
$portfolios = 2;
$i = 1;
$portfolios_list = array();
while ($i <= $portfolios) {
$portfolio_sql = "
SELECT COUNT(portfolio_number) AS portfolio_uploaded_images
FROM exp_submissions
WHERE member_id = $member_id
AND type_id = '1'
AND portfolio_number = $i
";
$query = $this->EE->db->query($portfolio_sql);
$return = $query->result();
$portfolios_list[] = $return;
$i++;
}
please try this
$portfolios = 2;
for ($i = 1; $i < $portfolios+1; $i++) { //this runs 2 times
$where_array = array('member_id' => $member_id, 'type_id' => '1', 'portfolio_number' => $i);
$this->db->where( $where_array );
//$this->db->select('*'); // I guess you do not need select for this
$portfolio_list[$i] = $this->db->get('exp_submissions')->num_rows();
}
var_dump($portfolio_list);
Try this
$portfolios = 2;
$i = 1;
while ($i <= $portfolios) {
$portfolio_sql = "
SELECT COUNT(portfolio_number) AS portfolio_uploaded_images
FROM exp_submissions
WHERE member_id = $member_id
AND type_id = '1'
AND portfolio_number = $i
";
$query = $this->db->query($portfolio_sql);
$portfolios_list[$i] = $query->num_rows; // This will give the No of rows fetched
$i++;
}
foreach($portfolios_list as $row => $no)
{
echo "The Results Fetched from query no ".$row." is ".$no;
}
I want to insert multipl rows in single query.
$firstname = array('Arfan','Awais','Ahmad'.....);
$lastname = array('Haider','k','Raza'..........);
Insert Data:
INSERT INTO `table_nmae`(firstname,lastname)
VALUES
('Arfan','Haider'),('Awais','k'),('Ahmad','Raza');
I can do it very easily if these are only three record.
If I did not know how many records are present then I can not able to use the above.
Then what I did do?
UPDATE:
If I can not know the numbers of record in array.I can simply use foreach loop to do this
But this decrease the performance.
foreach($firstname as $f){
foreach($lastname as $l){
INSERT INTO `table_name`(firstname,lastname)
VALUES('$f','$l');
}
}
Now this time I am using multi query.I think single query is good to performance.
FOR ONE COLUMN:
If I had only one column it is very easy to use single query for this.
INSERT INTO `table_name`(firstname) VALUES
.implode(',', $firstname);
But here is multi column how can I do that for those.
Thanks.....
Use for loop if $firstname and $lastname array are of same length
$str = "INSERT INTO `table_nmae`(firstname,lastname) VALUES"; // create string of query
for($i = 0 ;$i< count($firstname); $i++) {
$str .= "('" . $firstname[$i] . "','" . $lastname[$i] . "'),"; //append the values
}
$str = trim($str, ","); // remove last ,
Try
$size_of_array = count( $firstname );
$query = "INSERT INTO `table_nmae`(firstname,lastname) VALUES ";
for($i = 0; $i < $size_of_array; $i++ )
{
if( $i !== ($size_of_array-1))
$query .= "('{$firstname[$i]}', '{$lastname[$i]}'),";
else
$query .= "('{$firstname[$i]}', '{$lastname[$i]}')";
}
echo $query;
?>
Suppose you have array of values e.g. $values_arr.
$count = count( $values_arr );
$query = "INSERT INTO `table_nmae`(firstname,lastname) VALUES ";
for($i = 0; $i < $count; $i++ )
{
if( $i == 0)
$query .= "('{$values_arr[$i]['firstname']}', '{$values_arr[$i]['lastname']}')";
else
$query .= ",('{$values_arr[$i]['firstname']}', '{$values_arr[$i]['lastname']}')";
}
echo $query;
I'm trying to put the column names of a msSQL table into an array. Using
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
echo mssql_field_name($result) . "<br><br>";
}
the column names print to the screen just fine. Also get_type() shows that they are strings. However, when I try to put them into an array like this:
$column_names = array();
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
$current_column = mssql_field_name($result);
array_push($column_names, $current_column);
}
var_dump($column_names); gives me an array (albeit the expected length) of boolean values. All false. I would expect to see an array containing the names of all my columns. What am I doing wrong here? Thank you
Looks like you are missing the $i argument on the mssql_field_name call. Try this maybe:
$column_names = array();
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
$column_names[] = mssql_field_name($result, $i);
}
http://php.net/manual/en/function.mssql-field-name.php