I am wondering, What is the proper way for inserting PHP variables into a string?
This way:
echo \"Welcome \".$name.\"!\"
>
If you want to execute a SQL
command and your variables are array members, then you should not use single quotes inside []
of array (like this: ['']
); for example if you use this string as a SQL command, you get server error 500
:
$con = mysqli_connect('ServerName', 'dbUsername', 'dbPassword');
mysqli_select_db($con, 'dbName')
//'ID' is auto increment field.
$sql = "INSERT INTO sampleTable (ID, TraceNo) VALUES ('','$sampleArray['TraceNo']')";
mysqli_query($con, $sql)
The correct string is:
//'ID' is auto increment field.
$sql = "INSERT INTO sampleTable (ID, TraceNo) VALUES ('','$sampleArray[TraceNo]')";