0

I am trying to assign the contents of a 2D array to a string in PHP.

$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '$output['username']';";

I know the problem exists in how I'm writing the $output variable assignment.

The following line of code outputs the correct data from the variable:

echo $output['username'];  

The following error is being thrown:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
Filip Korngut
  • 207
  • 1
  • 3
  • 10

4 Answers4

1

Parameters surrouned by curly brackets will work well in your case. Here is what i mean. {$array['key']}
And for your example:

$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '{$output['username']}';";
FiftyStars
  • 298
  • 1
  • 16
0

You need to concatenate this string as the multiple single quotes from accessing the array element are mixing up the string.

$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '" . $output['username'] ."';";
q.Then
  • 2,743
  • 1
  • 21
  • 31
0

Try this:

 $sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '".$output['username']."'";
aldrin27
  • 3,407
  • 3
  • 29
  • 43
0

You can also try this:

$user_name =  $output['username'];
$sql = "SELECT Order_Code FROM Order WHERE CUST_CODE = $user_name";
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
kev
  • 33
  • 1
  • 6