Codeigniter calling table database which I don't call - php

In my controller :
I have function like this :
$this->admindata->examview($a,3);
In model, I just have function like this :
function examview($examid, $examtipe){
$this->db->select("exam_id");
$this->db->from("mainexam");
$query = $this->db->get()
return $query->result();
}
And i got error :
Column 'id_group' in field list is ambiguous
SELECT `mu`.`obli`, `mu`.`id_exam_question`, `p`.`id_question`, `question`, `type_question`, `m`.`id_gabungan`, `p_parent`, `id_group` FROM (`exam`, `exam` mu) LEFT JOIN `randomexam` c ON `mu`.`id_group`= `c`.`id_question_order` LEFT JOIN `question` p ON `p`.`id_question` = `c`.`id_question` LEFT JOIN `main` m ON `m`.`id_question` = `p`.`id_question` WHERE `mu`.`id_exam` = '10' GROUP BY `mu`.`id_exam_question` ORDER BY `question_type` asc, LIMIT 0
I don't even have JOIN in my function. And If I delete $this->admindata->examview($a,3), My error has gone. Codeigniter try to call other function I think.

Nah it's tripping up on something else, a query before this one is in need of fixing. Your query here would be like
SELECT exam_id FROM mainexam;
PHP & MYSQL: How to resolve ambiguous column names in JOIN operation?
Try to track down the query causing the error and apply bandages.

Related

mysql clause where not null in php

I am making a query which is not working. I want to get data where column of pic in is not empty in all_destination table, also I am using join in my query:
SELECT *
FROM `travels_detail`
INNER JOIN all_destination ON travels_detail.destination = all_destination.destination
WHERE all_destination.pic IS NOT NULL
whats wrong in my query?
You should check what is the error returned by MySQL. It will tell you what is the error.
You just want to remove the IS NOT NULL check and just have
SELECT *
FROM `travels_detail`
INNER JOIN all_destination ON travels_detail.destination = all_destination.destination
WHERE all_destination.pic;
This will only return records where all_destination.pic has been set.
try this:
SELECT td.some_attribute
FROM travels_detail as td
INNER JOIN all_destination as ad ON td.destination = ad.destination
WHERE ad.pic IS NOT NULL
You can try this.. It mostly works
SELECT * FROM `travels_detail` INNER JOIN all_destination ON travels_detail.destination = all_destination.destination WHERE all_destination.pic ! = ""

Left Join between two databases using PDO

I have two databases and I am trying to compare two tables. My code does not seem to be working, not sure what I am doing wrong.
Here is the code.
<?php
include 'connection.php';
/*
* This code compares between two tables
*/
//SQL call
$getData = $connection->prepare("SELECT `CustomerCity` FROM `auth` LEFT JOIN `tb_data.cobs.city` WHERE `CustomerCity` = `tb_data.cobs.city` LIMIT 3");
$getData->execute();
$gotData = $getData->fetchAll(PDO::FETCH_ASSOC);
print_r($gotData);
In my database I have two tables, on is cobs, the other is tb_data. tb_data has a table called cobs and auth is a table within a database called d_data. Both of these tables have a city column. I need get every record in auth that has a city that matches in the cobs table.
That looks like the query is using a mixture of explicit join syntax with obsolescent syntax for the join using the WHERE clause for the join conditions.
If so try:-
SELECT CustomerCity
FROM auth
LEFT JOIN tb_data.cobs
ON auth.CustomerCity = cobs.city
LIMIT 3
Others have pointed out that your query is wrong, but have not provided a correct answer. This is what you are likely looking for:
SELECT `auth.CustomerCity` FROM `auth`
LEFT JOIN `tb_data.cobs` ON `tb_data.cobs.city` = `auth.CustomerCity`
LIMIT 3
Try this :
Select *.auth, *.cobs from auth join cobs on auth.city = cobs.city limit 3
The query is incorrect, you need to specify the link betweeen the tables auth and tb_data.cobs.city. For example:
SELECT
*
FROM
FOOTABLE FOO
LEFT JOIN
BARTABLE BAR ON BAR.FOO_ID FOO.ID = -- here goes the link between them
WHERE
...

DQL Select every rows having one column's MAX value

