MySQL error #1054 unknown column in field - php

This is my query:
SELECT COUNT( DISTINCT (paypal_transaction.buyerId) ) AS cid FROM eg_posts_details
INNER JOIN paypal_transaction ON paypal_transaction.id = eg_posts_details.OrderId
WHERE seller_id =190
It runs perfectly on MySQL directly but when I run it from my PHP codeigniter model I get the #1054 error. I have no idea why this is happening. Please help.
Here is the PHP code:
$query = $this->db->query("SELECT COUNT( DISTINCT (paypal_transaction.buyerId) ) AS cid
FROM eg_posts_details
INNER JOIN paypal_transaction ON paypal_transaction.id = eg_posts_details.OrderId
WHERE seller_id =190");

As per your image reference, paypal transaction table contain buyerId and you used it as buyer_id. So use the following.
Use like this
$sql = "select count(distinct(`paypal_transaction`.`buyerId`)) as `cid` from `eg_posts_details` inner join `paypal_transaction` on `paypal_transaction`.`id` = `eg_posts_details`.`OrderId` where `seller_id`= '190' ";
$query = $this->db->query($sql);
Hope its work for you

Related

Error getting data from Relational database with PHP

I have designed a simple relational database. When I am trying to get the data from the server it is throwing an error: (I have skipped some code to make it simple)
This is the SQL syntax I am using:
$sql = "SELECT lead.id, lead.name, lead.phone, lead.email, treatment.name, source.name, status.name FROM lead join treatment join source join status on treatment.id = lead.treatment_id and source.id = lead.source_id and status.id = lead.status_id";
This is used inside HTML:
echo "
<tr>
<td>".$row["lead.id"]."</td>
<td>".$row["lead.name"]."</td>
<td>".$row["lead.email"]."</td>
<td>".$row["treatment.name"]."</td>
<td>".$row["source.name"]."</td>
<td>".$row["status.name"]."</td>
</tr>";
This code is giving an error, when I change $row["lead.id"] to $row["id"] it works but I need to mention the table name as I have same column names in almost all the tables.
is there any way to do it using the table name?
You have the on condition in wrong place and with incorrect and condition
you should use on condition for each table
$sql = "SELECT
lead.id
, lead.name
, lead.phone
, lead.email
, treatment.name
, source.name
, status.name
FROM lead
join treatment on treatment.id = lead.treatment_id
join source on source.id = lead.source_id
join status on status.id = lead.status_id";
and for index try using alias
avoiding table name and dot notation
$sql = "SELECT
lead.id as lead_id
, lead.name as lead_name
, lead.phone as lead_phone
, lead.email as lead_email
, treatment.name as treatment_name_
, source.name as source_name
, status.name as status_name
FROM lead
join treatment on treatment.id = lead.treatment_id
join source on source.id = lead.source_id
join status on status.id = lead.status_id";

Convert query in codeigniter

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

Can't use a sub-query inside an UPDATE query?

When I use this query, MySQL display me the "HY000" error : "You can't specify target table 'msg_pv' for update in FROM clause".
The problem seems to be that the query and sub-query aim the same table... but this is what I need to do !
<?php $requete = $pdo->prepare('UPDATE msg_pv SET
lu=:lu
WHERE id_ref_msg = :id_ref_msg AND date_message > ( SELECT MIN(date_message) FROM msg_pv WHERE id_ref_msg = :id_ref_msg AND lu="0" )');
NOTE: I've read some solutions into other posts with INNER JOIN but it was for different tables.
Try like this
UPDATE
msg_pv AS t1
CROSS JOIN (
SELECT MIN(date_message) AS date_message FROM msg_pv
WHERE id_ref_msg = :id_ref_msg AND lu="0"
) AS t2
SET
t1.lu = :lu
WHERE
t1.date_message > t2.date_message

Help construct a simple query Using 3 tables

Hey guys need some more help
I have 3 tables USERS, PROFILEINTERESTS and INTERESTS
profile interests has the two foreign keys which link users and interests, they are just done by ID.
I have this so far
$statement = "SELECT
InterestID
FROM
`ProfileInterests`
WHERE
userID = '$profile'";
Now I want it so that it selects from Interests where what it gets from that query is the result.
So say that gives out 3 numbers
1
3
4
I want it to search the Interests table where ID is = to those...I just don't know how to physically write it in PHP...
Please help.
Using a JOIN:
Best option if you need values from the PROFILEINTERESTS table.
SELECT DISTINCT i.*
FROM INTERESTS i
JOIN PROFILEINTERESTS pi ON pi.interests_id = i.interests_id
WHERE pi.userid = $profileid
Using EXISTS:
SELECT i.*
FROM INTERESTS i
WHERE EXISTS (SELECT NULL
FROM PROFILEINTERESTS pi
WHERE pi.interests_id = i.interests_id
AND pi.userid = $profileid)
Using IN:
SELECT i.*
FROM INTERESTS i
WHERE i.interests_id IN (SELECT pi.interests_id
FROM PROFILEINTERESTS pi
WHERE pi.userid = $profileid)
You are on the right track, lets say you execute the query above using this PHP code:
$statement = mysql_query("SELECT InterestID FROM `ProfileInterests`
WHERE userID = '$profile'");
Then you can use a PHP loop to dynamically generate an SQL statement that will pull the desired IDs from a second table. So, for example, continuing the code above:
$SQL = "";
while ($statementLoop = mysql_fetch_assoc($statement)) {
//Note the extra space on the end of the query
$SQL .= "`id` = '{$statementLoop['InterestID']}' OR ";
}
//Trim the " OR " off the end of the query
$SQL = rtrim($SQL, " OR ");
//Now run the dynamic SQL, using the query generated above
$query = mysql_query("SELECT * FROM `table2` WHERE {$SQL}")
I haven't tested the code, but it should work. So, this code will generate SQL like this:
SELECT * FROM `table2` WHERE `id` = '1' OR `id` = '3' OR `id` = '4'
Hope that helps,
spryno724
Most likely you want to join the tables
select
i.Name
from
ProfileInterests p
inner join
interests i
on
p.interestid = i.interestid
where
p.userid = 1

PHP And MYSQ help

ok here is my php and mysql code:
where it is bold i wanted to the the uid from the online table and if it in there
where online.uid = '' i needed so it put the uid in there.
$sql = ("select accounts.id,
accounts.tgid,
accounts.lastactivity,
cometchat_status.message,
cometchat_status.status,
**online.uid**
from friends_list join accounts
on friends_list.fid = accounts.id
left join cometchat_status
on accounts.id = cometchat_status.userid
where friends_list.status = '1'
and **online.uid = ''**
and friends_list.uid = '".mysql_real_escape_string($userid)."'
order by tgid asc");
#sledge identifies the problem in his comment above (I'm not sure why he didn't post an answer).
You are selecting a column from the online table, but you don't include it in your FROM clause. You have to query from a table in order to reference its columns in other parts of the query. For example:
$sql = ("select accounts.id,
accounts.tgid,
accounts.lastactivity,
cometchat_status.message,
cometchat_status.status,
online.uid
from friends_list
join accounts on friends_list.fid = accounts.id
join online on ( ??? )
left join cometchat_status
on accounts.id = cometchat_status.userid
where friends_list.status = '1'
and online.uid = ''
and friends_list.uid = '".mysql_real_escape_string($userid)."'
order by tgid asc");
You need to fill in the join condition, because there's not enough information in your original post to infer how the online table is related to other tables.
PS: Kudos for using mysql_real_escape_string().

Categories