i am stuck to get mysql last increment id :(
i just tried mysql_insert_id but getting 0 instead of last increment id :(
Here is the array and insert fucntion call
$input_data = array(
'id' => '',
'booking_date' => $_POST['booking_date'],
'event_name' => $_POST['ev_name'],
'exb_comp_name' => $_POST['exb_comp_name'],
'address' => $_POST['address'],
'city' => $_POST['city'],
'country' => $_POST['country'],
'tel1' => $_POST['tel1'],
'tel2' => $_POST['tel2'],
'email' => $_POST['email'],
'package' => $_POST['package'],
'stand_no' => $_POST['stand_no'],
'hall_no' => $_POST['hall_no'],
'area' => $_POST['area'],
'contact_per' => $_POST['contact_per'],
'desg' => $_POST['desg'],
'cell_no' => $_POST['cell_no'],
'url' => $_POST['url'],
);
echo get_insert_id('exb_reg',$input_data);
And here is the function code
function get_insert_id($table_name, $data){
global $connection;
$value_a = '';
$value_b = '';
foreach($data as $field => $values){
$value_a .= "`".$field."`,";
$value_b .= "'".$values."',";
}
$value_a = substr($value_a, 0, strlen($value_a)-1);
$value_b = substr($value_b, 0, strlen($value_b)-1);
$query = "INSERT INTO `$table_name` ($value_a) VALUES ($value_b)";
$result = mysqli_query($connection,$query);
$id = mysql_insert_id();
return $id;
}
i tried echo in function but at the end result is 0 :(
You've switched database libraries. Use mysqli_ throughout, it isn't compatible with the deprecated mysql_ library.
http://php.net/manual/en/mysqli.insert-id.php
Related
Looking for some advice.
Trying to return a long query with around 65,000 results that has a lot of aggeration on the server-side. Takes around 16 seconds to return all the data.
This is what I have so far.
It's returning the query results but never seems to store in Memcached.
If I print $cached_data, it always says false. Thank you.
<?php
header('Content-Type: application/json');
include('pdo.php');
$memcache = new Memcache();
$memcache->addServer("127.0.0.1", 11211);
$query = "SELECT TOP 5000 Snumber,Number,Name,Status,dials,Size,Y04,Y05,PD0405,Y06,PD0506,Y07,PD0607,Y08,PD0708,Y09,PD0809,Y10,PD0910,Y11,PD1011,Y12,PD1112,Y13,PD1213,Y14,PD1314,Y15,PD1415,Y16,PD1516,Y17,PD1617,Y18,PD1718,Y19,PD1819,Y20,PD1920 FROM table ORDER BY Snumber";
$key = md5($query);
$cached_data = $memcache->get($key);
$response = [];
if ($cached_data != null) {
$result = $cached_data;
} else {
$statement = $conn->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$memcache->set($key, $result);
}
foreach ($result as $row) {
$output[] = array(
'Snumber' => $row['Snumber'],
'MeterNumber' => $row['Number'],
'Account' => $row['Name'],
'Status' => $row['Status'],
'dialsHI' => $row['dials'],
'MeterSize' => $row['Size'],
'Y04' => $row['Y04'],
'Y05' => $row['Y05'],
'PD0405' => $row['PD0405'],
'Y06' => $row['Y06'],
'PD0506' => $row['PD0506'],
'Y07' => $row['Y07'],
'PD0607' => $row['PD0607'],
'Y08' => $row['Y08'],
'PD0708' => $row['PD0708'],
'Y09' => $row['Y09'],
'PD0809' => $row['PD0809'],
'Y10' => $row['Y10'],
'PD0910' => $row['PD0910'],
'Y11' => $row['Y11'],
'PD1011' => $row['PD1011'],
'Y12' => $row['Y12'],
'PD1112' => $row['PD1112'],
'Y13' => $row['Y13'],
'PD1213' => $row['PD1213'],
'Y14' => $row['Y14'],
'PD1314' => $row['PD1314'],
'Y15' => $row['Y15'],
'PD1415' => $row['PD1415'],
'Y16' => $row['Y16'],
'PD1516' => $row['PD1516'],
'Y17' => $row['Y17'],
'PD1617' => $row['PD1617'],
'Y18' => $row['Y18'],
'PD1718' => $row['PD1718'],
'Y19' => $row['Y19'],
'PD1819' => $row['PD1819'],
'Y20' => $row['Y20'],
'PD1920' => $row['PD1920']);
}
echo json_encode($output, JSON_PRETTY_PRINT);
I am having a problem passing a variable to be saved as INT in MySQL. The closest thing I could find to my issue is this question but I have already verified that I am not using single and double quotes. When I print_r($this->session->userdata('subdomain')) I see the correct subdomain id on my screen (in this case 10), but when I try passing this using $params it always save subdomain_id as 0 and I can not figure out why.
Here is Project_assignees_model:
function add_prefix_project_assignees($params)
{
$this->db->insert('prefix_project_assignees',$params);
return $this->db->insert_id();
}
Here is my Project controller (Specifically it is the $assignees array):
function edit($entry_id)
{
$this->load->library('form_validation');
$this->form_validation->set_rules('client_id','Client Id','required|integer');
$this->form_validation->set_rules('title','Project Title','required|max_length[255]');
$this->form_validation->set_rules('project_status','Project Status','required');
$this->form_validation->set_rules('project_details','Project Details','required');
$this->form_validation->set_rules('project_estimated_hours','Project Estimated Hours','required|max_length[255]');
$this->form_validation->set_rules('end','Project Deadline','required');
if($this->form_validation->run())
{
date_default_timezone_set("America/New_York");
$date = date("Y-m-d h:i:sa");
$params = array(
'subdomain_id' => $this->session->userdata('subdomain'),
'client_id' => $this->input->post('client_id'),
'title' => $this->input->post('title'),
'project_status' => $this->input->post('project_status'),
'project_details' => $this->input->post('project_details'),
'project_last_updated' => $date,
'project_estimated_hours' => $this->input->post('project_estimated_hours'),
'start' => $this->input->post('start'),
'end' => $this->input->post('end'),
'owner_id' => get_current_user_id(),
);
$prefix_projects_id = $this->Project_model->update_prefix_projects($entry_id,$params);
$assignees = array(
'subdomain_id' => $this->session->userdata('subdomain'),
'project_id' => $entry_id,
'user_id' => $this->input->post('assignee'),
);
$this->load->model('Project_assignee_model');
$insertAssignees = array();
for ($i = 0; $i < count($assignees['user_id']); $i++)
{
//Get user_id from ID
$userID = $assignees['user_id'][$i];
$insertAssignees[$i] = array('project_id' => $entry_id, 'user_id' => $assignees['user_id'][$i]); // $insert is ready for insert_batch
$prefix_assignees = $this->Project_assignee_model->add_prefix_project_assignees($insertAssignees[$i]);
}
//redirect('project/index');
}
else
{
$this->load->model('Client_model');
$data['prefix_clients'] = $this->Client_model->get_all_prefix_clients();
$data['prefix_projects'] = $this->Project_model->get_prefix_projects($entry_id);
$data['prefix_users'] = $this->aauth->list_users();
$this->load->model('Project_assignee_model');
$data['prefix_assignees'] = $this->Project_assignee_model->get_prefix_project_assignees($entry_id);
$this->load->view('project/edit',$data);
}
}
Why does print_r show the correct subdomain_id but when it is saved to database it saves as 0?
*******UPDATE*******
Just for reference, this works:
$assignees = array(
'subdomain_id' => 10,
'project_id' => $entry_id,
'user_id' => $this->input->post('assignee'),
);
But this does not (Even though printing the userdata for subdomain gives me 10):
$assignees = array(
'subdomain_id' => $this->session->userdata('subdomain'),
'project_id' => $entry_id,
'user_id' => $this->input->post('assignee'),
);
Sometimes I guess it just takes writing it out for others before I realize my problem. When I loop through each assignee to add them to the table I realized I was not passing the subdomain_id with it.
I had this:
$insertAssignees[$i] = array('project_id' => $entry_id, 'user_id' => $assignees['user_id'][$i]); // $insert is ready for insert_batch
Needed to be changed to have subdomain_id included
$insertAssignees[$i] = array('subdomain_id' => $this->session->userdata('subdomain'), 'project_id' => $entry_id, 'user_id' => $assignees['user_id'][$i]); // $insert is ready for insert_batch
i got this model which inserts data on the database but the problem is, other fields are not required so i did this,
public function insert(){
if($this->input->post('MNAME') = NULL){
$mname = "n/a";
}else{
$mname = $this->input->post('MNAME');
}
if($this->input->post('EXTENSION') = NULL){
$ext = "n/a";
}else{
$ext = $this->input->post('EXTENSION');
}
if($this->input->post('EMAIL') = NULL){
$email = "n/a";
}else{
$email = $this->input->post('EMAIL');
}
if($this->input->post('CELLNO') = NULL){
$cell = "n/a";
}else{
$cell = $this->input->post('CELLNO');
}
if($this->input->post('AGENCY_NO') = NULL){
$agency = "n/a";
}else{
$agency = $this->input->post('AGENCY_NO');
}
$input = array(
'ID_NUM' => $this->uri->segment(3),
'FNAME' => $this->input->post('FNAME' ),
'SURNAME' => $this->input->post('SURNAME' ),
'DO_BIRTH' => $this->input->post('DO_BIRTH' ),
'POBIRTH' => $this->input->post('POBIRTH' ),
'SEX' => $this->input->post('SEX' ),
'CIVILSTAT' => $this->input->post('CIVILSTAT' ),
'ID_NAT' => $this->input->post('ID_NAT' ),
'HEIGHT' => $this->input->post('HEIGHT' ),
'WEIGHT' => $this->input->post('WEIGHT' ),
'BLOOD_TYPE' => $this->input->post('BLOOD_TYPE' ),
'RES_ADD' => $this->input->post('RES_ADD' ),
'RES_ZIP' => $this->input->post('RES_ZIP' ),
'PERM_ADD' => $this->input->post('PERM_ADD' ),
'PERM_ZIP' => $this->input->post('PERM_ZIP' ),
'MNAME' => $mname,
'EXTENSION' => $ext,
'EMAIL' => $email,
'CELLNO' => $cell,
'AGENCY_NO' => $agency,
'DATE_CREATED' => date("Y-m-d")
);
$insert = $this->db->insert($this->table,$input);
return $insert;
}
but the problem is that i get this error Can't use method return value in write context says that its on line 108. which, the line 108 is the first if of my model. what is my error? and is there any codes which will be shorter?
You're only stating 1 = in your IF statements, for future note: It should be 2 ='s in a match.
I restructured this for you so your code is more readable and short.
// Checking if they're null and appending n/a if they're
$example = array('MNAME','EXTENSION','EMAIL', 'CELLNO', 'AGENCY_NO');
$new = array();
foreach ($example as $_ex)
{
$check = $this->input->post($_ex);
if ($check == null)? array_push($new, 'n/a') : array_push($new, $check);
}
Then replace with this (keeping in mind index's start at 0 in an array):
// Inside your DB insert query
'MNAME' => $new[0],
'EXTENSION' => $new[1],
'EMAIL' => $new[2],
'CELLNO' => $new[3],
'AGENCY_NO' => $new[4],
I hope this helped.
I got a function that requests some information from my database, puts it in an array and returns it. The sql-statement is correct.
When the current date is not found in the database, I would like to display an error on my website.
The error message, is added also in the return array.
First I check if the corresponding date exists in the database with a COUNT, if the count == 1, I get all the data from the database with another statement. If the count != 1, I put together an array hard code.
When a date is found in the database that is the same as the given parameter, the script works like a charm, but when I change the date in the database, the I get the following error:
Notice: Undefined variable: row in
/Applications/MAMP/htdocs/models/funcs.php on line 1257
Line 1252-1257:
while($stmt->fetch())
{
$row[] = array('id' => $id, 'datum' => $datum, 'mac' => $mac, 'ipad' => $ipad, 'iphone' => $iphone, 'imember' => $imember, 'applecare' => $applecare, 'verkoop_ochtend' => $verkoop_ochtend, 'verkoop_middag' => $verkoop_middag, 'verkoop_avond' => $verkoop_avond, 'vracht_ochtend' => $vracht_ochtend, 'vracht_middag' => $vracht_middag, 'vracht_avond' => $vracht_avond, 'service_ochtend' => $service_ochtend, 'service_middag' => $service_middag, 'service_avond' => $service_avond, 'werkzaamheden' => $werkzaamheden, 'error' => '');
}
$stmt->close();
$data = $row;
Does anyone know what I'm doing wrong here? Thanks in advance!
The complete function:
function get_current_planning($date)
{
GLOBAL $mysqli, $db_table_prefix;
$stmt = $mysqli->prepare("SELECT COUNT(datum) FROM " . $db_table_prefix . "planning WHERE datum = '" . $date . "'");
$result = $stmt->execute();
print_r($result);
var_dump($result);
if($result == 1)
{
$stmt->prepare("SELECT
*
FROM " . $db_table_prefix . "planning
WHERE datum = '" . $date . "'");
$stmt->execute();
$stmt->bind_result($id, $datum, $mac, $ipad, $iphone, $imember, $applecare, $verkoop_ochtend, $verkoop_middag, $verkoop_avond, $vracht_ochtend, $vracht_middag, $vracht_avond, $service_ochtend, $service_middag, $service_avond, $werkzaamheden);
while($stmt->fetch())
{
$row[] = array('id' => $id, 'datum' => $datum, 'mac' => $mac, 'ipad' => $ipad, 'iphone' => $iphone, 'imember' => $imember, 'applecare' => $applecare, 'verkoop_ochtend' => $verkoop_ochtend, 'verkoop_middag' => $verkoop_middag, 'verkoop_avond' => $verkoop_avond, 'vracht_ochtend' => $vracht_ochtend, 'vracht_middag' => $vracht_middag, 'vracht_avond' => $vracht_avond, 'service_ochtend' => $service_ochtend, 'service_middag' => $service_middag, 'service_avond' => $service_avond, 'werkzaamheden' => $werkzaamheden, 'error' => '');
}
$stmt->close();
$data = $row;
}
else
{
$row[] = array('id' => '', 'datum' => '', 'mac' => '', 'ipad' => '', 'iphone' => '', 'imember' => '', 'applecare' => '', 'verkoop_ochtend' => '', 'verkoop_middag' => '', 'verkoop_avond' => '', 'vracht_ochtend' => '', 'vracht_middag' => '', 'vracht_avond' => '', 'service_ochtend' => '', 'service_middag' => '', 'service_avond' => '', 'werkzaamheden' => '', 'error' => 'Er is geen planning gevonden voor de huidige datum!');
$stmt->close();
$data = $row;
}
return $data;
}
print_r($result) returns 1
var_dump($result) returns bool(true)
You're not fetch()ing the the result of your SELECT COUNT()... query. It looks to me like your while(...fetch()) loop in your second query is sometimes running zero times. This will result in the code beginning $row[] = getting run no times, which in turn will result in $row turning up undefined after the while(...fetch()) loop.
At any rate it's a bit wasteful to count the rows and then fetch them, with two consecutive queries. You may want to skip the SELECT COUNT query entirely. Instead, do this sort of thing:
$row = Array();
while($stmt->fetch()) {
$row[] = array('id' => $id, 'datum' => $datum, 'mac' => $mac, 'ipad' => $ipad, 'iphone' => $iphone, 'imember' => $imember, 'applecare' => $applecare, 'verkoop_ochtend' => $verkoop_ochtend, 'verkoop_middag' => $verkoop_middag, 'verkoop_avond' => $verkoop_avond, 'vracht_ochtend' => $vracht_ochtend, 'vracht_middag' => $vracht_middag, 'vracht_avond' => $vracht_avond, 'service_ochtend' => $service_ochtend, 'service_middag' => $service_middag, 'service_avond' => $service_avond, 'werkzaamheden' => $werkzaamheden, 'error' => '');
}
$stmt->close();
if (0 == count($row)) {
/* deal with the no matching rows case */
}
$data = $row;
It looks like you haven't defined a variable $row. You need a $row=Array(); before you can start treating $row as an array.
I have a query that returns multiple rows. I can't seem to find a way to store the rows in the $params array. Is there a way to loop throw and store each row in the $params variable
$aResult = $db->exec_sql($sql);
$params = array(
// where $aResult[o]'' would be row 1 [1] row 2 etc. //
'store_id' => $aResult[]['iStoreID'],
'user_id' => $aResult[]['iUserID'],
'store_name' => $aResult[]['cStoreName'],
'store_url' => $aResult[]['cStoreURL'],
'rid' => $aResult[]['cRID'],
'affiliate_id' => $aResult[]['iAffiliateID'],
'team_id' => $aResult[]['iTeamID'],
'bizOP' => $aResult[]['cDefaultBizOpp'],
'showBizOPp' => $aResult[]['iShowBizOppDropdown'],
'boPosting' => $aResult[]['iEnableBOPosting'],
'brandinglevel' => $aResult[]['iBrandingLevel']
);
thank you for your help
As simple as that:
$params = array();
foreach($aResult as $row) {
$params[] = array(
'store_id' => $row['iStoreID'],
'user_id' => $row['iUserID'],
'store_name' => $row['cStoreName'],
'store_url' => $row['cStoreURL'],
'rid' => $row['cRID'],
'affiliate_id' => $row['iAffiliateID'],
'team_id' => $row['iTeamID'],
'bizOP' => $row['cDefaultBizOpp'],
'showBizOPp' => $row['iShowBizOppDropdown'],
'boPosting' => $row['iEnableBOPosting'],
'brandinglevel' => $row['iBrandingLevel']
);
}
Without knowing the exact structure of the result array i guess you need something like this:
<?php
$params = array();
$mapping = array(
'store_id' => 'iStoredID',
'user_id' => 'iUserID',
// and so on...
);
foreach ($aResult as $row) {
$tempRow = array();
foreach ($row as $key => $value) {
$paramKey = isset($mapping[$key]) ? $mapping[$key] : $key;
$tempRow[$paramKey] = $value;
}
$params[] = $tempRow;
}
I use it like this
$aResult = mysql_query($sql);
while($data = mysql_fetch_array($result)) {
'store_id' = $aResult['iStoreID'];
}
At least that is the idea