Working with Symfony 2 and Doctrine, I'm searching for a way to select every rows having the max value in a specific column.
Right now, I'm doing it in two queries:
One to get the max value of the column in the table
Then I select rows having this value.
I'm sure this can be done with one query.
Searching, I have found this answer in a thread, that seems to be what I am searching for, but in SQL.
So according to the answer's first solution, the query I'm trying to build would be something like that:
select yt.id, yt.rev, yt.contents
from YourTable yt
inner join(
select id, max(rev) rev
from YourTable
group by id
) ss on yt.id = ss.id and yt.rev = ss.rev
Does anybody know how to make it in Doctrine DQL?
For now, here is the code for my tests (not working):
$qb2= $this->createQueryBuilder('ms')
->select('ms, MAX(m.periodeComptable) maxPeriode')
->where('ms.affaire = :affaire')
->setParameter('affaire', $affaire);
$qb = $this->createQueryBuilder('m')
->select('m')
//->where('m.periodeComptable = maxPeriode')
// This is what I thought was the most logical way of doing it:
->innerJoin('GAAffairesBundle:MontantMarche mm, MAX(mm.periodeComptable) maxPeriode', 'mm', 'WITH', 'm.periodeComptable = mm.maxPeriode')
// This is a version trying with another query ($qb2) as subquery, which would be the better way of doing it for me,
// as I am already using this subquery elsewhere
//->innerJoin($qb2->getDQL(), 'sub', 'WITH', 'm.periodeComptable = sub.maxPeriode')
// Another weird try mixing DQL and SQL logic :/
//->innerJoin('SELECT MontantMarche mm, MAX(mm.periodeComptable) maxPeriode ON m.periodeComptable = mm.maxPeriode', 'sub')
//->groupBy('m')
->andWhere('m.affaire = :affaire')
->setParameter('affaire', $affaire);
return $qb->getQuery()->getResult();
The Entity is GAAffairesBundle:MontantMarche, so this code is in a method of the corresponding repository.
More generally, I'm learning about how to handle sub-queries (SQL & DQL) and DQL syntax for advanced queries.
Thx!
After some hours of headache and googling and stackOverflow readings...
I finally found out how to make it.
Here is my final DQL queryBuilder code:
$qb = $this->createQueryBuilder('a');
$qb2= $this->createQueryBuilder('mss')
->select('MAX(mss.periodeComptable) maxPeriode')
->where('mss.affaire = a')
;
$qb ->innerJoin('GAAffairesBundle:MontantMarche', 'm', 'WITH', $qb->expr()->eq( 'm.periodeComptable', '('.$qb2->getDQL().')' ))
->where('a = :affaire')
->setParameter('affaire', $affaire)
;
return $qb->getQuery()->getResult();
For me when i trying to make a subquery i make:
->andWhere($qb->expr()->eq('affaire', $qb2->getDql()));
To achieve this using pure DQL and without use of any aggregate function you can write doctrine query as
SELECT a
FROM GAAffairesBundle:MontantMarche a
LEFT JOIN GAAffairesBundle:MontantMarche b
WITH a.affaire = b.affaire
AND a.periodeComptable < b.periodeComptable
WHERE b.affaire IS NULL
ORDER BY a.periodeComptable DESC
The above will return you max record per group (per affaire)
Expalnation
The equivalent SQL for above DQL will be like
SELECT a.*
FROM MontantMarche a
LEFT JOIN MontantMarche b
ON a.affaire = b.affaire
AND a.periodeComptable < b.periodeComptable
WHERE b.affaire IS NULL
ORDER BY a.periodeComptable DESC
Here i assume there can be multiple entries in table e.g(MontantMarche) for each affaire, so here i am trying to do a self join on affaire and another tweak in join is i am trying to join only rows from right table(b) where a's periodeComptable < b's periodeComptable, So the row for left table (a) with highest periodeComptable will have a null row from right table(b) thus to pick the highest row per affaire the WHERE right table row IS NULL necessary.
Similarly using your posted sample query with inner join can be written as
select yt.id, yt.rev, yt.contents
from YourTable yt
left join YourTable ss on yt.id = ss.id and yt.rev < ss.rev
where ss.rev is null
Hope it makes sense

Joining multiple tables in mySQL

