Mysql查询在PhpMyadmin中工作,但在php中连接时不能

时间:2014-07-30 08:58:30

标签: php mysql phpmyadmin

我写了一个mysql查询,在phpmyadmin版本3.5.4和mysql版本中工作正常:5.1.73

查询

$indexscoreqry = ("set @ctrnk:=-1; select x.* from  ( select productID , productname , price , mobile_score , format((((5/100)*mobile_score)+(5-(@ctrnk:=@ctrnk+1)*(5/(select count(*) from product a join product b on a.productID = b.productID where a.mobile_score = $mobilescore and a.categoryID !=13 and a.price > 0 order by a.price asc))))/2 , 2) as indexscore from product  where mobile_score = $mobilescore and categoryID !=13 and price > 0 order by price asc ) as x where x.productID = $proid");     
    $runQuery = mysql_query($indexscoreqry);
    $indexvalue = mysql_fetch_array($runQuery) or die(mysql_error());

查询输出:

set @ctrnk:=-1; select x.* from ( select productID , productname , price , mobile_score , format((((5/100)*mobile_score)+(5-(@ctrnk:=@ctrnk+1)*(5/(select count(*) from product a join product b on a.productID = b.productID where a.mobile_score = 75 and a.categoryID !=13 and a.price > 0 order by a.price asc))))/2 , 2) as indexscore from product where mobile_score = 75 and categoryID !=13 and price > 0 order by price asc ) as x where x.productID = 2242

PHPMyAdmin输出:

productID productname            price  mobile_score indexscore 
 2242     Motorola Moto G 16GB  13999       75        3.96

当我在PHP代码中运行时,我收到错误消息

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/public_html/controllers.php on line 3817 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 'select x.* from ( select productID , productname , price , mobile_score , format' at line 1

根据Pnoniq

的建议

我在单独的页面中尝试过mysqli,即使我遇到了同样的错误。这是我的完整脚本。

<?php

   error_reporting(-1);
   ini_set('display_errors', 'On');


$con=mysqli_connect("localhost","root","Zf5NODXBaaUElNHQ","mydb");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

echo $sql="set @ctrnk:=-1;
select x.* from
(
select productID , productname , price , mobile_score , format((((5/100)*mobile_score)+(5-(@ctrnk:=@ctrnk+1)*(5/(select count(*) from product a
join product b on a.productID = b.productID
where a.mobile_score = 77 and a.categoryID !=13 and a.price > 0 order by a.price asc))))/2 , 2) as indexscore
from product
where mobile_score = 77 and categoryID !=13 and price > 0 order by price asc
) as x where x.productID = 1582";
// mysqli_query or mysqli_multi_query
$result=mysqli_multi_query($con,$sql) or die(mysqli_error($con));

$row = mysqli_fetch_array($result, MYSQLI_BOTH);


print_r($row);

mysqli_close($con);
?>
Error : Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/public_html/mysqli.php on line 26

2 个答案:

答案 0 :(得分:4)

不要使用mysql _ *

前往docs并阅读警告。

然后,当从php查询数据库时,只允许1个查询。您正在传递2个查询。所以有你的问题

答案 1 :(得分:-1)

请检查数据库连接(如果已创建)。如果连接未在php页面中创建,则在页面中使用以下连接字符串

EG。数据库连接代码:

$con = mySql_connect("localhost","username","password");
$db = mySql_select_db($con,"databasename")