assgar Posted March 20, 2011 Share Posted March 20, 2011 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; } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 20, 2011 Share Posted March 20, 2011 It's rather hard to tell. The error you posted is a MySQL error telling you the table doesn't have a field called norma, but you haven't posted the query string so anyone can help with it. Quote Link to comment Share on other sites More sharing options...
assgar Posted March 20, 2011 Author Share Posted March 20, 2011 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; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.