Okay, I'm not that good at mySQL.
What I'm trying to do here is join 2 tables:
1. users
2. comments
I'm trying to make a comment system where it should pull the username and profile picture from users table and the comments and date_posted from the comments table.
Here is my query:
$mem_query = mysql_query("SELECT `comments`.`comment_id` AS `comments_id`, `users`.`user_id` AS `users_id`, `users`.`username`,`users`.`profile_pic`,`comments`.`txt_content`,`comments`.`date_posted` FROM `comments` INNER JOIN `users` ON `users`.`user_id` = `comments`.`user_id` WHERE `comments`.`post_id` = '$post_id'");
And I want to run the query using the while loop:
while($run_mem = mysql_fetch_array($mem_query)){
$comment_id = $run_mem['comments_id'];
$txt_content = $run_mem['comments.txt_content'];
$profile_pic = $run_mem['users.profile_pic'];
?>
//Run all the comments depending upon the post_id.
<?php
}
?>
As of now, it is giving me this error: - THIS IS NOT SHOWING AFTER my 2nd update.
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\simpleblog\view.php on line 73
How do I fix it? Thanks.
P.S: I know 'mysql_query' is being deprecated in PHP. I'll change that later.
P.S 2: I fixed the query from table.column to table.column, however, its not showing any errors but its not pulling any information from the database.
Look at the ` symbols, they should look like:
`table`.`column`
and not:
`table.column`
there is a big syntax error in your query:
SELECT `comments.comment_id` AS `comments_id`, `users.user_id` AS `users_id`, `users.username`,`users.profile_pic`,`comments.txt_content`,`comments.date_posted` FROM `comments` INNER JOIN `users` ON `users.user_id` = `comments.user_id` WHERE `comments.post_id` = '$post_id'
should be
SELECT `comments`.`comment_id` AS `comments_id`, `users`.`user_id` AS `users_id`, `users`.`username`,`users`.`profile_pic`,`comments`.`txt_content`,`comments`.`date_posted` FROM `comments` INNER JOIN `users` ON `users`.`user_id` = `comments`.`user_id` WHERE `comments`.`post_id` = '$post_id'
you wrote this: `comments.user_id` but it has to be this: `comments`.`user_id` and that at almost every position where you did that wrong
http://www.php.net/manual/en/function.mysql-query.php
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
Looks like the latter happened. An error occurred and the mysql_query call returned false.

Returning data from multiple tables in one query even if secondary tables do not apply

I have an issue getting data from three tables, which I want to return using one query.
I've done this before using a query something like this one:
$query = mysql_query("SELECT
maintable.`id`,
maintable.`somedata`,
maintable.`subtable1_id`,
subtable1.`somedata`,
subtable1.`subtable2_id`,
subtable2.`somedata`
FROM
`maintable` maintable,
`subtable1` subtable1,
`subtable2` subtable2
WHERE
maintable.`somedata` = 'some_search' AND
subtable1.`id` = maintable.`subtable1_id` AND
subtable2.`id` = subtable1.`subtable2_id`
")or die(mysql_error());
The problem this time is that the extra details might not actually apply. I need to return all results that match some_search in maintable, even if there is no subtable1_id specified.
I need something that will go along the lines of
WHERE
maintable.`somedata` = 'some_search'
AND IF maintable.`subtable1_id` IS NOT NULL (
WHERE subtable1.`id` = maintable.`subtable1_id` AND
subtable2.`id` = subtable1.`subtable2_id`
)
As you will probably guess, I am not an advanced mysql user! Try as I might, I cannot get the syntax right, and I have had no luck searching for solutions on the web.
Any help much appreciated!
It seems like the basic distinction you're looking for is between an INNER JOIN and a LEFT JOIN in MySQL.
An INNER JOIN will require a reference between the two tables. There must be a match on both sides for the row to be returned.
A LEFT JOIN will return matches in both rows, like an INNER, but it will also returns rows from your primary table even if no rows match in your secondary tables -- their fields will be NULL.
You can find example syntax in the docs.
If I got this right, you need to use MySQL LEFT JOIN. Try this:
SELECT
m.id,
m.somedata,
m.subtable1_id,
s1.somedata,
s1.subtable2_id,
s2.somedata
FROM
maintable m
LEFT JOIN
subtable1 s1
ON
m.subtable1_id = s1.id
LEFT JOIN
subtable2 s2
ON
s1.subtable2_id = s2.id
WHERE
m.somedata = 'some search'
This will return the data of maintable even if there's no equivalent data in subtable1 or 2 OR data of maintable and subtable1 if there's no equivalent data in subtable2.
How about this:
WHERE
maintable.`somedata` = 'some_search'
AND (maintable.`subtable1_id` IS NULL OR (
subtable1.`id` = maintable.`subtable1_id` AND
subtable2.`id` = subtable1.`subtable2_id` )
)
Keep in mind that this will result in a cross product of subtable1 and subtable2 with maintable when subtable1_id is NULL.

Categories