0

I am trying to run this sql query where 2 input parameters are used which the user has entered on the website.

However somehow the query doesn't run because of this error and I have no clue what to change to make it run

I tried with $sourcecoin and with "$sourcecoin" without in the query success. But is the query wrong or something else?

<?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();
?>

I am getting the error message as stated in the subject field.

"PHP Parse error: syntax error, unexpected '$sourcecoin' (T_VARIABLE), expecting ',' or ';'"

treyBake
  • 6,440
  • 6
  • 26
  • 57
  • Which line is the error on? – Barmar Apr 16 '19 at 16:06
  • I don't get that error, I get an error for this line: `echo "They have got the following exchange(s) in common". $row["Exchanges.exchange"] "
    ";` It needs a `.` before `"
    "`.
    – Barmar Apr 16 '19 at 16:09
  • The error you got is often from leaving out the `;` at the end of the previous line. – Barmar Apr 16 '19 at 16:12

1 Answers1

-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.'";

or put {$sourcecoin}

It's good though to get in the habit of using {$...} in double quotes instead of only $..., for times where you need to insert the variable in a string where it's not obvious to PHP which part is the variable and which part is the string.