I am attempting to perform a query using Zend_Db_Select. Here is my code:
$db = $this->db;
$select = $this->db->select(false)
->from('invoice',$data1)
->join('partner_settings', $db->quoteInto('partner_settings.clientid = ?', $clientid), array()) //'toggle_value'))
->join('partner_info', $db->quoteInto('partner_info.rowid = partner_settings.partnerinfoid', array())) //'type', 'shipper_name' => 'partner_info.name')))
->join('partner_shipping', $db->quoteInto('partner_shipping.partnersettingsid = partner_settings.rowid', array())) //'default_method_id')))
->join('partner_ship_methods', $db->quoteInto('partner_ship_methods.rowid = partner_shipping.default_method_id'), array()) //'shipping_method' => 'name'))
->where('storeid IN (?)',$inputid)
->where('partner_info.type = ?', 'shipping')
->where('partner_settings.toggle_value = ?', 'on')
->order(array('datetime_cre DESC'));
$data1 is an array containing these values:
Array
(
[0] => invoice_date AS inv_invoice_date
[1] => invoice_id AS inv_invoice_id
[2] => name AS inv_name
[3] => ups_track AS inv_ups_track
[4] => shipping_pdf AS inv_shipping_pdf
[5] => invoice_pdf AS inv_invoice_pdf
[6] => alert AS inv_alert
[7] => invoice_date AS inv_invoice_date
[8] => invoice_id AS inv_invoice_id
[9] => name AS inv_name
[10] => subtotal AS inv_subtotal
[11] => tax_inclusive AS inv_tax_inclusive
[12] => total AS inv_total
[13] => shipping_pdf AS inv_shipping_pdf
[14] => invoice_pdf AS inv_invoice_pdf
[15] => alert AS inv_alert
[16] => rowid
[17] => partner_info.name AS shipper_name
[18] => partner_ship_methods.name AS shipping_method
)
The resulting MYSQL query looks like this:
SELECT `invoice`.`invoice_date` AS `inv_invoice_date`, `invoice`.`invoice_id` AS `inv_invoice_id`, `invoice`.`name` AS `inv_name`, `invoice`.`ups_track` AS `inv_ups_track`, `invoice`.`shipping_pdf` AS `inv_shipping_pdf`, `invoice`.`invoice_pdf` AS `inv_invoice_pdf`, `invoice`.`alert` AS `inv_alert`, `invoice`.`invoice_date` AS `inv_invoice_date`, `invoice`.`invoice_id` AS `inv_invoice_id`, `invoice`.`name` AS `inv_name`, `invoice`.`subtotal` AS `inv_subtotal`, `invoice`.`tax_inclusive` AS `inv_tax_inclusive`, `invoice`.`total` AS `inv_total`, `invoice`.`shipping_pdf` AS `inv_shipping_pdf`, `invoice`.`invoice_pdf` AS `inv_invoice_pdf`, `invoice`.`alert` AS `inv_alert`, `invoice`.`rowid`, `partner_info`.`name` AS `shipper_name`, `partner_ship_methods`.`name` AS `shipping_method`, `partner_info`.*, `partner_shipping`.* FROM `invoice`
INNER JOIN `partner_settings` ON partner_settings.clientid = '33'
INNER JOIN `partner_info` ON partner_info.rowid = partner_settings.partnerinfoid
INNER JOIN `partner_shipping` ON partner_shipping.partnersettingsid = partner_settings.rowid
INNER JOIN `partner_ship_methods` ON partner_ship_methods.rowid = partner_shipping.default_method_id WHERE (storeid IN ('43')) AND (partner_info.type = 'shipping') AND (partner_settings.toggle_value = 'on') ORDER BY `datetime_cre` DESC
My biggest problem is with the SELECT columns clause which includes: partner_info.*, partner_shipping.* I don't want to include all columns from these tables. I have set the join() columns argument to empty array(), but it doesn't help.
Has anyone found a solution to this problem? I have been searching futilely.
You haven't set the columns argument to an empty array, you're passing the array as the second argument to quoteInto(). You have:
->join('partner_info', $db->quoteInto('partner_info.rowid = partner_settings.partnerinfoid', array()))
The quoteInto() serves no purpose since you aren't passing in any variables, so what you probably want is:
->join('partner_info', 'partner_info.rowid = partner_settings.partnerinfoid', array())
so the first parameter to join() is the table name, the second is the condition, the third is the columns.
Related
I have multiple array to insert into database but i don't fix the field name because can select format table data and insert into database but can check field name with $id_template.
This my format table(example)
So i want to know how can i get data from multiple array to insert into database
This my code in controller
$column = $this->m_rate_template->get_column($id_template);
$colum_detail = implode(",", $column);
$column_cut = explode(",", $colum_detail); //example data get format is Array ( [0] => min [1] => max)
foreach ($column_cut as $key => $val){
$a = $this->input->post($column_cut[$key]);
foreach ($a as $key1 => $val1){
echo $val1;
$child_data = array(
'id' => $this->m_rate_template->generate_id_in_template($template_name),
'id_rate' => $id_rate,
$column_cut[$key] => $val1
);
$this->m_rate_template->insert_rate($child_data, $template_name);
}
}
My data it show like this
Array ( [id] => 4ae665037e [id_rate] => 7f881e02bb [min] => 1 )
Array ( [id] => bc3e60157f [id_rate] => 7f881e02bb [min] => 2 )
Array ( [id] => 082de3ad82 [id_rate] => 7f881e02bb [max] => 1 )
Array ( [id] => ee135ecd8a [id_rate] => 7f881e02bb [max] => 2 )
actually, data should be like this
Array ( [id] => 4ae665037e [id_rate] => 7f881e02bb [min] => 1 [max] => 2)
Array ( [id] => 082de3ad82 [id_rate] => 7f881e02bb [max] => 1 [max] => 2)
Update
$array = array(
[0] => array(
'min' => '2500',
'max' => '5000'
),
[1] => array(
'min' => '5001',
'max' => '7000'
)
)
You can use batch insert to insert multiple
$this->db->insert_batch();
first parameter is table name and second is array of arrays(records)
if you have want to insert multiple record in table then you can also use codeigniter inbuilt insert_batch function without make query in loop.
so i thing your execution will be fast.
you have want to array in below format.
$array = array(
[0] => array(
'column 1' => 'value 1',
'column 2' => 'value 1'
),
[1] => array(
'column 1' => 'value 2',
'column 2' => 'value 2'
)
)
$this->db->insert_batch('tbl_name',$array)
so please make your code and generate your array as above in loop and simply pass your array in insert_batch function.
how can i get the first row of result? Below is my code which is giving me an error like this Undefined index: module
if ( substr( $action, 0, 4 ) === "stl_" )
{
$query = "SELECT * FROM a_actions LEFT JOIN a_modules ON ( a_modules.id=a_actions.module_id )
WHERE a_actions.id=(SELECT dependency FROM a_actions WHERE action='{$action}') AND a_modules.module_status = 1 ";
$action = \Yii::$app->db->createCommand( $query )
->queryAll();
//print_r($action);die();
$module = $action[ 'module' ];
$action = $action[ 'action' ];
}
$action has value
Array ( [0] => Array ( [id] => 7 [module_id] => 7 [action] => index [label] => Members [dependency] => [created_by] => [created_at] => [updated_by] => [updated_at] => [is_deleted] => 0 [module] => members [module_name] => [module_status] => 1 ) )
in Yii1 i would have used
$action = \Yii::$app->db->createCommand( $query )
->queryRow();
You can use queryOne()
\Yii::$app->db->createCommand( $query )
->queryOne();
QueryOne()
My code gave this warning, but when i print_r the row, it display the value selected.
So how do i solve this
This is the code "
$query3 = ("Select b.*, c.ScrubNurseName from scrubnurse b, surgery a, scrubnurselist c
where b.rn_no = '$id'
and a.SurgId = b.SurgId
and b.ScrubNurse = c.ScrubNurseID
ORDER BY b.SurgId, b.SerialNo");
$result3 = mysql_query($query3) or die(mysql_error());
$row3 = mysql_fetch_array($result3);
<?php
/*try utk display scrubnurse */
print_r($row3);
while($row3 = mysql_fetch_array($query3)){?>
<p>Scrub Nurse : <?php echo $row3['ScrubNurse'];?>
<p>Status : <?php echo $row3['Status'];?>
<?php }
and this is the result from print_r($row3)
Scrub Nurse Array ( [0] => 16
[SerialNo] => 16 [1] =>
rand52fad80207b6a1.33040579 [SurgID] => rand52fad80207b6a1.33040579 [2]
=> RN001-13 [Rn_No] => RN001-13 [3] =>
2014-02-10 [Surg_Date] => 2014-02-10 [4] => 015405 [ScrubNurse] =>
015405 [5] => C [SNRole] => C [6] => Azhari Landut [ScrubNurseName] => Azhari
Landut )
Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\cotds3\editpt_surgery2.php on line 470
In line while($row3 = mysql_fetch_array($query3)){? there is typo it should be while($row3 = mysql_fetch_array($result3)){?
I am trying to do a simple left join, while using table aliases, to return all of the values from both tables in this format:
[Alias1] => Array
(
[id] => 1
[city] => bay area
[state] => CA
)
[Alias2] => Array
(
[id] => 1
[city] => san francisco
[baseball_team] => giants
)
Here is my very simple mysqli_query:
$result = mysqli_query($sql, SELECT Alias1.id, Alias1.city, Alias1.state, Alias2.id, Alias2.city, Alias2.baseball_team FROM database.table1 AS Alias1 LEFT JOIN database.table2 AS Alias2 ON Alias1.id = Alias2.id)
To get the results... I use:
while($row = mysqli_fetch_array($result))
{
$data[] = $row;
}
print_r($data);die();
Problem: The data prints out as such:
[0] => Array
(
[0] => 1
[id] => 1
[1] => bay area
[city] => bay area
[2] => CA
[state] => CA
[3] => 1
[4] => san francisco
[5] => giants
[baseball_team] => giants
)
Other Info: I originally tried to get the data using mysql_fetch_assoc($result)... But the duplicate columns don't return at all.
Instead of:
$data[] = $row;
Use this to form the data array that you desire:
$data['Alias1'] = array(
"id"=>$row['id'],
"city"=>$row['1_city'],
"state"=>$row['state'],
);
$data['Alias2'] = array(
"id"=>$row['id'],
"city"=>$row['2_city'],
"baseball_team"=>$row['baseball_team'],
);
OR, if you expect more than one row you need to use something like:
$data[] = array(
"Alias1" => array(
"id"=>$row['id'],
"city"=>$row['1_city'],
"state"=>$row['state'],
),
"Alias2" => array(
"id"=>$row['id'],
"city"=>$row['2_city'],
"baseball_team"=>$row['baseball_team'],
),
);
Note:
If your city values are different between your two tables then you should use an alias so that you can pull the other one differently. If they are the same, I suggest you not have them listed twice in the database as this is duplicating data and leaves your database denormalized.
Edit:
Your query should be something like
SELECT Alias1.id, Alias1.city AS 1_city, Alias1.state, Alias2.id, Alias2.city AS 2_city, Alias2.baseball_team FROM database.table1 AS Alias1 LEFT JOIN database.table2 AS Alias2 ON Alias1.id = Alias2.id
See edited answers above for these aliases.
This function works fine, what is flipping me out is that the array returned has all the information needed except $row[15] which has data in it on the table Orders
function SelectOrder($orderid)
{
connect();
$result = mysql_query("SELECT * FROM `Orders` WHERE `OrderID` =".$orderid." LIMIT 0 , 30");
$row = mysql_fetch_row($result);
return $row;
}
print_r($row);
Prints
Array ( [0] => 24
[1] => Grei
[2] => Tristram
[3] => 19 2nd Blvd.
[4] => Richmond
[5] => J7V 5R6
[6] => Ontario
[7] => Canada
[8] => grei#email.ca
[9] => (514) 555-5555
[10] => Snow Removal
[11] => 210
[12] => 32.5
[13] => 23.07
[14] => 200.57
[15] =>
[16] => 123 same street
[17] => 1
[18] => 0 )
When I use the same select statement within PHPMyAdmin
SELECT *
FROM `Orders`
WHERE `OrderID` = 24
LIMIT 0 , 30
I get value [15] (SNAME = Frank Ditripani)
PHPMyAdmin SQL Results
OrderID-Fname-Lname-Address-City-Pcode-Prov-Country-Email-Phone-Service-Price-Discount-Tax PYMNTAmount-SNAME-SADD-Agreed-PayPalPaid
24-Grei-Tristram-19 2nd Blvd.-Richmond-J7V 5R6-Ontario-Canada-grei#email.ca-(514) 555-5555-Snow Removal-210-32.5-23.07-200.57-Frank Ditripani-123 same street-1-0
Both SNAME and SADD are the exact same properties in the table which is varchar(50) and SADD is returned but not SNAME.
This is the first time I have ever posted a question I usually find my answers here but this one is driving me nuts! and I am a bit embarrassed as the answer is probabley an easy one.
The query might be the same, but is the database the same?
Check that you are connecting to the same database!
My bet is the connection parameters are different for the two programs.
Check the connection character set - DB, client and connection should use the same one.