SQL query error

Codes here !

Moderators: egami, macek, gesf

Post Reply
varrey
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Mar 12, 2018 2:19 am
Location: Singapore

Sat Mar 17, 2018 6:10 am

$sql = "SELECT * FROM 'Det'";
$result = $conn->query($sql);

if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{

$sql1 = "INSERT INTO 'Details' ( icno, Shares2013, TS2013, GS2013, loancashTS2013, loancashGS2013, loangiroTS2013, loangiroGS2013, giroTS2013, giroGS2013, payoutTS2013, payoutGS2013, TS2014, GS2014, payoutTS2014, payoutGS2014, TS2015, GS2015, minusShares2015, payoutTS2015, payoutGS2015, minusShares2016, addShares2016, payoutTS2016, payoutGS2016, minusShares2017, addShares2017, payoutTS2017, payoutGS2017 ) VALUES ('$row["icno"]', $row["Shares2013"], $row["TS2013"], $row["GS2013"], $row["loancashTS2013"], $row["loancashGS2013"], $row["loangiroTS2013"], $row["loangiroGS2013"], ¤row["giroTS2013"], $row["giroGS2013"], $row["payoutTS2013"], $row["payoutGS2013"], $row["TS2014"], $row["GS2014"], $row["payoutTS2014"], $row["payoutGS2014"], $row["TS2015"], $row["GS2015"], $row["minusShares2015"], $row["payoutTS2015"], $row["payoutGS2015"], $row["minusShares2016"], $row["addShares2016"], $row["payoutTS2016"], $row["payoutGS2016"], $row["minusShares2017"], $row["addShares2017"], $row["payoutTS2017"], $row["payoutGS2017"]);

}
}

it says syntax error, unexpected ' " '.... can someone please point out the error

User avatar
phpRob
New php-forum User
New php-forum User
Posts: 65
Joined: Mon Feb 26, 2018 7:15 am

Sat Mar 17, 2018 7:43 am

Try changing

Code: Select all

$sql = "SELECT * FROM 'Det'";
to

Code: Select all

$sql = 'SELECT * FROM Det';
or

Code: Select all

$sql = 'SELECT * FROM `Det`';
It is wasteful to use backticks on table/column names if they have no space in the name, but if they do have a space, backticks are required. If the backticks are present, that is fine too. Double- or single-quotes are not allowed in this situation.

Further, always use single quotes in php unless you need interpolation to occur (variable-replacement) in the string. Wrapping a php string in double-quotes is telling the engine to parse it for variables and replace any found with their respective values.

Post Reply