简体   繁体   中英

What is wrong in my SQL query?

I have a query that joins two table, and it keeps getting the 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' at line 1`.

I can't seem to figure out what is wrong here?

I've already check this but in my case it is different.

Here is my query:

$sQuery = "SELECT SQL_CALC_FOUND_ROWS A.id, A.studentNum, B.lastName, B.firstName, B.middleName, B.year, B.courseBlock, B.status, A.facultyloading_id"+
          " FROM table_enrolledstudents AS A"+
          " INNER JOIN table_student AS B"+
          " ON A.studentNum = B.studentNum";  

What do you think is wrong? Can you help me? Thank you!

Use the PHP string concatenation operator . (dot).

Or to improve readability/maintainability you could also put the query into a single string with line breaks.

$sql = "SELECT SQL_CALC_FOUND_ROWS A.id, A.studentNum, B.lastName, B.firstName, B.middleName, B.year, B.courseBlock, B.status, A.facultyloading_id
        FROM table_enrolledstudents AS A
        INNER JOIN table_student AS B
        ON A.studentNum = B.studentNum";

Explanation: This is because PHPs type system allows arithmetic operations on strings (which can contain numbers), eg "1" + "2" would return the sum, which is 3 . If a string used in an arithmetic operation does not contain a parsable number, then it is treated as zero, so the result of "abc" + 5 is 5 .

As Mr. Aleksander Bavdaz said, I have replaced the plus and make it a dot and the error is gone. Here is my new query. Thank you for your help!

$sQuery = "SELECT SQL_CALC_FOUND_ROWS A.id, A.studentNum, B.lastName, B.firstName, B.middleName, B.year, B.courseBlock, B.status, A.facultyloading_id".
                " FROM table_enrolledstudents AS A".
                " INNER JOIN table_student AS B".
                    " ON A.studentNum = B.studentNum"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM