I am running the following query in codeiginter, I want to return the data from the database but I'm getting a blank array as a response. My attempts to debug this issue have shown that the return value from the model may be the issue. I need a extra pair of eyes on this so if anyone can help me let me know.
Model
function test($term)
{
$sql = "select description from category where title = '$term'";
$query = $this->db->query($sql);
return $query->result();
}
Controller
function test()
{
$this->load->model('test');
$term = $this->input->post('term',TRUE);
$rows = $this->test->test($term);
echo json_encode($rows);
}
your code looks correct... try printing the last query that was made and check it out
COntroller
function test()
{
$this->load->model('test');
$term = $this->input->post('term',TRUE);
$rows = $this->test->test($term);
echo $this->db->last_query();exit; //this gives you the last query that was made.
echo json_encode($rows);
}
check if the posted value is correct... you can even try running this in mysql and see if this is returning any rows...
use echo $this->db->last_query(); and get you query and try it with your mysql editor
I notice by running echo $this->db->last_query(); the value of $term had a leading white space that cause the query to return null. by adding $term = trim($term, " "); I was able to remove the leading white space and get the correct result.
$this->load->model('tp_model');
$term = $this->input->post('term',TRUE);
$term = trim($term, " ");
$rows = $this->tp_model->test($term);
Related
My target is to show an echo "No data" when the query result is empty. What happening currently is that when my query is empty, it shows an error. (Please see the picture below for the error) I provided my codes below and my screenshot. Any help will be appreciated. Thank you
Controller:
public function new1($arenaID=0) {
$data['activeNav'] = "";
$data['subnav'] = "arenas";
$this->header($data);
$this->nav();
$data['k1']=$this->arenas->meron1();
$this->load->view('new', $data);
$this->footer();
}
Model:
function meron1(){
$query = $this->db->query("SELECT fightID FROM fight_entries where position='meron' ORDER BY fightID DESC LIMIT 1");
return $query->row()->fightID+1;
}
A better way to do this is just to check the number of rows being returned.
function meron1(){
$query = $this->db->query("SELECT fightID FROM fight_entries where position='meron' ORDER BY fightID DESC LIMIT 1");
if($query->num_rows() > 0){
$query = $query->rows();
return $query->fightID+1;
}else{
$message = "No Data";
return $message;
}
Here is my database
I want to get the number of received letters but it does not work. This is my code:
$letters3=LettersModel::orderby('id','desc')->get();
$parts= array();
foreach ($letters3 as $letters) {
$parts = explode(',',$letters->girandeh);
foreach($parts as $key=>$parts2){
if($parts2==$email){
echo count($parts2);
}
}
}
It will be displayed in the following way [here is my db][2]
I am a beginner.
I'm trying to fix, i think i need help.
-This is code Controller
if(Input::hasFile('image')){
$dest = 'media/images/product/';
$name = str_random(6).'_'.Input::file('image')->getClientOriginalName();
//$resize =
Input::file('image')->move($dest,$name);
}
$loaispname = Input::get('loaispname');
$loaisp = new Loaisp;
$datas = $loaisp->getidloaisp($loaispname);
$idloaisp = $datas->id;
$item = new Sanpham;
$item->loaisp_id = $idloaisp;
$item->sanpham_name = Input::get('sanpham');
$item->sanpham_img = $name;
$item->sanpham_tieude = Input::get('tieude');
$item->sanpham_gia = Input::get('gia');
$item->sanpham_chitiet = Input::get('chitiet');
$item->sanpham_vitri = Input::get('vitri');
$item->save();
return Redirect::to('admin/dsachsanpham')->with('thanhcong','Saved');
-This is code Model
public function getidloaisp($loaispname){
//return Loaisp::where('loaisp_name','=',$loaispname)->get();
return DB::table('loaisp')->where('loaisp_name',$loaispname)->first();
}
-this is Error
ErrorException (E_UNKNOWN)
Trying to get property of non-object
Open: E:\xampp\htdocs\www\daunhot\app\controllers\AdminController.php
$loaispname = Input::get('loaispname');
$loaisp = new Loaisp;
$datas = $loaisp->getidloaisp($loaispname);
$idloaisp = $datas->id; // This is error
$item = new Sanpham;
When you see this error there could be two error: You got nothing back with your query(DB::table('loaisp')->where('loaisp_name',$loaispname)->first();), Or you got back an array, if you are using ->get() instead of ->first() you will get this error.
Try to use a foreach on your query result, or try to use dd() function on your result so you can see if is empty or not. If it is empty that is your porblem.
Same as what the rest is saying, dump $datas before the error, maybe add '=' to your where clause in that 'getidloaisp' function. You should be able to use the one your quoted out though, that is if you change the '->get()' to '->first()'.
Check if you are getting 'loaispname' on Input::get by:
echo "<pre>";
print_r($loaispname);
echo "</pre>";
exit;
If you're getting your posted data then do:
echo "<pre>";
print_r($datas);
echo "</pre>";
exit;
$datas->id work with DB table is need to be the table datas and cell id, and also they are case sensitive, not use Id or other.
I am using Zend framework and Oracle, below is my save() function
public function save()
{
$time = strtotime($_POST['DeliveryDate']);
$deliveryDate = date('d-M-Y',$time);
try{
$result = '';
$dml = "INSERT INTO NOBL_TEC_EFT(SNO_PK, ITEM, ITEM_TYPE, QUANTITY, UNIT_PRICE, NEED_BY_DATE, SITE, DELIVER_TO_LOCATION, AFE, TASK, REQUESTOR, ATTRIBUTE1, ATTRIBUTE2) VALUES (Sno_seq.nextval, '".$_POST['Item']."', '".$_POST['ItemType']."', ".(int)$_POST['Quantity'].", ".(int)$_POST['TotalPrice'].", '".$deliveryDate ."', '".$_POST['Site']."', '".$_POST['DeliverToLoc']."', '".$_POST['Afe']."', 'Task', '".$_POST['Requestor']."', '".$_POST['CodeOne']."', '".$_POST['CodeTwo']."')";
$query = Application_Model_DbTable_Eftrqsthead::getDefaultAdapter();
// echo $dml;
$query = $query->query($dml);
if($query){
return true;
}else{
return false;
}
}catch(Exception $e){
echo $e->getMessage();
}
}
Above in my INSERT statement am inserting SNO_PK as auto incremented value, what I want to do here is I want to get the SNO_PK value from that INSERT statement and assign it to a PHP variable, then return it. I am new to Oracle, any help will be appreciated, Thanks.
I have seen RETURNING clause but didn't understood how to use it in my case.
Check get_class_methods($query) and you will find something like $query->lastInsertId();.
PS. Watch out how you put variables into your SQL. Short way to SQLInjection ;)
It's Simple, while you are executing query, suddenly you will get its last (incremented value). I have implemented your code.
$query = Application_Model_DbTable_Eftrqsthead::getDefaultAdapter();
// echo $dml;
$query = $query->query($dml);
if($query){
echo $query;
return true;
}else{
return false;
}
}catch(Exception $e){
echo $e->getMessage();
}
}
How can i get the value ,what i set by addStatusHistoryComment while creating order by php script.
$order = $observer->getEvent()->getOrder();
$dbOrderId = $order->getId();
$MagOrderId = $order->getRealOrderId();
Mage::log('dbOrderId : '. $dbOrderId);
Mage::log('MagOrderId : '. $MagOrderId);
I need to get like something $order->getStatusHistoryComment()
it is not working.
Need help.
The following data is not working as order is not commit yet.
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT comment FROM sales_flat_order_status_history WHERE parent_id=' $dbOrderId' limit 1 ";
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
foreach ($connection->fetchAll($sql) as $arr_row) {
$comments=$arr_row['comment'];
Use getStatusHistoryCollection instead of getStatusHistoryComment and it should work. The method is defined in Mage_Sales_Model_Order.
Or you can use getVisibleStatusHistory if you want only the comments visible on frontend.