How to retrieve last inserted user id? I have to use that user id for inserting that user id into next query that also should be done in this transaction only.
my query:
$db->beginTransaction ();
$sql = $db->query ( "INSERT INTO user( user_id, title)
VALUES ( :p_user_id, :p_title )",
array ( 'p_user_id' => '', 'p_title' => $title ) );
You can use this method to retrive the last insert id :
$db->lastInsertId()
With you code :
$db->beginTransaction ();
$sql = $db->query ( "INSERT INTO user( user_id, title) VALUES ( :p_user_id, :p_title )",
array ( 'p_user_id' => '', 'p_title' => $title ) );
$db->lastInsertId() ;
Use insert() method for inserting data. Also it returns ID.
$db = new Zend_Db_Table('user');
$lastInsertId = $db->insert(array('user_id' => '', 'title' => $title));
Try this this should work:
$groupID = Engine_Db_Table::getDefaultAdapter()->lastInsertId();
Related
I have MySQL Query i want to write this query zend 2.
select billsec, call_status, Count(*)
from (
select
billsec,
if(ANSWERED_NUM is Null, 'Missed', 'Answered') call_status
from cust_info
where billsec in (
select id
from `users`
where `account_id` = 452 and `added_by` = 20694 and `status` = 'active'
)
)a
group by a.billsec,a.call_status
in zend 2 am trying to write like this but
$adapter = $this->getAdapter();
$resultset = $this->select( function( Select $select ) use ( $request, $adapter ) {
$sub1 = new Select( 'users' );
$sub1->columns( array( 'id' ) );
$sub1->where( array( 'account_id' => '452','added_by' => '20694','status' => 'active' ) );
$sub2 = new Select( 'cust_info' );
$sub2->columns(array("id","billsec", "if(ANSWERED_NUM is Null, 'Missed','Answered')"=>"call_status"));
$sub2->where('billsec IN(?)', new \Zend\Db\Sql\Expression( '?', array( $sub1 ) ));
var_dump( $sub2->getSqlString( $adapter->getPlatform() ) );die();
});
When am print this query output like this:
"SELECT `cust_info`.`id` AS `id`, `cust_info`.`billsec` AS `billsec`, `cust_info`.`call_status` AS `if(ANSWERED_NUM is Null, 'Missed','Answered')` FROM `cust_info` WHERE billsec IN('')"
Here am not able to write IN condition Query, thank in advance.
Can't figure out why this code isn't working:
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute([$SQL_values]);
And these are dumps of the two strings being inserted into those statements:
$SQL_update = UPDATE laptops SET asset_tag = :asset_tag WHERE id = :id
$SQL_values = 'asset_tag' => 5544, 'id' => 23
You missed : in your code:-
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute([':asset_tag' => 5544, ':id' => 23]);
So actually what you have to do is:-
$SQL_values =[':asset_tag' => 5544, ':id' => 23]; // create array like this
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute($SQL_values); // pass that array
Or
$SQL_values =['asset_tag' => 5544, 'id' => 23]; // create array like this
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute($SQL_values); // pass that array
Note:- execute won't accept a string, it must be an array.
I dont know why is not inserting the values of the array in the db, I parse the array, but the values in the db are not changing, I manually add some data to the db table :
public function widget( $args, $instance ) {
global $wpdb;
$table_name = $wpdb->prefix . "cbrrate";
$limit=12;
$i=$x=0;
$currency = array();
$codes = array('USD','EUR');
//creando la tabla
if( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)
{
$sql = 'CREATE TABLE ' .$table_name . '(
hit_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
code CHAR(3),
rate FLOAT UNSIGNED,
PRIMARY KEY (code) )';
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
//initialise an array to hold the values to be inserted
$sqlParts1=array();
//loop through the code to create an array with keys as value
foreach($codes as $code){
//add the rate to the sql query
$sqlParts1[] = "('$code', ".((float)$currency[0][$code]).") ";
}
$sql = "INSERT INTO $table_name (code, rate) VALUES ".implode(', ', $sqlParts1).' ON DUPLICATE KEY UPDATE rate = VALUES(rate)';
print_r($sql);
the array $currency is:
Array (
[0] => Array ( [USD] => 73.63 [EUR] => 79.97 )
[1] => Array ( [USD] => 74.05 [EUR] => 80.53 )
)
In PHP I'm attempting to build a form that needs to check for an ID in one table and if it exists it should create a record in another table. So far, that works but the issue I'm having is when I attempt to handle the case if the ID I checked for didn't exist. If it doesn't exist I'd like to create another one. But every time I try it, I get 500 errors from the server when I fetch the results.
Essentially I made the following function
function trySQL($con, $query, $params) {
$stmt = $con->prepare($query);
$result = $stmt->execute($params);
$id = $stmt->insert_id;
return array($stmt,$result,$id);
}
I call this function multiple times through out my php code but when I call it more then once and attempt to fetch the results it breaks.
$custINSquery = "
INSERT INTO custs (
FirstName,
LastName,
EmailAddress,
PhoneNumber
) VALUES (
:FirstName,
:LastName,
:EmailAddress,
:PhoneNumber
)
";
$createJob = "
INSERT INTO jobs (
custs_id,
StAddress,
State,
ZipCode,
MoistureLocation,
status_id
) VALUES (
:custs_id,
:StAddress,
:State,
:ZipCode,
:IssueDesc,
:status_id
)
";
$custSELquery = "SELECT id, FirstName, LastName, EmailAddress FROM custs WHERE FirstName = :FirstName AND LastName = :LastName AND EmailAddress = :EmailAddress";
$custSELquery_params = array(
':FirstName' => $_POST['FirstName'],
':LastName' => $_POST['LastName'],
':EmailAddress' => $_POST['EmailAddress']
);
$checkcust = trySQL($db, $custSELquery, $custSELquery_params);
$row = $checkcust[0]->fetch();
if(!$row){
$custINSquery_params = array(
':FirstName' => $_POST['FirstName'],
':LastName' => $_POST['LastName'],
':EmailAddress' => $_POST['EmailAddress'],
':PhoneNumber' => $_POST['PhoneNumber']
);
$custins = trySQL($db, $custINSquery, $custINSquery_params);
$custsel = trySQL($db, $custSELquery, $custSELquery_params);
$custs_id = $custsel[0]->fetch();
if ($custs_id != null) {
$createJobParam = array(
':custs_id' => $custs_id,
':StAddress' => $_POST['StAddress'],
':State' => $_POST['State'],
':ZipCode' => $_POST['ZipCode'],
':IssueDesc' => $_POST['MoistureLocation'],
':status_id' => $_POST['status_id']
);
$jobins = trySQL($db, $createJob, $createJobParam);
$jobres = $jobins[0]->fetch();
die("um...");
if ($jobres) {
# code...
die("looks like I made it");
}
}
} else {
$createJobParam = array(
':custs_id' => $row['id'],
':StAddress' => $_POST['StAddress'],
':State' => $_POST['State'],
':ZipCode' => $_POST['ZipCode'],
':IssueDesc' => $_POST['MoistureLocation'],
':status_id' => $_POST['status_id']
);
$data['success'] = true;
$data['message'] = 'Success!';
}
Additional Notes: When I look through the php doc's they are saying that I could use the inserted_id thing in order to get the ID that I inserted previously but when I try that it just gives me nulls with this set up.
Any help would be appreciated.
Thanks!
Im trying to insert the following array into my table to no avail. I tried several examples but none worked.
Each POST array contains two strings from an input form (each string from an input row) and the $data keys are of the same name of the columns in the table.
How do I insert this data into two rows with a single query?
$data = array(
'user_id' => $_POST['user_id'],
'order' => $_POST['order'],
'type' => $_POST['type'],
'series' => $_POST['series'],
'repetition' => $_POST['repetition'],
'load' => $_POST['load'],
'pause' => $_POST['pause']
);
EDIT.: Could swear I had copied the query.
$columns = implode(',',array_keys($data));
$values = implode(',',array_values($data));
$query = " INSERT INTO userdata ($columns) VALUES ($values)";
You can insert this type of array like this
$fields=" ".implode(",",array_keys($data))." ";
$data="'".implode ("','",$data)."'";
$query ="INSERT INTO table_name ($fields) VALUES ($data)";
if($query){
mysql_query($query);
}