MySQL - 查询无法正常工作[关闭]
问题描述:
Here is my query:
if($GetSize != ""){
$r = mysql_query("select * from `ProductSizes` ps, `products` p WHERE `ps.Size`='$GetSize' and `p.id`=`ps.Product`");
} else {
$r = mysql_query("SELECT * FROM `products` WHERE `Category`='$Cat' $FilterSQL $FilterSQLS $BrandSearch ORDER $OrderBy LIMIT $first_product_shown1, $products_per_page");
}
while($rowi = mysql_fetch_array($r))
{
$id = $rowi['id'];
$Title = $rowi['Title'];
$Image = $rowi['Image'];
$Price = $rowi['Price'];
$PriceOld = $rowi['PriceOld'];
$Rating = $rowi['Rating'];
$SDProductCode = $rowi['ProductCode'];
$Lev = $Price * GetSetting('PoundRate');
$LevOld = $PriceOld * GetSetting('PoundRate');
$Lev = number_format($Lev, 2, '.', '');
$LevOld = number_format($LevOld, 2, '.', '');
}
I think i have some mistake in it because i get no results.
Do you find any mistake and how the fixed variant must look like ?
Thanks in advance!
这是我的查询: p>
if($ GetSize! =“”){
$ r = mysql_query(“select * from`ProductsSizes` ps,`products` p WHERE`ps.Size` ='$ GetSize'和`p.id` =`ps.Product`”) ;
} else {
$ r = mysql_query(“SELECT * FROM`products` WHERE`Category` ='$ Cat'$ FilterSQL $ FilterSQLS $ BrandSearch ORDER $ OrderBy LIMIT $ first_product_shown1,$ products_per_page”); \ n}
而($ rowi = mysql_fetch_array($ r))
{
$ id = $ rowi ['id'];
$ Title = $ rowi ['Title']; \ n $ Image = $ rowi ['Image'];
$ Price = $ rowi ['Price'];
$ PriceOld = $ rowi ['PriceOld'];
$ Rating = $ rowi ['Rating'] ;
$ SDProductCode = $ rowi ['ProductCode'];
$ Lev = $ Price * GetSetting('PoundRate');
$ LevOld = $ PriceOld * GetSetting('PoundRate');
$ Lev = number_format ($ Lev,2,'。','');
$ LevOld = number_format($ LevOld,2,'。','');
}
code> pre>
\ n 我认为我有一些错误因为 我没有得到任何结果。 p>
您是否发现任何错误以及固定变体的外观如何? p>
提前致谢! p>
div>
答
You are using WHERE
clause before JOIN
:
Correction:
mysql_query("
select * from `ProductSizes` c
join `products` o on o.`id` = c.`Product`
WHERE `c.Size` = '$GetSize'
");
Side Note:
I suggest you to use mysqli_*
or PDO
instead of mysql_*
becuase it's deprecated and not available in PHP 7.
答
Consider modifying your query like below cause your query syntax is wrong. JOIN
clause should come before WHERE
select c.*
from `ProductSizes` c
join `products` o on o.`id` = c.`Product`
WHERE c.`Size` = '$GetSize';
答
SQL query seems to be incorrect... try this
mysql_query("select * from `ProductSizes` ps, `products` p WHERE `ps.Size`='$GetSize' and p.`id`=ps.`Product`")
Also a good idea to check MySQL query result using mysql_query_error function
if(mysql_query_error()) {
//Probably syntax error
}
答
Try this
mysql_query("select * from `ProductSizes` c join `products` o on o.`id`=c.`Product` WHERE c.`Size`='$GetSize'")