Codeigniter Insert Data MSSQL - sql-server-2008

I am working website with multiple database and it use both MSSQL and MYSQL but I dont why insert data wont work. I have 2 codes sample below:
1st MODEL code
$this->mssqlu->insert('tbl_account', array('id ' => CONVERT(BINARY(16),$username, 1), 'password' => CONVERT(BINARY(16),$password, 1), 'Email' => $email, 'accounttype' =>0, 'birthdate' => '2011-11-11 00:00:00','pin' => $pin, 'fb_id' => $fb_id ));
return $this->mssqlu->insert_id();
2nd MODEL Code
$lu_sql = sprintf("INSERT INTO tbl_account (id,password,Email,accounttype,birthdate,pin,fb_id) VALUES ((CONVERT (binary,$username)),(CONVERT (binary,$password)),$email,0,'2011-11-11 00:00:00',$pin,$fb_id)");
$query = $this->mssqlu->query($lu_sql);
return $query;

$data=array('id ' => CONVERT(BINARY(16),$username, 1), 'password' =>
CONVERT(BINARY(16),$password, 1), 'Email' => $email, 'accounttype'
=>0,'birthdate' => '2011-11-11 00:00:00','pin' => $pin, 'fb_id' =>
$fb_id );
return (($this->mssqlu->insert('tbl_account', $data)) ? $this->mssqlu->insert_id() : False);

Related

$wpdb->insert_id is not working

I am trying to get the last inserted in id MySQL but it doesn't return anything. Row is getting inserted in the database and also $wpdb->last_query gives the last inserted query also.
Here is my code
global $wpdb;
$table_name = $wpdb->prefix . "request_from";
$wpdb->insert($table_name,
array('pre' => $prefix,
'first_name' => $first_name,
'middle_name' => $middle_initial,
'last_name' => $last_name,
'badge_name' => $name_badge,
'title' => $title,
'company' => $company,
'direct_mail' => $direct_mail,
'twitter' => $twitter_handle,
'direct_phone' => $direct_phone,
'address' => $address,
'address2' => $address_2,
'city' => $city,
'state' => $state,
'province' => $province,
'zip' => $zip_code,
'country' => $country,
'cc' => $cc,
'cc_contact' => $second_contact,
'cc_mail' => $second_email,
'cc_phone' => $second_phone)
);
$x= $wpdb->last_query;
$id = $wpdb->insert_id
I have a column called id with auto increment value
id int(11) No None AUTO_INCREMENT
Follow wpdb reference in Codex for troubleshooting:
enable database error display via $wpdb->show_errors()
check what query is being formed and run via $wpdb->last_query
Please check code by print or dump variable,
global $wpdb;
$table_name = $wpdb->prefix . "request_from";
$wpdb->insert($table_name,
array('pre' => $prefix,
'first_name' => $first_name,
'middle_name' => $middle_initial,
'last_name' => $last_name,
'badge_name' => $name_badge,
'title' => $title,
'company' => $company,
'direct_mail' => $direct_mail,
'twitter' => $twitter_handle,
'direct_phone' => $direct_phone,
'address' => $address,
'address2' => $address_2,
'city' => $city,
'state' => $state,
'province' => $province,
'zip' => $zip_code,
'country' => $country,
'cc' => $cc,
'cc_contact' => $second_contact,
'cc_mail' => $second_email,
'cc_phone' => $second_phone)
);
$x= $wpdb->last_query;
$id = $wpdb->insert_id;
// Let's output the last autogenerated ID.
echo $wpdb->insert_id;
// This returns the same result
echo mysql_insert_id();

CakePHP Sort order not affecting all records

