-3

can you see what is wrong on this query? I have been watching it for really long time and I can't see it.

ERROR: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 1

$sql="SELECT country_name FROM countries WHERE country_id IN (";
    foreach($cartContentVsDatabase as $key => $val){
      $sql.= $key['country_id'].",";        
    }
    ")";
Siyual
  • 16,415
  • 8
  • 44
  • 58
Tom Cake
  • 15
  • 1
  • 1
  • 5

4 Answers4

2

Some issues:

  • You have a trailing comma in your country list,
  • countries should be quoted as strings,
  • you are accessing the values from the key instead of the value part of the array elements.
  • you have a dangling closing parenthesis, which does nothing.
  • You should not even inject country strings, as that makes your code vulnerable for code injection: use prepared statements.

Here is code you could use:

// first put the countries in an array
foreach($cartContentVsDatabase as $key => $val){
  $countries[] = $val['country_id'];  
}
// create a list of `?`: one for every country
$in = join(',', array_fill(0, count($countries), '?'));

// use that in the query
$sql="SELECT country_name FROM countries WHERE country_id IN ($in)";

// Prepare the statement (this is PDO syntax)
$statement = $pdo->prepare($select);

// Pass the countries as parameter values, and execute
$statement->execute($countries);

See this Q&A for more on prepared statements in the context of this in (...) clause.

Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286
1

try this, change ")"; to $sql.= ")";

    $array_count = count($cartContentVsDatabase);
    $temp_count = 0;
    $sql="SELECT country_name FROM countries WHERE country_id IN (";
    foreach($cartContentVsDatabase as $key => $val){
       $temp_count++;
      if($array_count < $temp_count)
      {
           $sql.= $val['country_id'];   
      }
      else
      {
          $sql.= $val['country_id'].",";   
      }

    }
    $sql.=  ")";
Dave
  • 3,073
  • 7
  • 20
  • 33
0

You could make your life a lot easier by

$sql= "SELECT country_name FROM countries WHERE country_id IN (".implode(",",array_column($cartContentVsDatabase,"country_id")). ")";

You can (and probably should) use a prepared query e.g. like the one below:

$sql= "SELECT country_name FROM countries WHERE country_id IN (".implode(",",array_fill(0,count($cartContentVsDatabase),"?")). ")";

and then bind the contents of $cartContentVsDatabase when you execute.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
0

In your code you are not concatenate ")"; properly at the end. you can also store data into array and than use implode() for comma separated values like:

Example:

<?php

$sql = "SELECT country_name FROM countries ";

$countries = array();
foreach($cartContentVsDatabase as $key => $val){
  $countries[] = $val['country_id'];  // store country id in your array
}

if(count($countries) > 0){
     $countrylist = implode("','",$countries); // implode all country list with comma.    
     $sql .= "WHERE country_id IN ('$countrylist')";
}    
echo $sql; // print your query.

?>

Still i don't know, $key['country_id'] is the correct one or not, i think this should be $val['country_id'].

devpro
  • 16,184
  • 3
  • 27
  • 38