function renew_status($propertyId){
$code = '';
$code = mt_rand(500000, 999999);
$q = "UPDATE `ps_listings` SET
`accessCode` = $code,
`renew` = '0'
WHERE `id` = $propertyId ";
mysql_query($q) or die(mysql_error());
$this->db->select('accessCode')->from('ps_listings');
$this->db->where(array('id' => $propertyId));
$query = $this->db->get();
$results = $query->result();
return $results[0]->accessCode;
}
I am updating a mysql table through codeigniter function. first time when i access my function it updates wrong number but if i refresh the page correct value is updated.
return $results[0]->accessCode; gives me this code 893195. while in database it saves 997228. Please Help me
Try this
function renew_status($propertyId){
$code = '';
$code = mt_rand(500000, 999999);
$data = array('accessCode' => $code, 'renew' => '0');
$this->db->where('id',$propertyId);
$this->db->update('ps_listings',$data);
$this->db->select('accessCode');
$this->db->where('id',$propertyId);
$query = $this->db->get('ps_listings');
$results = $query->result_array();
return $results[0]->accessCode;
}
1) Use mysql_affected_rows for check works update or no.
2) I think refresh page is wrong way. Use Session for deny double update.
3) For debugging retrieve and print accessCode before updating, after, and in
result(UI).
1) is "id" column unique and not null?
2) what does the following code returns :
echo count($results);
Related
I have a query that returns all the data while running at MSSQL, but when I try to get the result with php code it returns null
SELECT:
$query = "SELECT DISTINCT (E080SER.desser) as desser,
E080SER.CODFAM codfam, e085cli.apecli apecli,
E085CLI.CODCLI codcli, E085CLI.NOMCLI nomeCli
FROM
E160CTR,
E160CVS, e080ser,
E085CLI,
E070EMP,
E070FIL
WHERE
e070emp.nomemp like '%Gestão tech%' and
e080ser.codser = e160cvs.codser and
e080ser.codser like ('%manw%') and (E160CTR.CODEMP = 1) and
((E160CTR.CODEMP = E070FIL.CODEMP) AND (E160CTR.CODFIL =
E070FIL.CODFIL) AND
(E160CTR.CODCLI = E085CLI.CODCLI) AND (E160CVS.CODEMP =
E160CTR.CODEMP) AND
(E160CVS.CODFIL = E160CTR.CODFIL) AND (E160CVS.NUMCTR =
E160CTR.NUMCTR)) AND
(E160CTR.SITCTR = 'A') and e080ser.sitser = 'a' and
E080SER.CODEMP IN (1, 9)
order by e080ser.desser";
PHP CODE:
$sql = sqlsrv_query($conn, $query);
while($item = sqlsrv_fetch_array($sql)){
var_dump($item);
}
Sometimes it's necessary to fetch all result sets with sqlsrv_next_result() to get your data. You may try with this:
<?php
...
$sql = sqlsrv_query($conn, $query);
do {
while($item = sqlsrv_fetch_array($sql)){
var_dump($item);
}
} while (sqlsrv_next_result($sql));
...
?>
There is an extra semicolon after the while loop, i.e. the body of the loop is empty. Then the result you try to read is after the last row, that's why you don't get what you expected.
I've found the problem
The problem was the encoding, I put the $query inside of utf8_encode(), and now it is returning the results.
Thank you all for your time.
I use codeigniter and store my data like 123,234 .
My table name is schedule and column name batch.
$batch = 123;
$lot =1;
$dataTest = $this->db->query("select q_set from schedule where lot='$lot'
and batch='$batch'")->row();
see my image below
I try to select one value and match my sent value. ## how to match both the values ##
the value of column batch in database is '123,321', and the variable $batch is 123,they are not equal so you can't select it.
you must send a data equal to the one in your database.
try
$lot=1;
$batch = 123321; //or $batch='123,321' i'm not sure the date type of variable $batch
$dataTest = $this->db->query("select q_set from schedule where lot='$lot' and batch='$batch'")->row();
Try this
$batch = "123,321";
$lot =1;
$dataTest = $this->db->query("select q_set from schedule where lot=".$lot."
and batch='".$batch."')->row();
or use
$batch = "123";
$lot =1;
$dataTest = $this->db->query("select q_set from schedule where lot=".$lot."
and batch like '%".$batch."')->row();
Using query builder. Please note if batch is a string it should be $batch = "123,234" plus you can't have commas with integers.
$this->db->select('q_set');
$this->db->where('lot', $lot);
$this->db->where('batch', $batch);
$q = $this->db->get('schedule');
if ($q->num_rows() > 0}
print_r($q->row());
} else {
echo 'no rows';
}
you can also use find_in_set.but it mostly used to find comma seperated values.
$this->db->select('q_set');
$this->db->from("schedule");
$this->db->where("FIND_IN_SET('$lot', lot)");
$this->db->where("FIND_IN_SET('$batch', batch)");
--------------OR---------------
you can try this..
$this->db->select('q_set');
$this->db->from('schedule as S1');
$this->db->where('S1.lot',$lot);
$this->db->where('S1.batch',$batch);
$query = $this->db->get();
return $query->row();
You can use IN Operator,
$result = $this->db->select('*')->from($table);
$result = $this->db->where('lot =',$lot);
$result = $this->db->where_in('batch',$batch);
$result = $this->db->order_by('id','desc');
$result = $this->db->get();
The where_in() operator checks if the value exists in the specified column
I am on point where I have to usk on forum.
So, I have an array that is my return from join table sql query.
i am displaying it correctly without the problem.
but some of those values I want to put in different table of mysql database.
$array = joint_table();
$array_value = array['key'];
I can echo array_value and it's displaying correctly, also checked variable type and it returns STRING.
however when I am inserting it into the table, it's empty cell.
I am inserting other stuff like date() and such and that is inserted correctly.
So my sql query works fine, besides I am using same query in other places without problem.
Only values I have from that array are not inserting, but still can echo them.
<?php
$page_title = 'Complete Task';
require_once('includes/load.php');
// Checkin What level user has permission to view this page
page_require_level(2);
$task = join_task_table((int)$_GET['id']);
?>
<?php
if(isset($_POST['complete_task'])){
$area = $task['area'] ;
$jig = $task['jig'];
$desc = $task['description'];
$freq = $task['freq'];
$date = make_date();
$user = current_user();
$user_done = remove_junk(ucfirst($user['name']));
$comment = remove_junk($db->escape($_POST['comment']));
if(empty($errors)){
$sql = "INSERT INTO tpm_history (area_name,jig_name,description,frequency,date_done,done_by_user,comment)";
$sql .= " VALUES ('{$area}','{$jig}','{$desc}','{$freq}','{$date}','{$user_done}','{$comment}')";
$result = $db->query($sql);
if($result && $db->affected_rows() === 1){
$session->msg('s',"Job Completed");
redirect('home.php', false);
} else {
$session->msg('d',' Sorry failed to complete the task!');
redirect('task_complete.php?id='.$task['id'], false);
}
} else{
$session->msg("d", $errors);
redirect('task_complete.php?id='.$task['id'],false);
}
}
?>
I am lost. Help.
I am trying to get a list of coupons through ajax when the checkboxes are selected. So everything else is working fine but the query is returning only the first match.
So my query is:
$this->db->from('tbl_coupons');
if($storeids !=''){
$ids = array($storeids);
$this->db->where_in('coupon_store', $ids );
}
$this->db->where('coupon_cat', $catid);
$this->db->where('coupon_status', 'active');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$ds = $query->result_array();}
According to this my SQLquery becomes
SELECT * FROM `tbl_coupons`
WHERE `coupon_store` IN('1,97')
AND `coupon_cat` = '16'
AND `coupon_status` = 'active'
But this query is returning values with coupon_store=1 and no results are coming for coupon_store=97
I checked values for coupon store 97 which exists in that category.
use below way if data exist it will be part of query.
storeids = explode(',',storeids);
$ids = array();
foreach($storeids as $val){
$ids[] = $val;
}
if(!empty($ids)){
$this->db->where_in('coupon_store', $ids );
}
hope it will create proper sql query
The query is mostly correct, except at line 2, where you need to make the change as:
WHERE coupon_store IN('1','97')
everything else remains the same.
I built PHP 2 year ago and now i want to change all about database to PDO,i have some problem with update table. I use this function to update table.
public function update($tabel, $fild = null ,$where = null)
{
$update = 'UPDATE '.$tabel.' SET ';
$set=null; $value=null;
foreach($fild as $key => $values)
{
$set .= ', '.$key. ' = :'.$key;
$value .= ', ":'.$key.'":"'.$values.'"';
}
$update .= substr(trim($set),1);
$json = '{'.substr($value,1).'}';
$param = json_decode($json,true);
if($where != null)
{
$update .= ' WHERE '.$where;
}
$query = parent::prepare($update);
$query->execute($param);
$rowcount = $query->rowCount();
return $rowcount;
}
everything work fine using this
$updatefild = array('count' => 20);
$where = "id = '123'";
echo $db->update("mytable",$updatefild, $where);
but i get problem when i want to update row with existing row, in mysql_query I usually use
mysql_query("update mytable set count=count+1 where id='123'");
how i achieve that use PDO ?
thanks
First, why are you using JSON just to decode it into an array? That is confusing.
Secondly, if you were trying to add a number to an existing field, you don't even need prepare().
You could just do
PDO->query("update mytable set count=count+".intval($int)." where id='123'");
If you were doing prepare, you could do:
$stmt = PDO->prepare("update mytable set count=count+:count where id='123'");
$stmt->execute(array(':count' => 1));
or
$stmt = PDO->prepare("update mytable set count=count+? where id='123'");
$stmt->execute(array(1));
Edit: You wouldn't be able to do it with how your function is written as you can't bind column names. PDO will quote it as a standard string. You would have to find a work around, possibly including the =count in the field somehow.