phpmysql 连接中的混乱

发布于 2024-11-09 11:50:38 字数 2733 浏览 2 评论 0原文

我的脚本如下:

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


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

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


$sGroupBy = " GROUP BY A.Range_sampling, A.Lot_no ";
                $sQuery = "SELECT SQL_CALC_FOUND_ROWS DATE(A.Inspection_datetime) AS Date, A.Line, A.Model, 
                                  A.Lot_no,A.Range_sampling,COUNT(A.Serial_number) AS Error,B.name AS PIC
                           FROM inspection_report A
                           LEFT JOIN Employee B
                           ON A.NIK=B.NIK
                           WHERE CHAR_LENGTH( A.Range_sampling ) < 17
                           AND MONTH(A.Inspection_datetime)=MONTH(CURRENT_DATE)" .$sGroupBy.$sOrder.$sLimit; //error
        $rResult = mysql_query($sQuery) or _doError(_ERROR30 . ' (<small>' . htmlspecialchars($sql) . '</small>): ' . mysql_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 '0, 10' at line 7

添加空格后显示:

Error message is :: "Error occuered during query execution: (<small></small>): 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 'desc LIMIT 0, 10' at line 8";

完整查询:

SELECT SQL_CALC_FOUND_ROWS DATE(A.Inspection_datetime) AS Date、A.Line、A.Model、A.Lot_no、A。 Range_sampling,COUNT(A.Serial_number) AS Error,B.name AS PIC FROM Inspection_report A LEFT JOIN Employee B ON A.NIK=B.NIK WHERE CHAR_LENGTH( A.Range_sampling ) < 17 AND MONTH(A.Inspection_datetime)=MONTH(CURRENT_DATE) GROUP BY A.Range_sampling, A.Lot_no ORDER BY desc LIMIT 0, 10

I have the script like:

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


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

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


$sGroupBy = " GROUP BY A.Range_sampling, A.Lot_no ";
                $sQuery = "SELECT SQL_CALC_FOUND_ROWS DATE(A.Inspection_datetime) AS Date, A.Line, A.Model, 
                                  A.Lot_no,A.Range_sampling,COUNT(A.Serial_number) AS Error,B.name AS PIC
                           FROM inspection_report A
                           LEFT JOIN Employee B
                           ON A.NIK=B.NIK
                           WHERE CHAR_LENGTH( A.Range_sampling ) < 17
                           AND MONTH(A.Inspection_datetime)=MONTH(CURRENT_DATE)" .$sGroupBy.$sOrder.$sLimit; //error
        $rResult = mysql_query($sQuery) or _doError(_ERROR30 . ' (<small>' . htmlspecialchars($sql) . '</small>): ' . mysql_error() );

for this script I got 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 '0, 10' at line 7

after add space show:

Error message is :: "Error occuered during query execution: (<small></small>): 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 'desc LIMIT 0, 10' at line 8";

The full query:

SELECT SQL_CALC_FOUND_ROWS DATE(A.Inspection_datetime) AS Date, A.Line, A.Model, A.Lot_no,A.Range_sampling,COUNT(A.Serial_number) AS Error,B.name AS PIC FROM inspection_report A LEFT JOIN Employee B ON A.NIK=B.NIK WHERE CHAR_LENGTH( A.Range_sampling ) < 17 AND MONTH(A.Inspection_datetime)=MONTH(CURRENT_DATE) GROUP BY A.Range_sampling, A.Lot_no ORDER BY desc LIMIT 0, 10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你鎻心 2024-11-16 11:50:38

尝试在字符串的开头和结尾放置一些空格,例如“ ORDER BY ”和“ LIMIT ”。

另外,请向我们显示错误 sQuery 变量的最终值。

现在您在 ORDER BY 子句中没有字段:

ORDER BY desc LIMIT 0, 10

BY 和 desc 之间应该是您要排序的字段名称

Try to put some spaces at the beginning and end of strings like " ORDER BY " and " LIMIT ".

Also, show us the final value of the erroneous sQuery variable.

Now you don't have a field in the ORDER BY clause:

ORDER BY desc LIMIT 0, 10

Between BY and desc should be the field name by which you are sorting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文