Here is what I am working with:
$query = "SELECT Products.Title, Product_Lines.pl_Title, Manufacturers.man_Title".
"FROM Products, Product_Lines, Manufacturers ".
"WHERE Products.pl_ID = Product_Lines.pl_ID AND Product_Lines.man_ID = Manufacturers.man_ID";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['Title']. " - ". $row['pl_Title']. " - ". $row['man_Title'];
echo "<br />";
}
I am getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'WHERE Products.pl_ID = Product_Lines.pl_ID AND
Product_Lines.man_ID = Manufactur' at line 1
I am unfamiliar with this method and this error
SELECT Products.Title, Product_Lines.pl_Title, Manufacturers.man_Title
FROM Products INNER JOIN Product_Lines ON Products.pl_ID = Product_Lines.pl_ID INNER JOIN Manufacturers ON Product_Lines.man_ID = Manufacturers.man_ID
WILL DO
I don't see white space before your FROM clause. This is a possible cause for the error. Try:
$query = "SELECT Products.Title, Product_Lines.pl_Title, Manufacturers.man_Title".
" FROM Products, Product_Lines, Manufacturers ".
"WHERE Products.pl_ID = Product_Lines.pl_ID AND Product_Lines.man_ID = Manufacturers.man_ID";
Need To follow two things
Foriegn Key
Join
Search Join i.e [left join right join...] In Inter u ll get anwser for ur question/..
Related
I have two tables 'lr_users' and 'lr_ranks' with the following table structures:
lr_users:
user_id(pk), username, rank(fk), job_id(fk), date_joined
lr_ranks: rank_id(pk), name
$qry= mysqli_query($con, "SELECT lr_users.username, lr_users.rank, lr_users.job_id, lr_users.date_joined lr_ranks.name AS rankname FROM lr_users
LEFT JOIN lr_ranks ON lr_users.rank = lr_ranks.rank_id")or die(mysqli_error($con));
$rows = mysqli_num_rows($qry);
Above is my PHP code for generating a 'LEFT JOIN' query, however when I run it i get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.name AS rankname FROM lr_users LEFT JOIN lr_ranks ON lr_users.rank = lr_ran' at line 1.
What am I doing wrong?
Syntax error (comma missing before lr_users.date_joined):
$qry= mysqli_query($con, "SELECT lr_users.username, lr_users.rank, lr_users.job_id, lr_users.date_joined, lr_ranks.name AS rankname FROM lr_users
LEFT JOIN lr_ranks ON lr_users.rank = lr_ranks.rank_id")or die(mysqli_error($con));
$rows = mysqli_num_rows($qry);
If you formatted your code more legibly, this kind of error wouldn't arise:
$query = "
SELECT u.username
, u.rank
, u.job_id
, u.date_joined
, r.name rankname
FROM lr_users u
LEFT
JOIN lr_ranks r
ON u.rank = r.rank_id;
";
I'm trying to make a MySQL select for use in a mysql_query function, but I keep getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN Orgs ON Jobs.CustOrgID = Orgs.ID
LEFT JOIN Persons ON Jobs.CustPer' at line 11
I've tried everything and searched everywhere, but nothing seems to be working. All help is appreciated.
$Qry = "SELECT
Jobs.ID,
'Jobs.Status',
Jobs.JobNum,
'Orgs.Nme',
'Persons.FirstNme',
'Persons.LastNme',
'JobTypes.JobType',
'Jobs.Dsc',
'Jobs.Notes'
FROM Jobs ";
if($column !== null && $text !== null) {
$Qry .= "WHERE " . $column . " LIKE '%" . $text . "%' ";
}
$Qry .= "LEFT JOIN Orgs ON Jobs.CustOrgID = Orgs.ID
LEFT JOIN Persons ON Jobs.CustPersonID = Persons.ID
LEFT JOIN JobTypes ON Jobs.JobTypeID = JobTypes.ID
ORDER BY JobNum";
SOLUTION:
SELECT ...
FROM ...
LEFT JOIN ...
LEFT JOIN ...
WHERE ...
ORDER BY ...
My WHERE was in the wrong place, it should come after the two LEFT JOINS.
You're inserting the WHERE in the wrong place. It must come AFTER the joins:
SELECT ...
FROM ...
LEFT JOIN ...
LEFT JOIN ...
WHERE ...
ORDER BY ...
you should use backticks instead of single quotes , or you dont have to use backticks if they are not reserved keywords or separated strings .
try this :
$Qry = "SELECT
`Jobs`.`ID`,
`Jobs`.`Status`,
`Jobs`.`JobNum`,
`Orgs`.`Nme`,
`Persons`.`FirstNme`,
`Persons`.`LastNme`,
`JobTypes`.`JobType`,
`Jobs`.`Dsc`,
`Jobs`.`Notes`
FROM Jobs ";
I have the following error below for my sql query. I have been racking my head and can't seem to figure it out. Any help is appreciated.
Error:
08-07-2013, 19:05:55: Database access error. Please contact the site administrator. SELECT * FROM realty_auctions WHERE id = 42962 INNER JOIN realty_agents ON realty_auctions.agentsid=realty_agents.agentsid; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN realty_agents ON realty_auctions.agentsid=realty_agents.age' at line 3 page:/home/propoint/public_html/item.php line:567
SQL Query:
// get agent data
$query = "SELECT * FROM " realty_auctions WHERE id = " . $id ." INNER JOIN realty_agents ON " realty_auctions.agentsid=realty_agents.agentsid; ";
$result = mysql_query($query);
$system->check_mysql($result, $query, __LINE__, __FILE__);
$agent_data = mysql_fetch_assoc($result);
echo $agent_data;
join must come before where
SELECT *
FROM realty_auctions au
INNER JOIN realty_agents ag ON au.agentsid = ag.agentsid
WHERE au.id = $id
And since probably both tables have an id column I suggest naming the table explicitly with au.id.
Im new to php and my sql in trying to get all the results from this table if nothing is selected but for some reason its always displaying one result. Any ide why
$query = "SELECT *, ROUND(AVG(d.rating),0) FROM restaurant AS r, review AS d WHERE 1=1 ";
if($vicinity) $query .= "AND r.vicinity=\"$vicinity\" ";
if($cuisine) $query .= "AND r.cuisine=\"$cuisine\" ";
if($price) $query .= "AND r.price=\"$price\"";
if($name) $query .= "AND r.name LIKE \"%$name%\"";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
and im only getting the first item on the table
I would look into PDO personally. You can find out a lot about it in the manual here.
guessing you're only getting one result because the avg call without a group by is triggering some interesting behavior. try adding a group by, and i'm guessing you also want to associate the restaurants and reviews with a join. eg:
$query = "SELECT *, ROUND(AVG(d.rating),0) FROM restaurant AS r LEFT JOIN review AS d on r.id = d.restaurant_id WHERE 1=1";
...
...
$query .= ' GROUP BY r.id';
$result = mysql_query($query);
per the thread, sounds like you should look into prepared statements as well :). and the SELECT * should probably also just be SELECT r.* - the data returned as part of the * results from the rating won't be meaningful after the group by (the r.* and round(avg(d.rating),0) values should be though)
Try to use prepared statements and get results and use iterator to parse and print in a loop will get. See also this tutorial.
Ive got the query below :
$sql = "SELECT `scanners`.`KordNo`, `scanners`.`BundleNumber`
FROM `scanners`, `TWOrder`, `Stock`
INNER JOIN `TWORDER` ON `scanners`.`KordNo` = `TWOrder`.`KOrdNo`
AND `scanners`.`Date` = '" . $date . "'
INNER JOIN `Stock` ON `TWOrder`.`Product` =`Stock`.`ProductCode`
AND `Stock`.`ProductGroup` NOT BETWEEN 400 AND 650
AND `scanners`.`Scanner` IN (
ORDER BY `scanners`.`KordNo` ASC";
foreach($scanner as $x)
{$sql .= $x . ",";}
$sql .= "0);";
// And query the database
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
$return[] = $row;
}
When i echo the sql on php my admin i get the error not a unique table/alias stock;
can someone advise?
Since you're using explicit JOINs, drop the other two tables off of the FROM clause.
...
FROM `scanners`
INNER JOIN `TWORDER` ON `scanners`.`KordNo` = `TWOrder`.`KOrdNo`
...
On line 2 you have...
FROM `scanners`, `TWOrder`, `Stock`
Then you have some INNER JOINs on to TWOrder and Stock.
That's mixing syntax (, and JOIN) which is messy. Stick to JOIN
It means that TWOrder and Stock are mentioned Twice in the query
If you REALLY need to include those table multiple times in one query, you need to give them alias names, so they can be distiguished from each other.
But I think it's probably a mistake and that Line 2 should just be
FROM `scanners`
Then, also, I'm not sure how you got that to compile. You have IN ( and then an ORDER BY clause, to which you append a list of values. You should append the list before the ORDER BY and then append the ORDER BY after you've finished the loop.