In my CakePHP 2.7.7 app, I have an issue where using PaginatorComponent isn't properly sorting my results. See this link for an image:
Sample Data
The data should be sorted by descending last name, but you can see there are a couple users who are seemingly exempt from this data. Doesn't matter what order I do it in, ascending or descending, these few records don't get sorted. For reference:
index() in UsersController:
public function index() {
$this->User->contain('Status');
$this->paginate = array(
'limit' => 15,
'order' => array('User.last_name' => 'DESC'),
'conditions' => array('User.department_id =' => $this->Session->read('Auth.User.department_id')),
'contain' => 'Status'
);
$this->set('users', $this->Paginator->paginate());
}
Any ideas what could have caused this? I'm at a loss here. Thanks in advance!
SELECT `User`.`id`, `User`.`first_name`, `User`.`last_name`, `User`.`middle`, `User`.`address`, `User`.`address_2`, `User`.`city`, `User`.`state_id`, `User`.`zip`, `User`.`home`, `User`.`cell`, `User`.`work`, `User`.`email`, `User`.`status_id`, `User`.`status_reason`, `User`.`rank`, `User`.`birthday`, `User`.`gender`, `User`.`school`, `User`.`employer`, `User`.`position`, `User`.`created`, `User`.`modified`, `User`.`updated_by`, `User`.`radio`, `User`.`ident`, `User`.`parent_name`, `User`.`parent_number`, `User`.`parent_email`, `User`.`squad`, `User`.`squad_leader`, `User`.`ride_along`, `User`.`drivers_license`, `User`.`username`, `User`.`password`, `User`.`department_id`, `User`.`join_date`, `User`.`group_id`, `User`.`test`, (CONCAT(`User`.`first_name`, " ", `User`.`last_name`)) AS `User__name`, `Status`.`id`, `Status`.`status` FROM `admin_cake`.`users` AS `User` LEFT JOIN `admin_cake`.`statuses` AS `Status` ON (`User`.`status_id` = `Status`.`id`) WHERE `User`.`department_id` = 1 ORDER BY `User`.`last_name` ASC LIMIT 15
When you use $this->paginate to set conditions, limit...you need to use function $this->pagination().
$this->pagination and $this->Paginator->pagination() can not be combined.
1)
public function index() {
$this->User->contain('Status');
$this->paginate = array(
'limit' => 15,
'order' => array('User.last_name' => 'DESC'),
'conditions' => array('User.department_id =' => $this->Session->read('Auth.User.department_id')),
'contain' => 'Status'
);
// Assign settings
$this->Paginator->settings = $this->paginate;
$this->set('users', $this->Paginator->paginate());
}
2)
public function index() {
$this->User->contain('Status');
$this->paginate = array(
'limit' => 15,
'order' => array('User.last_name' => 'DESC'),
'conditions' => array('User.department_id =' => $this->Session->read('Auth.User.department_id')),
'contain' => 'Status'
);
//Don't use $this->Paginator->paginate()
$this->set('users', $this->paginate());
}

BEGIN,INSERT,COMMIT with ON DUPLICATE KEY UPDATE

