what is the error in this please help!
here is the query from mysql :
public function stgradu($condition=">=50"){
$stmt=$this->db->prepare("SELECT
courses_has_schedule.mark as mark,
Sum(courses.crs_hours) as total_hrs
FROM
courses_has_schedule
Inner Join courses ON courses_has_schedule.crs_id = courses.crs_id
Inner Join schedule ON schedule.sch_id = courses_has_schedule.sch_id
Inner Join student ON student.st_id = schedule.st_id
where
student.st_id=? AND courses_has_schedule.mark $condition");
$stmt->execute(array($this->getCurrentStudent(true)));
return $stmt->fetchAll();
}
when i run this if statement :
$stgr = $dataService->stgradu(">=50");
if(($stgr->total_hrs==7) AND ($stgradu->mark >= 50)) {
echo Yes*";
}else
echo "No";
when i run the app its gives me this error
(( Notice: Trying to get property of non-object in C:\xampp\htdocs\samer\stgr.php on line >12))
please help all!
fetchAll returns an array and you are trying to acces it like an object. Try this:
if(($stgr['total_hrs']==7) AND ($stgradu['mark'] >= 50)) {
You need to pass PDO::FETCH_OBJ to fetchAll() because it returns an associative array by default.
public function stgradu($condition=">=50"){
$stmt=$this->db->prepare("SELECT
courses_has_schedule.mark as mark,
Sum(courses.crs_hours) as total_hrs
FROM
courses_has_schedule
Inner Join courses ON courses_has_schedule.crs_id = courses.crs_id
Inner Join schedule ON schedule.sch_id = courses_has_schedule.sch_id
Inner Join student ON student.st_id = schedule.st_id
where
student.st_id=? AND courses_has_schedule.mark $condition");
$stmt->execute(array($this->getCurrentStudent(true)));
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
If you are supposed to get only one row then change it to fetch(PDO::FETCH_OBJ) and your code should run fine.
thanx all.. i knew it by my self :P the syntax should be...
return $stmt->fetchAll()[0];
Related
This question already has answers here:
Nested joins in Codeigniter
(2 answers)
Closed 1 year ago.
I am new to Codeigniter, and I want to convert the SQL query into the Codeigniter-style query
My query looks like this :
SELECT tbl_detail_order.ID, tbl_detail_order.order_id, tbl_detail_order.produk, products.name, tbl_detail_order.qty, tbl_detail_order.harga, tbl_order.tanggal, tbl_pelanggan.nama, tbl_pelanggan.alamat, tbl_pelanggan.telp
FROM tbl_pelanggan
INNER JOIN (tbl_order INNER JOIN
(products INNER JOIN tbl_detail_order
ON products.kd_barang = tbl_detail_order.produk)
ON tbl_order.ID = tbl_detail_order.order_id)
ON tbl_pelanggan.ID = tbl_order.pelanggan;
And I try to make it in Codeigniter like this
public function getAllJoin()
{
$this->db->select('tbl_detail_order.ID, tbl_detail_order.order_id, tbl_detail_order.produk, products.name, tbl_detail_order.qty, tbl_detail_order.harga, tbl_order.tanggal, tbl_pelanggan.nama, tbl_pelanggan.alamat, tbl_pelanggan.telp');
$this->db->from('tbl_detail_order');
$this->db->join('tbl_order','tbl_detail_order.order_id = tbl_order.id','INNER');
$this->db->join('tbl_pelanggan','tbl_order.pelanggan = tbl_pelanggan.id','INNER');
$this->db->join('products','products.kd_barang = tbl_pelanggan.id','INNER');
$query = $this->db->get();
return $query->result_array();
}
Can you correct my code? Is it wrong or right?
As this other post mentions, there isn't an API for nested joins in the CodeIgniter DB syntax. So you just use the outer one, and move the rest inline with that.
public function getAllJoin()
{
$this->db->select('tbl_detail_order.ID, tbl_detail_order.order_id, tbl_detail_order.produk, products.name, tbl_detail_order.qty, tbl_detail_order.harga, tbl_order.tanggal, tbl_pelanggan.nama, tbl_pelanggan.alamat, tbl_pelanggan.telp');
$this->db->from('tbl_detail_order');
$this->db->join('tbl_order INNER JOIN (products INNER JOIN tbl_detail_order ON products.kd_barang = tbl_detail_order.produk) ON tbl_order.ID = tbl_detail_order.order_id','tbl_detail_order.order_id = tbl_order.id','INNER');
$query = $this->db->get();
return $query->result_array();
}
Im trying to insert an SQL statment into a variable. The statement contains keywords entered by the user in a search bar. However, for some reason I can keep getting the error "Trying to get the property of non-object". Below is my code:
public function searchTable() {
$sql = "SELECT grades_eng.Grade, domain_math_eng.Domain, cluster_eng.Cluster, math_standards_eng.Standard FROM ".$this->standardsTable."
WHERE Standard LIKE '%".$this->keyword." %'
INNER JOIN grades_eng ON math_standards_eng.Grade_Id = grades_eng.Id
INNER JOIN domain_math_eng ON math_standards_eng.Domain_Math_Eng_Id = domain_math_eng.Id
INNER JOIN cluster_eng ON math_standards_eng.Cluster_Eng_Id = cluster_eng.Id";
$results = $this->conn->query($sql);
//returns array
return $results;
}
The code for the object being used:
$search = new SearchResult($conn, $subject, $keyword);
$queryResults = $search->searchTable();
$search->displayResults($queryResults);
Im confident is my sql query that's causing the error because when I use the following code, it displays results :
$sql = "SELECT * FROM ".$this->standardsTable." WHERE Standard LIKE '%".$this->keyword."%' ";
$results = $this->conn->query($sql);
Im Trying to display the same results but replace the IDs with actual text. The query does work when I run it in MySql.
P.S Still working on learning to use Aliases so I apologize in advance.
I just learned that the "Where" keyword was suppose to go towards the end. Lesson learned!
$sql = "SELECT grades_eng.Grade, domain_math_eng.Domain, cluster_eng.Cluster, math_standards_eng.Standard FROM ".$this->standardsTable."
INNER JOIN grades_eng ON math_standards_eng.Grade_Id = grades_eng.Id
INNER JOIN domain_math_eng ON math_standards_eng.Domain_Math_Eng_Id = domain_math_eng.Id
INNER JOIN cluster_eng ON math_standards_eng.Cluster_Eng_Id = cluster_eng.Id
WHERE Standard LIKE '%".$this->keyword."%' ";
I have a method below, that selects some comments. I'm having issues getting the data, not because the query doesn't work but the while loop doesn't fetch the data if I don't use an array, here is my code
public function selectAllComment($idarticle)
{
$db = $this->getBdd();
$sql = $db->query('SELECT c.comment, c.date_comment
FROM comments as c
INNER JOIN joint_a_comments on joint_a_comments.id_comment = c.id
INNER JOIN articles a ON joint_a_comments.id_article = a.id
WHERE a.id ="'.$idarticle.'"
ORDER BY c.date_comment DESC
LIMIT 0,10');
while($result[] = $sql->fetch(PDO::FETCH_ASSOC))
{
$rescomment[] = new Comments($result);
}
return $rescomment;
With this above, I can see that the data are fetched using var_dump on $result.
However, first problem is:
1- if I do the same with the object i.e. $rescomment (I instantiate a class to hydrate the data and I use them in a view...).
2- If I remove the brackets [] from $result, I get "boolean false"....any idea guys?
Im working on ecommerce platform. I have a query in normal form. i want to convert to codeigniter.
this is my query
SELECT products.product_name,products.product_id,products.short_description,pi.img,
CASE WHEN products.sp_price=0 THEN products.price WHEN products.sp_price!=0 THEN products.sp_price END as pprice FROM
(`offers_products`) JOIN `products` ON `offers_products`.`product_id` = `products`.`product_id`
LEFT JOIN (SELECT image_name as img,product_id as pid from product_images GROUP BY pid)pi
ON `products`.`product_id` = `pi`.`pid` .
How do i convert this to codeigniter query.
I tried, but getting syntax error. Please help me, im new to codeigniter.
There is no need to convert your query. And also the is no rule that you should use codeigniter query only.
you can use
$res = $this->db->query("your query here")->result();
$res will have that result() you want.
This will help you.
For more reference, check here
Just use
public function method_name()
{
$query = $this->db->query("SELECT products.product_name,products.product_id,products.short_description,pi.img, CASE WHEN products.sp_price=0 THEN products.price WHEN products.sp_price!=0 THEN products.sp_price END as pprice FROM (`offers_products`) JOIN `products` ON `offers_products`.`product_id` = `products`.`product_id` LEFT JOIN (SELECT image_name as img,product_id as pid from product_images GROUP BY pid)pi ON `products`.`product_id` = `pi`.`pid`");
$result = $query->result_array();
return $result;
}
We use result_array for pass data as Objective array
i have this function in my script.php
$email=$_SESSION['cspa']['email'];
function getit()
{
$get_pendingsupport=mysql_query("select
cus.email,
datetime,
s.service_type,
subject,
support_id,
status,
u.urgency
from tbl_client_support c
inner join tbl_client_service s on c.service_id=s.service_id
inner join customer_reg cus on c.customer_reg_id=cus.id
inner join tbl_client_urgency u on c.urgency_id=u.urgency_id
where status='open'
and cus.email='$email';
") or die(mysql_error());
return mysql_num_rows($get_pendingsupport) ;
}
that my function and am calling it like this in home.php
but it returning 0 as the value
but when i bring it to home.php it's okay
please what could be wrong
It returns zero because zero rows are being returned. Why? Because you never pass $email to your function so it is not available to your query. Since it is used in your WHERE clause your query doesn't return the rows you think it does as no rows have a status of "Open" and an empty customer email field.
You can solve this by passing that variable to your function:
function getit($email)
{
and when you call it:
$num_rows = getit($email);
You are not passing the $email to the query. Try this:
function getit($email)
{
$get_pendingsupport=mysql_query("select
cus.email,
datetime,
s.service_type,
subject,
support_id,
status,
u.urgency
from tbl_client_support c
inner join tbl_client_service s on c.service_id=s.service_id
inner join customer_reg cus on c.customer_reg_id=cus.id
inner join tbl_client_urgency u on c.urgency_id=u.urgency_id
where status='open'
and cus.email='$email';
") or die(mysql_error());
return mysql_num_rows($get_pendingsupport) ;
}