PHP解析错误:语法错误,意外的'$ sourcecoin'(T_VARIABLE),期望为','或';'

时间:2019-04-16 16:05:40

标签: php sql

我正在尝试运行此sql查询,其中使用了用户在网站上输入的2个输入参数。

但是由于这个错误,查询以某种方式无法运行,而且我也不知道如何更改才能使其运行

我尝试使用$sourcecoin和“ $ sourcecoin”,但查询没有成功。但是查询是错误的还是其他原因?

<?php
$servername = "xx";
$username = "xx";
$password = "xx";
$dbname = "xx";
$sourcecoin = strip_tags(trim($_POST["sourcecoin"]));
$destcoin = strip_tags(trim($_POST["destcoin"]));


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    echo "Connection not established. Check credentials";
} 


$sql = "SELECT Pairs_Source.Exchange, Exchanges.HyperLink
FROM Pairs AS Pairs_Source INNER JOIN Pairs AS Pairs_Dest ON Pairs_Source.Exchange = Pairs_Dest.Exchange
Left join Exchanges on Pairs_Source.Exchange=Exchanges.Exchange
WHERE Pairs_Source.Coin='$sourcecoin' AND Pairs_Dest.Coin='$destcoin'";


$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "They have got the following exchange(s) in common". $row["Exchanges.exchange"] " <br>";
    }
} else {
    echo "Unfortunately these 2 coins don't have an exchange in common";
}
$conn->close();
?>

我收到主题字段中所述的错误消息。

  

“ PHP解析错误:语法错误,意外的'$ sourcecoin'(T_VARIABLE),期望为','或';'”

1 个答案:

答案 0 :(得分:-2)

$sql = "SELECT Pairs_Source.Exchange, Exchanges.HyperLink
        FROM Pairs AS Pairs_Source INNER JOIN Pairs AS Pairs_Dest ON Pairs_Source.Exchange = Pairs_Dest.Exchange
        Left join Exchanges on Pairs_Source.Exchange=Exchanges.Exchange
        WHERE Pairs_Source.Coin="'.$sourcecoin.'" AND Pairs_Dest.Coin="'.$destcoin.'";

或放置{$sourcecoin}

最好养成在双引号中而不是仅在双引号中使用{$ ...}的习惯,在某些情况下,您需要在字符串中插入变量,而这对于PHP来说并不明显。是变量,哪一部分是字符串。