I have two tables:
1- beoordelingen
2 - sterren
I need to update 3 values in table beoordelingen and insert new values into table sterren
How do I do that?
ON DUPLICATE KEY doesn't work and I can't find anywhere how to do it.
$sql=$dbo->prepare("BEGIN;INSERT INTO beoordelingen(beoordelingid,userid,werkid,inlijn,
sjabloon,conclusie,save)
VALUES ('$beoordelingid','$user','$werkid','$inlijn','$sjabloon','$conclusie','0')
ON DUPLICATE KEY UPDATE
beoordelingid=VALUES(beoordelingid),userid=VALUES(userid), werkid=VALUES(werkid),
inlijn=VALUES(inlijn),sjabloon=VALUES(sjabloon),conclusie=VALUES(conclusie),
save=VALUES(save);
INSERT INTO sterren (beoordelingid,userid,werkid,titelwerk,actie,sterren)
VALUES ('$beoordelingid','$user','$id','$titelwerk',
'$actie','$sterren'); COMMIT");
I adapted the following. The insert1 still won't work. Insert2 works
$dbo->beginTransaction();
//NOT WORKING
$insert1 = $dbo->prepare("INSERT INTO beoordelingen(beoordelingid, userid, werkid, inlijn, sjabloon, conclusie, save)
VALUES (:beoordelingid, :user, :werkid, :inlijn, :sjabloon, :conclusie, '0')
ON DUPLICATE KEY UPDATE
beoordelingid=VALUES(beoordelingid),
userid=VALUES(userid),
werkid=VALUES(werkid),
inlijn=VALUES(inlijn),
sjabloon=VALUES(sjabloon),
conclusie=VALUES(conclusie),
save=VALUES(save);"
);
//THIS WORKS
$insert2 = $dbo->prepare("INSERT INTO sterren (beoordelingid, userid, werkid, titelwerk, actie, sterren)
VALUES (:beoordelingid, :user, :id, :titelwerk, :actie, :sterren);"
);
//NOT WORKING
$insert1->execute(array(
'beoordelingid' => $beoordelingid,
'user' => $user,
'werkid' => $werkid,
'inlijn' => $inlijn,
'sjabloon' => $sjabloon,
'conclusie' => $conclusie,
'save' => $save
));
//THIS WORKS
$insert2->execute(array(
'beoordelingid' => $beoordelingid,
'user' => $user,
'id' => $werkid,
'titelwerk' => $titelwerk,
'actie' => $actie,
'sterren' => $sterren
));
$dbo->commit();
My last problem:
$sterren=$_POST['sterren'];
$user=$_REQUEST['user'];
$userid=$_POST['userid'];
$actie=$_POST['actie'];
$dbo->beginTransaction();
//THIS WORKS
$insert1 = $dbo->prepare("INSERT INTO sterren(sterren,userid,actie)
VALUES (:sterren, :userid, :actie);"
);
//THIS DOESN"T WORK
$insert2 = $dbo->prepare("INSERT INTO sterren(sterren,userid,actie)
VALUES (:-sterren, :user, :actie);"
);
//THIS WORKS
$insert1->execute(array(
'sterren' => $sterren,
'userid' => $userid,
'actie' => $actie
));
//THIS DOESN"T WORK
$insert2->execute(array(
'-sterren' => $sterren,
'user' => $user,
'actie' => $actie
));
$dbo->commit();
This is not how to use prepared statements (->prepare()) or how to use a transaction with PDO.
First off, prepare() is for "preparing" one SQL query with placeholders so that you can pass the parameters in a later function (execute()) and avoid SQL injection from concatenating strings.
Second, if you want to use a transaction then you should use PDO's beginTransaction and commit functions.
Try something like this:
$dbo->beginTransaction();
$insert1 = $dbo->prepare('INSERT INTO beoordelingen(beoordelingid, userid, werkid, inlijn, sjabloon, conclusie, save)
VALUES (:beoordelingid, :user, :werkid, :inlijn, :sjabloon, :conclusie, :save)
ON DUPLICATE KEY UPDATE
beoordelingid=VALUES(beoordelingid),
userid=VALUES(userid),
werkid=VALUES(werkid),
inlijn=VALUES(inlijn),
sjabloon=VALUES(sjabloon),
conclusie=VALUES(conclusie),
save=VALUES(save);'
);
$insert2 = $dbo->prepare('INSERT INTO sterren (beoordelingid, userid, werkid, titelwerk, actie, sterren)
VALUES (:beoordelingid, :user, :id, :titelwerk, :actie, :sterren);'
);
$insert1->execute(array(
'beoordelingid' => $beoordelingid,
'user' => $user,
'werkid' => $werkid,
'inlijn' => $inlijn,
'sjabloon' => $sjabloon,
'conclusie' => $conclusie
));
$insert2->execute(array(
'beoordelingid' => $beoordelingid,
'user' => $user,
'id' => $id,
'titelwerk' => $titelwerk,
'actie' => $actie,
'sterren' => $sterren
));
$dbh->commit();

Codeigniter trans_start() not rolling back all queries

