My array print_r is :
Array
(
[email] => xxx#cs.caddmu.edu
[attempt] => 0
[timestamp] => 1369676665
[smtp-id] => <1369676650.51a39b6a76cec#www.openaccesspub.org>
[response] => 451 4.2.0 Temporarily Grey listed. Try again in a couple of minutes
[category] => Invitation
[event] => deferred
)
Array
(
[email] => reidsdds#cs.cdsmu.edu
[timestamp] => 1369676845
[smtp-id] => <1369676650.51a39b6a76cec#www.openaccesspub.org>
[response] => 250 2.0.0 r4RHlOGH017661 Message accepted for delivery
[category] => Invitation
[event] => delivered
)
Array
(
[email] => jrai#openhh.com
[timestamp] => 1369678994
[smtp-id] => <1369678990.51a3a48e93428#ohhpb.org>
[category] => Reviewers
[event] => processed
)
Array
(
[email] => sss#lusst.fi
[timestamp] => 1369678997
[smtp-id] => <1369678990.51a3a48e93428#oub.org>
[response] => 250 2.0.0 Ok: queued as 02C103F0454
[category] => Revie
[event] => delivered
)
And the tables have these rows :
event_id, event, email, category, timestamp, response, attempt, url, status, reason, type, action, m_id .
I'm trying to insert in mysql table with this :-
foreach ($temp_array as $key => $poke)
{
mysql_query ("INSERT INTO temp_array (email,timestamp,category,event,response,attempt,reason,o_id,operator,action,...)VALUES ('$poke[email]','$poke[timestamp]','$poke[category]','$poke[event]',.... )" );
}
But i'm getting error " Undefined index: category in
C:\xampp\htdocs\eembeta\array_temp.php on line 34
How to insert data which does not have specific sequence?
This is not about sequence, but $poke['category'] does not exist in that array (element to insert). Be sure that all array fields you want to use are initialised:
$poke['category'] = $poke['category'] ? $poke['category'] : '';
That code means: Is that field set? yes: use it. no: use empty string.
That way $poke['category'] is known, so you get no error.
Btw, you can replace '' by 0, null or whatever you need.
Before using the data inside your query, sanatize it to avoid SQL Injections. To achieve this, consider using PDO.
Answering your question
For each key, create a temporary variable like:
$event = isset($_GET ['event']) ?$_GET ['event'] : null;
Related
So here's the thing.
I'm querying the database using Doctrine 2.5 and I'd like to use Doctrine's createQuery() method to do it. The reason is that Doctrine 2 hydrates the results in a camelCase manner, and my column names are all with underscores. And since most of the simple queries work fine with createQuery (or even with the query builder) I'd like to move all native MySQL queries to Doctrine's DQL (to get all results in camelCase so that's a standarized results). Either that or you guys can teach me how to make Doctrine 2 return the results in the same way that the columns are in the database... xD
I have the following query:
$sql = '
SELECT sp.*, so.user_id
FROM shop_order so
INNER JOIN shop_payment sp ON(so.payment_id=sp.id)
WHERE so.id = '.$orderID;
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$result = $stmt->fetch();
This results in the following result:
Array
(
[id] => 14
[payment_type_id] => 2
[flag] => boleto_bb
[tid] =>
[billet] => 1
[parcel_quantity] =>
[cart_name_holder] =>
[cart_date_holder] => 1999-12-01
[cart_number_holder] => 0
[datecreated] => 2017-08-30 14:51:25
[dateupdate] => 2017-08-30 16:36:18
[status] => 2
[user_id] => 16
)
But if I do it like this:
$query = $em->createQuery('
SELECT sp, so.userId
FROM ShopOrder so
INNER JOIN ShopPayment sp WITH (so.paymentId = sp.id)
WHERE so.id = ' . $orderID);
$result = $query->getArrayResult();
I get the following result:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 14
[paymentTypeId] => 2
[flag] => boleto_bb
[tid] =>
[billet] => 1
[parcelQuantity] =>
[cartNameHolder] =>
[cartDateHolder] => DateTime Object
(
[date] => 1999-12-01 00:00:00.000000
)
[cartNumberHolder] => 0
[datecreated] => DateTime Object
(
[date] => 2017-08-30 14:51:25.000000
)
[dateupdate] => DateTime Object
(
[date] => 2017-08-30 16:36:18.000000
)
[status] => 2
)
[userId] => 16
)
)
How can I get the first result from the createQuery method?
Have User and UserProfile models. User model has hasOne association with UserProfile. UserProfile mysql table is empty. When I do $this->User->find('all', array('contain' => array('UserProfile'))) instead of an empty UserProfile array, as you would expect, I get an array populated with empty fields that correspond to schema:
Array
(
[0] => Array
(
[User] => Array
(
[id] => 1
[firstname] => Joe
[surname] => Bloggs
[email] => katie.barnes#scotlandsdna.com
[password] => $2a$10$re4r7AXWQcXgkKcabUqmtO6j.7p2bA1t2SLeG93eVsiDPBgGaeXRS
[enabled] => 1
[user_group_id] => 1
[created] => 2014-06-26 15:01:38
[modified] => 2014-06-26 15:01:38
)
[UserProfile] => Array
(
[id] =>
[job_title] =>
[user_id] =>
[enabled] =>
[created] =>
[modified] =>
)
)
Anyone seen this and know how to fix it?!
This is correct behavior because the hasOne association is not optional. If you want to make it optional, then change it to a hasMany and only ever create one record.
Cake is performing a LEFT JOIN from User to UserProfile and the query result produced NULL values for the missing record. This is the same result you would get if you executed the SQL in an editor outside of Cake.
how do i add the new attribute to a json object i have stored in a mysql table column? now i have json_encode data stored like:
"1":{"lastname":"blah","firstname":"R.A.","function":"Manager","email":"test#hotmail.com","barcode":"33432799181"}
and i like to add a new data pair, visted:0 which i want to update as soon as a person has been visited.
"1":{"lastname":"blah","firstname":"R.A.","function":"Manager","email":"test#hotmail.com","barcode":"33432799181", "visited":"0"}
How do i push an element to the existing json data?
$jsondataTmp = json_decode($core,true);
$custom = array('visited'=>'0');
$jsondataTmp[] = $custom;
but it adds an array and not within each
[1] => Array
(
[lastname] => blah
[firstname] => R.A.
[function] => Manager
[email] => test#hotmail.com
[barcode] => 33432799181
)
[2] => Array
(
[lastname] => blah
[firstname] => R.A.
[function] => Manager
[email] => test#hotmail.com
[barcode] => 33432799181
)
[3] => Array
(
[visited] => 0
)
and not
[2] => Array
(
[lastname] => blah
[firstname] => R.A.
[function] => Manager
[email] => test#hotmail.com
[barcode] => 33432799181
[visited] => 0
)
MySQL doesnot have native support for JSON - MySQL still is pure RDBMS system.
One easy hack for the time being can be using a REPLACE function.
UPDATE table_name
SET column_name = REPLACE ( column_name, "}", ',"visited":"0"}';
I have this query for a search form:
// General Query
$conditions = array(
'editore LIKE' => "%$e%",
'titolo LIKE' => "%$t%"
);
Now, if the user chooses fill some field (eg author, year, etc), I have:
if (isset($menu)&&$menu!='')
$conditions[]=array('classe LIKE' => "%$menu%");
Or:
if ($anno&&$anno2)
$conditions[] = array('anno BETWEEN ? AND ?' => array($anno,$anno2));
If the user chooses some tags for the book, I have:
$query_t = $CdBibliotem->query("SELECT codiceBiblio FROM cd_bibliotem WHERE codiceTematica IN (".implode(',', $fields).")");
$query_t = Set::classicExtract($query_t,'{n}.cd_bibliotem.codiceBiblio');
$tot=implode(',', $query_t);
$conditions[]=array('codiceBiblio IN (?)'=> "$tot");
This should work, but instead I don't know why, it gives me only the first record.
This is an example of debug a final query, where the user selects only 1 tag, and leaves the others fields empty:
Array (
[editore LIKE] => %%
[titolo LIKE] => %%
[0] => Array
(
[autori LIKE] => %%
)
[1] => Array
(
[codiceBiblio IN (?)] => 118729,118656,118645,118554,118534,118533,118532,118531,118530,118529,118528,118527,118526,118121,117632,117515,117040,116562,115851,115787,114613,114612,113545,113385,113142
) )
In this case, it gives me all the details of the first book (with id=118729), while it should gives me the details of all that books.
CodeIgniter Active Record is easy, well documented and powerful. But when I try to insert MySQL built in functions CONCAT, NOW, GROUP_CONCAT, DATEDIFF, TRIM etc or my custom functions it is giving errors. The following code works fine...
$result = $this->db->select('p.first_name, p.last_name, p.mobile_number, p.email_address')->from('profile p')->get()->result();
But When I want to contact first_name and last_name and use MySQL CONCAT function like this...
$result = $this->db->select('CONCAT(p.first_name, " ", p.last_name) fullname, p.mobile_number, p.email_address')->from('profile p')->get()->result();
It is showing database errors
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '", `p`.`last_name)` fullname, `p`.`mobile_number`, `p`.`email_address` FROM (`pr' at line 1
SELECT CONCAT(p.first_name, `"` ", `p`.`last_name)` fullname, `p`.`mobile_number`, `p`.`email_address` FROM (`profile` p)
Filename: D:\xampp\htdocs\example\system\database\DB_driver.php
Line Number: 330
Is there a any way to insert MySQL Functions inside CodeIgniter Active Record? Hope I am clear. Thanks in advance.
Since I don't know your exact error:
From user_guide:
$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.
$result = $this->db->select('CONCAT(p.first_name, " ", p.last_name) fullname, p.mobile_number, p.email_address', FALSE)->from('profile p')->get()->result();
Place you "select " in array
$this->db->select(array('CONCAT(p.first_name, " ", p.last_name) fullname, p.mobile_number, p.email_address'))
by.
For the case of update using active records you can use
$this->db->set("date_read", "NOW()", FALSE);
Will output something like this
`SET `date_read` = NOW(),
FIXED THE ISSUE
adding the field names in active record array can fix this problem. inside array you can use any mysql functions. the fix of above.
MySQL Table :
fullname mobile_number email_address
------------------- ------------- ------------------------------
sitaramaiah javvadi 9989403339 gnt.sitaramaiah#mstonline.in
raja kumar guthula 9949526012 gnt.rajkumar#mstonline.in
chandra sekhar k.l 9912144556 gnt.sekhar#mstonline.in
khadar basha 98884884584 khadar333332#gmail.com
super administrator 9841866445 admin#gmail.com
CodeIgniter Active Record :
$result = $this->db->select(array('CONCAT(p.first_name, " ", p.last_name) `fullname`', 'p.mobile_number', 'p.email_address'))->from('profile `p`')->get()->result();
echo '<pre>';
print_r($result);
Output :
Array
(
[0] => stdClass Object
(
[fullname] => sitaramaiah javvadi
[mobile_number] => 9989403339
[email_address] => gnt.sitaramaiah#mstonline.in
)
[1] => stdClass Object
(
[fullname] => raja kumar guthula
[mobile_number] => 9949526012
[email_address] => gnt.rajkumar#mstonline.in
)
[2] => stdClass Object
(
[fullname] => chandra sekhar k.l
[mobile_number] => 9912144556
[email_address] => gnt.sekhar#mstonline.in
)
[3] => stdClass Object
(
[fullname] => khadar basha
[mobile_number] => 98884884584
[email_address] => khadar333332#gmail.com
)
[4] => stdClass Object
(
[fullname] => super administrator
[mobile_number] => 9841866445
[email_address] => admin#gmail.com
)
)