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.
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 was forced to update to MySQL 5 and PHP 5 as my service provider is no longer supporting older versions. I have a working site with this MySQL search that was working fine. The following is an include placed on the user's listing page. When I take the SQL statement out and run it on the server it returns the correct result. My guess is that it is something to do with formatting. Any ideas why I might be getting this error?
"Query failed: 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 'ORDER BY x.id_have LIMIT 0,30' at line 1"
With this syntax
<table cellpadding="15" border="0">
<tr>
<td valign="top">
So far, the following members have these items which match;
<ul>
<?
$peoplewhomatch = mysql_query("SELECT x.id_customer, g.title Have, c.user_name as Member, g.title gwants, gg.title Has, y.id_customer x_username
FROM Want x
JOIN Want y
ON (y.id_sellversion,y.id_version) = (x.id_version,x.id_sellversion)
inner join Version as v
on x.id_version = v.id_version
inner join Version as vv
on y.id_version = vv.id_version
inner join Game as g
on g.id_game = vv.id_game
inner join Customer as c
on y.id_customer = c.id_customer
inner join Game as gg
on gg.id_game = v.id_game
WHERE x.id_have = $hid
ORDER BY x.id_have
LIMIT 0, 30")or die("Query failed: " . mysql_error());
while($row = mysql_fetch_array($peoplewhomatch))
{ ?>
<span class='greenitalic'><?=$row['Member']?></span> has <span class='highshadow'><?=$row['Has']?></span> and wants <span class='ishadow'><?=$row['gwants']?></span><BR><?}?>
</td>
</tr>
</table>
You probably need to wrap $hid in quotes like so:
$peoplewhomatch = mysql_query("SELECT x.id_customer, g.title Have, c.user_name as Member, g.title gwants, gg.title Has, y.id_customer x_username
FROM Want x
JOIN Want y
ON (y.id_sellversion,y.id_version) = (x.id_version,x.id_sellversion)
inner join Version as v
on x.id_version = v.id_version
inner join Version as vv
on y.id_version = vv.id_version
inner join Game as g
on g.id_game = vv.id_game
inner join Customer as c
on y.id_customer = c.id_customer
inner join Game as gg
on gg.id_game = v.id_game
WHERE x.id_have = '$hid'
ORDER BY x.id_have
LIMIT 0, 30")or die("Query failed: " . mysql_error());
I'm new to PDO statements and so far I've managed to work with it, use prepared statements and many things, until today.
I have two querys, the first retrieve some data, store the results and then the second query uses that data to retrieve the final data. I'm working on a bad designed DB, that's why I have to do weird things.
The first query gets the year of start and the year of end of a sport league. Then, the year is passed to the second query to get data between those years (WHERE).
The problem is that bindParam seems to not work, it doesn't bind the parameter, shows a ?, and then the SQL throws the following exception:
Connection failed: SQLSTATE[42000]: Syntax error or access violation:
1064 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 ''0701' AND ?'0630' ORDER BY e.FECHA DESC' at line 5
The SQL:
$sqlQueryAuxiliar = "SELECT ano_inicio, ano_fin
FROM TEMPORADAS
ORDER BY ano_inicio DESC
LIMIT 1;";
$sqlQuery = "SELECT e.id, e.JORNADA, DATE_FORMAT(e.FECHA, '%Y-%m-%d'),
e.HORA, c1.nombre_temporada, c2.nombre_temporada
FROM ENCUENTROS AS e
JOIN CLUBS AS c1 ON (e.COD_EQUIL = c1.siglas)
JOIN CLUBS AS c2 ON (e.COD_EQUIV = c2.siglas)
WHERE e.FECHA BETWEEN :anoInicio'0701' AND :anoFinal'0630'
ORDER BY e.FECHA DESC;";
And this is the PHP code:
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmtAux = $this->_db->prepare($sqlQueryAuxiliar);
$stmtAux->execute();
$fetched = $stmtAux->fetchAll();
$stmtAux = null;
$stmt = $this->_db->prepare($sqlQuery);
$stmt->bindParam(':anoInicio', $fetched[0][0], PDO::PARAM_STR, 12);
$stmt->bindParam(':anoFinal', $fetched[0][1], PDO::PARAM_STR, 12);
$stmt->execute();
while ($row = $stmt->fetch()) {
$partidos[] = $row;
}
$stmt = null;
You cannot concatenate strings in your query this way. Change your query to
SELECT e.id, e.JORNADA, DATE_FORMAT(e.FECHA, '%Y-%m-%d'), e.HORA, c1.nombre_temporada, c2.nombre_temporada
FROM ENCUENTROS AS e
JOIN CLUBS AS c1 ON (e.COD_EQUIL = c1.siglas)
JOIN CLUBS AS c2 ON (e.COD_EQUIV = c2.siglas)
WHERE e.FECHA BETWEEN :anoInicio AND :anoFinal
ORDER BY e.FECHA DESC
and the bindParams to
$stmt->bindValue(':anoInicio', $fetched[0][0] . '0701', PDO::PARAM_STR);
$stmt->bindValue(':anoFinal', $fetched[0][1] . '0630', PDO::PARAM_STR);
Stands to reason, you're building invalid sql:
WHERE e.FECHA BETWEEN :anoInicio'0701' AND :anoFinal'0630'
would be built as basically
WHERE e.FETCHA BETWEEN foobar'0701' AND barbaz'0630'
which is a syntax error.
You probably want
WHERE e.FETCH BETWEEN concat(:anoInicio, '0701') AND concat(:anoFinal, '0630')
instead.
If you are using bound parameters you should not also be passing in a hard-coded value in your query..
"SELECT e.id, e.JORNADA, DATE_FORMAT(e.FECHA, '%Y-%m-%d'), e.HORA, c1.nombre_temporada, c2.nombre_temporada
FROM ENCUENTROS AS e
JOIN CLUBS AS c1 ON (e.COD_EQUIL = c1.siglas)
JOIN CLUBS AS c2 ON (e.COD_EQUIV = c2.siglas)
WHERE e.FECHA BETWEEN :anoInicio AND :anoFinal
ORDER BY e.FECHA DESC;";
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/..