key value pair addition from query result, php - php

I am having some issues with the below, wanting to add the query result "id" as the key to the value "concat" -- Do I need a foreach for this? What is the best way?
$concatCol = mysqli_query( $connS, "SELECT id, ShipmentNumber, InvoiceNumber, BillofLading from testTable");
$data =array();
while ($row = mysqli_fetch_array($concatCol)) {
$id = $row["id"];
$ShipmentNumber = $row["ShipmentNumber"];
$InvoiceNumber = $row["InvoiceNumber"];
$BillofLading = $row["BillofLading"];
$concat = $ShipmentNumber.$InvoiceNumber.$BillofLading;
echo $concat."<br>";
$data[] = $concat;
}

I'm not really sure what your expected output is. But I think one of the following will work for you:
$data[]['id'] = $concat;
which will result in an array like this:
Array
(
[0] => Array
(
[id] => ShipmentNumberInvoiceNumberBillofLanding
)
[1] => Array
(
[id] => ShipmentNumberInvoiceNumberBillofLanding
)
)
The second option would be:
$data[$id] = $concat;
Which will yield a result like:
Array
(
[1] => ShipmentNumberInvoiceNumberBillofLanding
[4] => ShipmentNumberInvoiceNumberBillofLanding
)
Where the keys 1 and 4 are the $id for each returned row. In both example the ShipmentNumberInvoiceNumberBillofLanding would of course be the concatenated values for each row.

Related

select query to make one column as key in mysql

I need the array as (2) from a single query
can anyone help ?
1. Array
(
[0] => Array
(
[crop_id] => 3
[crop_name] => Barley
)
2. Array
(
[0] => Array
(
[Barley] => 3
)
)
Don't know about query but you can do that simply using array_map()
$array[] = array('crop_id' => 3, 'crop_name' => "Barley");
$result = array_map("myfunction",$array);
print_r($result);
function myfunction($v)
{
$data = [];
$data[$v['crop_name']] = $v['crop_id'];
return $data;
}
Let's suppose you have stored your data in array named as crop_data like
$crop_data[0][crop_id]=3;
$crop_data[0][crop_name]='Barley';
....
$crop_data[n][crop_id]=187;
$crop_data[n][crop_name]='Wheat'
Try this code:
$new_crop_result=array()
foreach($crop_data as $key=>$record)
{
$new_crop_result[$key][$record[crop_name]]=$record[crop_id];
}

php mysql select for organizational chart / multi level structure

I have build my user table like this:
Now I would like to select the 3 blue rows.
I have build the following function:
function display_children($parent, $array = array()) {
global $db;
$stmt = $db->prepare("SELECT id AS man, parent_id FROM user WHERE parent_id= ?");
$stmt->execute(array($parent));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
display_children($row['man'], $array);
$array[] = array("man" => $row['man'], "parent_id" => $row['parent_id']);
echo $row['man']."\n"; //for debug
}
return $array;
}
print_r(display_children(50001));
My Output is:
50002
50004
50003
Array
(
[0] => Array
(
[man] => 50002
[parent_id] => 50001
)
[1] => Array
(
[man] => 50003
[parent_id] => 50001
)
)
The first three lines are correct, but the array is missing one.
The problem is, that the first parent_id has two rows. But the array is empty at the beginning. Is there a solution for my query?
the solution was to change the following line from
display_children($row['man'], $array);
to
$array = display_children($row['man'], $array);

Fetch all rows based on the query into an array and return single value

Fetch all rows based on the query into an array and return single value
Query database for data
//----------------------------------------------------------------------
$result = mysql_query("SELECT * FROM $tableName"); query
$array = mysql_fetch_row($result);
fetch result In Array
$arr = array();
while ($row = mysql_fetch_assoc($result)) {
$arr2 = array();
foreach ($row as $val) $arr2[] = $val;
$arr[] = $arr2;
}
Result Will Be
Array
(
[0] => Array
(
[0] => status_site
[1] => 0
)
[1] => Array
(
[0] => title_site
[1] => Script
)
[2] => Array
(
[0] => keys_site
[1] =>
)
)
I need to make function that return element 0 to 1
ex: function getsetting (title_site){
return value script}
Replace the $arr[] = $arr2; with $arr[$arr2[0]] = $arr2[1];. That might solve your problem.