I have 3 insert queries running between trans_start() and trans_complete(). They are running perfectly fine... The only problem is, when I run rollback before complete it only rollback insert query 1 and 3. These are the only queries rolling back even I tried to change its positions and no success. Please let me know what I am doing wrong? I tried writing queries by $this->db->query(...) instead of using active records but its giving same result...
I have the following code.
$this->db->trans_start();
//insert transaction
$i_data = array(
'user_id' => $app['opp_id']
, 'amount' => $app['total']
, 'type' => 1
, 'feb_bal' => 1
, 'note' => 'Earnings'
);
$this->db->insert('transaction', $i_data);
$tran_id = $this->db->insert_id();
//insert 2
$c_data = array(
'oppt_opp_id' => $app['id']
, 'status' => 0 //closed
);
$this->db->insert('progress', $c_data);
//insert 3
$e_data = array(
'tran_id' => $tran_id
, 'app_id' => $app['id']
, 'type' => 1
, 'status' => 2
);
$this->db->insert('earn_spend', $e_data);
$this->db->trans_rollback();
$this->db->trans_complete();
CodeIgniter's database abstraction allows you to use transactions with
databases that support transaction-safe table types. In MySQL, you'll
need to be running InnoDB or BDB table types rather than the more
common MyISAM.
Documentation: http://ellislab.com/codeigniter/user-guide/database/transactions.html
And could you try following codes? I prepared codes according to the CI document
<?php
$this->db->trans_begin();
//insert transaction
$i_data = array(
'user_id' => $app['opp_id']
, 'amount' => $app['total']
, 'type' => 1
, 'feb_bal' => 1
, 'note' => 'Earnings'
);
$this->db->insert('transaction', $i_data);
$tran_id = $this->db->insert_id();
//insert 2
$c_data = array(
'oppt_opp_id' => $app['id']
, 'status' => 0 //closed
);
$this->db->insert('progress', $c_data);
//insert 3
$e_data = array(
'tran_id' => $tran_id
, 'app_id' => $app['id']
, 'type' => 1
, 'status' => 2
);
$this->db->insert('earn_spend', $e_data);
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
}
else
{
$this->db->trans_commit();
}
?>

How to search value from serialized data n mysql?

I want to search data which is stored in serialized form in this table.
Table Structure
For
meta_key - mgm_member_options
meta_value -
a:31:{s:2:"id";s:1:"1";s:13:"custom_fields";a:6:{s:10:"first_name";s:5:"Admin";s:9:"last_name";s:5:"Basil";s:7:"address";s:0:"";s:4:"city";s:0:"";s:5:"state";s:0:"";s:3:"zip";s:0:"";}s:22:"other_membership_types";a:0:{}s:12:"payment_info";a:0:{}s:6:"coupon";a:0:{}s:7:"upgrade";a:0:{}s:6:"extend";a:0:{}s:4:"code";s:10:"mgm_member";s:4:"name";s:10:"Member Lib";s:11:"description";s:10:"Member Lib";s:7:"setting";a:0:{}s:6:"saving";s:1:"1";s:8:"trial_on";s:1:"0";s:10:"trial_cost";s:1:"0";s:14:"trial_duration";s:1:"0";s:19:"trial_duration_type";s:1:"d";s:16:"trial_num_cycles";s:1:"0";s:8:"duration";s:1:"0";s:13:"duration_type";s:1:"m";s:6:"amount";s:1:"0";s:8:"currency";s:3:"USD";s:9:"join_date";s:0:"";s:13:"last_pay_date";s:0:"";s:11:"expire_date";s:0:"";s:15:"membership_type";s:5:"guest";s:6:"status";s:8:"Inactive";s:12:"payment_type";s:0:"";s:13:"autoresponder";s:0:"";s:10:"subscribed";s:0:"";s:9:"rss_token";s:32:"af8214c978dedcaa066bd8c223b7d22d";s:13:"user_password";s:16:"m9ROVxRwfxWwJzti";}
I want to find zip code.
How can do this using sql query?
Query that i am using
if(isset($_GET['as_zip']) && !empty($_GET['as_zip'])){
$as_zip = $_GET['as_zip'];
array_push($meta_query_array, array('key' => 'mgm_member_options', 'value' => $as_zip, 'compare' => '='));
} // prepare arguments $args = array( 'role' => $role, 'number' => '10', 'orderby' => 'display_name', 'fields' =>
'all_with_meta', 'meta_query' => $meta_query_array );