Jump to content

IMPLODE not working


assgar

Recommended Posts

 

I am trying to use a single select to get the first and last name for a group of users.

Here are the two approaches (Test 1 & Test 2) I tried to get the data.

Both approach gave the same error.

 

 

Error: Unknown column 'norma' in 'where clause'

 

What is wrong with my approch(s)?

 

<?
//open connection
$mysqli = db_connect();
db_select($mysqli, $db_id);

//initiate array
$per_list = array();
$per = array();

//assign values of user name to array
$per_list[] = "norma";
$per_list[] = "alex";
$per_list[] = "sean";



/**Test 1**/       
$query = "SELECT userid, first_name, last_name
	   FROM users
  	   WHERE username IN (".implode(',',$per_list).")";
  $result = mysqli_query ($mysqli, $query) or die("Error: ".mysqli_error($mysqli));
while($row = mysqli_fetch_array($result))
   {
   	 $per[] = $row;
   }  
   

/**Test 2**/
$per_list = implode(", ", $per_list);   

$query = "SELECT userid, first_name, last_name
	   FROM users
  	   WHERE username IN ($per_list)";
$result = mysqli_query ($mysqli, $query) or die("Error: ".mysqli_error($mysqli));
while($row = mysqli_fetch_array($result))
   {
   	 $per[] = $row;
   }
   
?>   

Link to comment
Share on other sites

Thanks for the response.  I was able to correct my error.  Below are three approaches that works

 

//Test 1  works 

$query = "SELECT userid, first_name, last_name
	   FROM users
  	   WHERE username IN ('".implode("','",$per_list)."')
       AND org_code = '$org_code'
       AND deleted = 'N'";
  $result = mysqli_query ($mysqli, $query) or die("Error: ".mysqli_error($mysqli));
while($row = mysqli_fetch_array($result))
   {
   	 $per[] = $row;
   }  

 

//Test 2 works

$per_list = implode("', '", $per_list);   

$query = "SELECT userid, first_name, last_name
	   FROM users
  	   WHERE username IN ('$per_list')
       AND org_code = '$org_code'
       AND deleted = 'N'  ";
$result = mysqli_query ($mysqli, $query) or die("Error: ".mysqli_error($mysqli));
while($row = mysqli_fetch_array($result))
   {
   	 $per[] = $row;
   }

 

 

//test 3 works

$query = "SELECT userid, first_name, last_name 
          FROM users
          WHERE username IN ('".join("','", array_values($per_list))."')
          AND deleted = 'N'";
$result = mysqli_query ($mysqli, $query) or die("Error: ".mysqli_error($mysqli));
while($row = mysqli_fetch_array($result))
   {
   	 $per[] = $row;
   }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.