Mysqli query inner join error - mysql

Whats wrong with this query
$yourfriendsbud = mysqli_query($con, "SELECT avatar,firstname,lastname,id
FROM people
INNER JOIN friends
ON
people.id=friends.zatrazio_id
WHERE status=0 AND dobio_id='$user_id' LIMIT 50");
while($row = mysqli_fetch_array($yourfriendsbud))
It reports the error:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/u573639388/public_html/menu.php on line 64
line 64 is
while($row = mysqli_fetch_array($yourfriendsbud))

That means you have an id column in both tables. You have to be more specific in the SELECT, using SELECT people.id, avatar, firstname...

Related

A Database Error Occurred (SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay)

This database has worked fine for two years, now it's getting the following error? Can someone explain this issue and suggest a way to resolve this?
Error Number: 1104
The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
Code:
select
salary_tb.*, order_tb.order_no,
daily_target_tb.customer as customer_name,
jobs_tb.job as job_name
from
salary_tb
left join
order_tb on salary_tb.order_id = order_tb.id
left join
daily_target_tb on order_tb.id = daily_target_tb.order_id
left join
jobs_tb on salary_tb.job_id = jobs_tb.id
where
salary_tb.id > 0
and salary_tb.isDeleted = 0
and salary_tb.employee_id = '1'
group by
salary_tb.id
Filename: models/SalaryModel.php
Line Number: 22
The issue is your query is either returning huge number of rows which is more than value set for MAX_JOIN_SIZE configuration or your SQL_BIG_SELECTS is set to 0 which will not allow you to run queries which take too long to return a result. You can do one/all of the following 3 things :
1.SET SQL_BIG_SELECTS=1;
2.MAX_JOIN_SIZE= (More than the no of rows returned by the join statement)
Refine your query so that it returns appropriate values within the set parameters and in less time.(Using Indexing, Returning less no. of rows).
Current Query
function get_rows($where)
{
$sql =
"select salary_tb.*, order_tb.order_no, daily_target_tb.customer as customer_name, jobs_tb.job as job_name
from salary_tb
left join order_tb on salary_tb.order_id = order_tb.id
left join daily_target_tb on order_tb.id = daily_target_tb.order_id
left join jobs_tb on salary_tb.job_id = jobs_tb.id
where salary_tb.id > 0";
if($where != ""){
$sql .= $where;
}
$query =# $this->db->query('SET SQL_BIG_SELECTS=1');
$this->db->query($sql);
return $query->result_array();
}

Updating the last row MYSQL

I want to update the last row in a mysql table
UPDATE logend
SET endsecs = endsecs+'$moretime'
WHERE id = (SELECT id FROM logend ORDER BY id DESC LIMIT 1)
But it doesn't work, because of this error:
ERROR 1093 (HY000): You can't specify target table 'logend' for update in FROM clause
In MySql you can't use the updated table in a subquery the way you do it.
You would get the error:
You can't specify target table 'logend' for update in FROM clause
What you can do is an UPDATE with ORDER BY and LIMIT:
$sql = "UPDATE logend SET endsecs=endsecs+'$moretime' ORDER BY id DESC LIMIT 1";
Can't you just get the max(id) and update that?
$sql = "UPDATE logend SET endsecs=endsecs+'$moretime' WHERE id = (
SELECT id FROM (
SELECT MAX(id) FROM logend) AS id
)";
Here's another solution: a self-join, to find the row for which no other row has a greater id.
Also, you should really not interpolate POST inputs directly into your SQL statement, because that exposes you to SQL injection problems. Use a query parameter instead.
$moretime = $_POST['moretime'];
$sql = "
UPDATE logend AS l1
LEFT OUTER JOIN logend AS l2 ON l1.id < l2.id
SET l1.endsecs = l1.endsecs + ?
WHERE l2.id IS NULL";
$stmt = $mysqli->prepare($sql);
if (!$stmt) {
trigger_error($mysqli->error);
die($mysqli->error);
}
$stmt->bind_param("s", $moretime);
$ok = $stmt->execute();
if (!$ok) {
trigger_error($stmt->error);
die($stmt->error);
}

How to access subquery table in the main query with MySQL?

select m1.id, m1.status, at.view_data, at.view_graph, ta.tag_string
from
access_tbl at, image_campaign_tbl m1
RIGHT JOIN
(select
GROUP_CONCAT(t.name) as tag_string , c.image_campaign_id
from campaign_tags_tbl c,tag_tbl t
where c.tag_id=t.id
$tag_q
group by c.image_campaign_id
) as ta
ON ta.image_campaign_id=m1.id
where
m1.client_id =$client_id
and m1.client_id = at.client_id
$prev_filter
limit $start,$end;
Error message:
in LOGS: DBD::mysql::db selectall_arrayref failed: Unknown column 't.name' in 'where clause' at /home/sakthi/rtads/Project/pm/Image/UI.pm line 2536.**
In Perl Module, I'm passing the same value of $tag_q to the $prev_filter to get the Pagination of filter based on TAGS values in the next page
if ( $prev_filter eq '' ) {
$prev_filter =
$search_clist_q . ' '
. $tag_q . ' '
}
From the error msg, I got the error which I'm doing. Since I'm trying to access the table of subquery in the main query, this error is happening.
So I want to know how to access the tag_string(or)t.name outside the subquery.
First of all, i suggest you to avoid use of old school syntax for jointures (FROM table1, table2,... WHERE table1.column1 = table2.column2 AND ...).
Here is the query that seems to return what you're looking for:
SELECT IC.id
,IC.status
,A.view_data
,A.view_graph
,TA.tag_string
FROM access_tbl A
INNER JOIN image_campaign_tbl IC ON IC.client_id = A.client_id
AND IC.client_id = $client_id
RIGHT JOIN (SELECT CT.image_campaign_id
,GROUP_CONCAT(T.name) AS [tag_string]
FROM campaign_tags_tbl CT
INNER JOIN tag_tbl T ON T.id = CT.tag_id
GROUP BY CT.image_campaign_id) TA ON TA.image_campaign_id = IC.id
WHERE <Your filters here>
LIMIT $start, $end
Hope this will help you.

Getting the closest time in a PDO statement

I am working from 2 databases and I need to find records which matches the closest times. Both fields are datetime().
So in essence:
table1.time = 2012-06-07 15:30:00
table2.time = 2012-06-07 15:30:01
table2.time = 2012-06-07 15:30:02
table2.time = 2012-06-07 15:30:03
NOTE: The table I am querying (table2) is a mssql table, and table1.time is a datetime() time. I need to find in table2 the row which closest matches table1.time, but I have no guarnatee that it would be an exact match, so I need the closest. I only need to return 1 result.
I tried the SQL below based on an example from a previous stackoverflow query but it failed to work.
Table1 is a mysql database where table2 is mssql and the query happens on table2 (mssql)
try {
$sql = "
SELECT
PCO_AGENT.NAME,
PCO_INBOUNDLOG.LOGIN AS LOGINID,
PCO_INBOUNDLOG.PHONE AS CALLERID,
PCO_INBOUNDLOG.STATION AS EXTEN,
PCO_INBOUNDLOG.TALKTIME AS CALLLENGTH,
PCO_INBOUNDLOG.CHANNELRECORDID AS RECORDINGID,
PCO_SOFTPHONECALLLOG.RDATE,
PCO_INBOUNDLOG.RDATE AS INBOUNDDATE
FROM
PCO_INBOUNDLOG
INNER JOIN
PCO_LOGINAGENT ON PCO_INBOUNDLOG.LOGIN = PCO_LOGINAGENT.LOGIN
INNER JOIN
PCO_SOFTPHONECALLLOG ON PCO_INBOUNDLOG.ID = PCO_SOFTPHONECALLLOG.CONTACTID
INNER JOIN
PCO_AGENT ON PCO_LOGINAGENT.AGENTID = PCO_AGENT.ID
WHERE
PCO_INBOUNDLOG.STATION = :extension
AND ABS(DATEDIFF(:start,PCO_SOFTPHONECALLLOG.RDATE))
";
$arr = array(":extension" => $array['extension'], ":start" => $array['start']);
$query = $this->mssql->prepare($sql);
$query->execute($arr);
$row = $query->fetchAll(PDO::FETCH_ASSOC);
$this->pre($row);
}
I am getting the following error at the moment:
SQLSTATE[HY000]: General error: 174 General SQL Server error: Check messages from the SQL Server [174] (severity 15) [(null)]SQLSTATE[HY000]: General error: 174 General SQL Server error: Check messages from the SQL Server [174] (severity 15) [(null)]
Found a shorter version:
SELECT * FROM `table` WHERE `date` < '$var' ORDER BY date LIMIT 1;

configuring how to pull data out of 3 tables with mysql

Here is how the the database is layout. I can connect to DB fine.
I had it pullling from database two things but adding a third i can not get it to pull. I just need some help if i could ..
database - mmoore_drupal
table 1
table name = content_type_uprofile
data
vid= 19723
nid =19674
field_name_value = matthew moore
table 2
table name = location_instance
data
lid = 1521
vid = 19723
nid = 19674
table 3
table name = location
data
lid = 1521
street =
city =
country =
latitude =
longitude =
I am trying to pull name and then other info from the other two tables. But mainly i need to have name and other information from location. I thought i had to have the other table to associate the connection. Any help is appreciated.
$query = "SELECT content_type_uprofile.field_name_value,location.street,location.city
FROM location_instance,location,content_type_uprofile
WHERE location_instance.lid = location.lid and location_instance.nid=content_type_uprofile.nid"
$result = mysql_query($query) or die(mysql_error());
// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
echo $row['nid']."-".$row['street']. " - ". $row['city'];
echo "<br />";
}
?>
Use this SQL (explicit join syntax):
SELECT
content_type_uprofile.nid,
location.street,
location.city
FROM
content_type_uprofile
INNER JOIN location_instance
ON (content_type_uprofile.nid = location_instance.nid)
INNER JOIN location
ON (location_instance.lid = location.lid)
The SQL that you posted is using implicit join SQL syntax.
I think for some reason, I think the line in your SQL:
WHERE location_instance.lid = location.lid and location_instance.nid=content_type_uprofile.nid
is filtering out all the rows from your result set. I'm not sure because I avoid the implicit syntax.
You were also missing the nid field which your PHP code is looking for in the result set.
As long as your data is correct (i.e. the fields that you are joining on have the right values), the SQL that I posted will work for you.
You ever done something with join's?
select *.locaition,
*.content_type_uprofile
from location_instance li
inner join location l on l.lid = li.lid
inner join content_type_uprofile ctu on ctu.vid = li.vid