gpt4 book ai didi

php - MySQL 查询在 phpMyAdmin 中有效,在 PHP 中失败

转载 作者:可可西里 更新时间:2023-11-01 07:06:25 28 4
gpt4 key购买 nike

SELECT SQL_CALC_FOUND_ROWS * 
FROM (
SELECT *
FROM tbl_substances
LIMIT 0 , 25
) AS s
LEFT JOIN (
SELECT subid, list1, list2, list3, list4, list5
FROM tbl_substances_lists
WHERE orgid = '1'
) AS x ON s.subst_id = x.subid
LEFT JOIN (
SELECT subid, info
FROM tbl_substances_info
WHERE orgid = '1'
) AS y ON s.subst_id = y.subid

想法是,您有一个物质主列表 (tbl_substances),然后如果您在 tbl_substances_lists 或 tbl_substances_info 中输入了有关它们的任何信息,那么也可以显示这些信息(只要您使用正确的组织 ID 登录)

显示所有物质很重要,即使它们没有自定义信息,这就是我使用 LEFT JOIN 的原因。

这个查询在 phpMyAdmin 中完美运行,但是当我在我的数据库脚本中使用它时,我得到:

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 ') AS s LEFT JOIN (SELECT subid, list1, list2, list3, list4, list5 FROM tbl_substances_' at line 2

我不确定问题是否是我遗漏的明显问题,或者是否与这段代码使用 mysql_query 这一事实有关,我知道它已被弃用且过时等等。

我不是数据库专家,所以如果您觉得这个查询非常难看,那么我提前道歉!

编辑 2

这是构建此查询的代码(它根据您要搜索的内容动态构建,但这是基本形式)

    /*
* Length
*/

if ( isset( $_POST['iDisplayStart'] ) && $_POST['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".mysql_real_escape_string( $_POST['iDisplayStart'] ).", ".
mysql_real_escape_string( $_POST['iDisplayLength'] );
}


/*
* Ordering
*/

$sOrder = "";
if ( isset( $_POST['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<intval( $_POST['iSortingCols'] ) ; $i++ )
{
if ( $_POST[ 'bSortable_'.intval($_POST['iSortCol_'.$i]) ] == "true" )
{
$iColumnIndex = array_search( $_POST['mDataProp_'.$_POST['iSortCol_'.$i]], $aColumns );
$sOrder .= $aColumns[ $iColumnIndex ]."
".mysql_real_escape_string( $_POST['sSortDir_'.$i] ) .", ";
}
}

$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}

/*
* Table info
*/
$sTable = "tbl_substances ".$sLimit.") AS s
LEFT JOIN (
SELECT subid, list1, list2, list3, list4, list5
FROM tbl_substances_lists
WHERE orgid = '".$orgid."'
) AS x
ON s.subst_id = x.subid
LEFT JOIN (
SELECT subid, info
FROM tbl_substances_info WHERE orgid = '".$orgid."'
) AS y
ON s.subst_id = y.subid";

$sWhere = "";


/*
* SQL queries
* Get data to display
*/

$sQuery = "
SELECT SQL_CALC_FOUND_ROWS * FROM (SELECT * FROM $sTable
$sWhere
$sOrder
$sLimit
";

$rResult = mysql_query( $sQuery ) or die(mysql_error());

假设此时 $sWhere 和 $sOrder 中没有任何内容 - $sLimit 由用户选择,但在这种情况下它将是 LIMIT 0, 25 以获取前 25 条记录。

在这种情况下,这一切结合起来产生回显 $sQuery 的结果:

SELECT SQL_CALC_FOUND_ROWS * 
FROM (
SELECT *
FROM tbl_substances LIMIT 0, 25
) AS s
LEFT JOIN (
SELECT subid, list1, list2, list3, list4, list5
FROM tbl_substances_lists
WHERE orgid = '1'
) AS x
ON s.subst_id = x.subid
LEFT JOIN (
SELECT subid, info
FROM tbl_substances_info
WHERE orgid = '1'
) AS y
ON s.subst_id = y.subid

最佳答案

还没有分析代码,但在查询方面我没有看到语法错误。但我建议您像这样编写查询:

SELECT SQL_CALC_FOUND_ROWS * 
FROM tbl_substances s
LEFT JOIN tbl_substances_lists x ON s.subst_id = x.subid AND x.orgid = '1'
LEFT JOIN tbl_substances_info y ON s.subst_id = y.subid AND y.orgid = '1'
LIMIT 0, 25

应该给出相同的结果。

关于php - MySQL 查询在 phpMyAdmin 中有效,在 PHP 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13803570/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com