PHP Unique Values from Column in Array

I have an array that is generated from a SQL query that I run. It looks like the following:
$arr[$i]['id'] = $id;
$arr[$i]['name'] = $name;
$arr[$i]['email'] = $email;
How can I get the unique values from the email column? I appreciate the help.
The best answer is :
array_unique(array_column($arr, 'email'))
Either filter it in your column using the DISTINCT method in MySQL, or use something like
$uniqueEmails = array();
foreach($arr as $array)
{
if(!in_array($array['email'], $uniqueEmails)
$uniqueEmails[] = $array['email'];
}
Since PHP 5.5, a new function called array_column() is also available.
You can use it following way:
$allEmails = array_column($arr, 'email');
$uniqueEmails = array_unique($allEmails);
Remove duplicates from array comparing a specific key
Consider the same array but id of 3rd index is different:
$all_array = Array
(
[0] => Array
(
[id] => 1
[value] => 111
)
[1] => Array
(
[id] => 2
[value] => 222
)
[2] => Array
(
[id] => 3
[value] => 333
)
[3] => Array
(
[id] => 4
[value] => 111
)
)
Now, both 1 & 4 have same values. So we want to remove any of them:
$unique_arr = array_unique( array_column( $all_array , 'value' ) );
print_r( array_intersect_key( $all_array, $unique_arr ) );
If you get the list sorted by email from SQL you can improve performance by looping through the array like Gareth does, but instead only compare current email address with the last inserted email address. Below is a code example for this:
$uniqueEmails = array();
$lastemail = '';
foreach($arr as $array)
{
if($array['email'] != $lastemail)
{
$uniqueEmails[] = $array['email'];
$lastemail = $array['email'];
}
}

How to fetch a row of data and store it in an array?

I'm having trouble fetching data from my database and sorting it in an array.
Here is my current code:
I'm looping through each engine to create a sorted array with each servers ID.
for($i=0; $i <= $servers_default_engines_count; $i++){
//get all servers with this engine type, and get how many
$query = $db->query("SELECT `id` FROM `cgshop_servers` WHERE `server_engine` = $i");
$server_id = mysql_fetch_row($query);
$server[$i] = array();
array_push($server[$i], $server_id);
};
Result with mysql_fetch_row:
[0] => Array
(
[0] => Array
(
[0] => 2
)
)
[1] => Array
(
[0] => Array
(
[0] => 1
)
)
)
also:
[0] => Array
(
)
[1] => Array
(
)
[2] => Array
(
[0] => 1
)
But i'm looking to return it like this:
() placed for explanation reasons.
Array(
[0] (engine1) => Array(
[0](server1) => 2 (serverid)
)
[1(engine2)] => Array(
[0](server1) => 1 (serverid)
[1](server2) => 3 (serverid)
)
)
This is the current data in the mysql tables:
cgshop_servers:
id server_name server_ip serevr_port server_engine source_engine
1 test1 195.62.14.65 15464 1 1
2 test2 195.62.14.63 15464 0 0
3 test3 195.64.14.62 15465 1 1
Have you tried using mysql_fetch_assoc (information)
mysql_fetch_assoc — Fetch a result row as an associative array
Example Usage
$mysql_query = mysql_query("SELECT * FROM `cgshop_servers` WHERE `server_engine` = ?", $i);
$my_array = mysql_fetch_assoc($mysql_query);
print_r($my_array);
you are fetching it wrongly, since you said you are using prepared statement, below is the updated code, you can use.
$server = array();
for($i=0; $i <= $servers_default_engines_count; $i++){
//get all servers with this engine type, and get how many
$sth = $db->prepare("SELECT `id` FROM `cgshop_servers` WHERE `server_engine` = ?");
$sth->bind_param('i', $i);
$sth->execute();
$server[$i] = $sth->fetch();
};
or better of you can fetch it with one single query without any loop like this.
$totalServerCounts = range(0, $servers_default_engines_count);
$sth = $db->query("SELECT `id` FROM `cgshop_servers` WHERE `server_engine` IN(".implode(', ', $totalServerCounts).")");
$servers = $sth->fetch_all();

Categories