select user name and surname from user table using array values - php

I have this code in php:
<?php
$arstring = implode(' ',$array);
?>
the $arstring while output some values like 23, 30 etc. I have a table users(user_id, name, surname). My question is how can I get all names and surnames from users table, that have a user_id equal to those that outputs $arstring. For example how to get all names surnames from users that have user_id equal to 23, 30 etc..

The query would be something like:
$sql = 'SELECT name, surname FROM users WHERE
user_id IN (' . implode(', ', $array) . ')';
PS: if $array contains data that comes directly from the user as input and you haven't sanitized it, before running the query you should make sure that $array contains only integers (so to avoid SQL injections). You may use this code (before the query):
$array = array_map('intval', $array);

Related

select user id from a table, using results of an array in php

I have a table called users(user_id, name, surname). In my php code a get an array with values like that:
<?php
$array = array();
while($e = mysql_fetch_array($favorites)){
$like_users = $e['user'];
$array[]=$like_users;
}
$arstring = implode(' ',$array);
?>
I want to select surname and name from table users, that their user_id is equal to those numbers exists as values in $array. Any idea how to do this?
Try this query
$sql='select name,surname from user where user_id in(?,?.....?)'
The number of ? is equal to the length of the array.
Then use
$query=$con->prepare($sql);
$query->execute($array);
Then fetch the returned rows.
PS:Creating prepared statements with variable number of arguments have been asked before on SO and the method is same as I have described.
collect all user_ids from SQL Query in one Array, then compare this array with your existing array
<?php
$array = array();
while($e = mysql_fetch_array($favorites)){
// $e['user'] equal user_id
$array[$user_id]= $e['user'];
}
?>
we use $array[$user_id] to save each user_id to corresponding array index, then compare two array as you want

One Select statement for criteria of one column with multiple values

I want to make a SQL SELECT statement that selects multiple rows based on the value of one column, but I have a list of valid values that match the rows I want to select.
Specifically, I have a list of user IDs. I want to select the entire row for every row that has a user id that is in this list (this list is in PHP, and I'm also making the SQL calls from there).
My code so far, looks like this:
$list_of_ids = array(1, 5, 7, 23);//list of user ids I want to select.
$query = sprintf("SELECT * FROM users WHERE user_id=?????", ????);
How do I do this?
$ids = implode(',',$list_of_ids);
$query = sprintf("SELECT * FROM users where user_id in ($ids)");
Convert array in comma separated string like this:
$list_of_ids = array(1, 5, 7, 23);
$out = implode(",", $list_of_ids) ;
$query = sprintf("SELECT * FROM users WHERE user_id in =(".$out.")");

PHP MySQL extract field then split at double spaces

I have a table which contains rows, within each row there is a field which contains peoples names seperated by a double space.
I need to extract this field from each row, then split the field into separate names and dump into another table.
What's the best way to do this? Do I split it within the sql query or extract the fields then split using PHP?
New info:
OK, the field contains full names e.g. john doe, tom smith etc etc the double spaces separate the full names e.g.
JOHN DOE TOM SMITH JOHN WOO
There might be one name in the field but there could also be potentialy up to 10
Thanks
Darren
you can do it in sql. should be something like this:
INSERT INTO tbl_dest (first_name, last_name) VALUES
(SELECT
LEFT(ts.name, LOCATE(' ',ts.name)) AS first_name,
SUBSTRING(ts.name, LOCATE(' ',ts.name)+2) AS last_name
FROM tbl_source AS ts)
as #Marc B stated, it will only work if the data is consistent (ie each row's name field contains a double space and the first double space separates the name and password.
you can ignore other rows (without the double space) by simply adding
INSERT ...
(SELECT ...
FROM tbl_source AS ts WHERE LOCATE(' ', ts.name) > 0)
if the field starts with a double space (ie first name is empty) it will also be ignored
EDIT:
since you said that each row can contain multiple pairs of first/last name. here's a code example:
$query = "SELECT `name` FROM `tbl_source`";
$res = mysql_query($query, $link);
$names = array();
while ($row = mysql_fetch_assoc($res)) {
$nmlist = explode(' ',$row['name']);
for($i=0, $n=count($nmlist); $i<$n-1; $i+=2){
$names[] = "('" . mysql_real_escape_string(stripslashes($nmlist[$i])) ."',"
."'" . mysql_real_escape_string(stripslashes($nmlist[$i+1])) ."')";
}
}
$insert_query = "INSERT INTO `tbl_dest` (`first_name`,`last_name`) VALUES "
.implode(',', $names);
mysql_query($insert_query, $link);
in this example, double spaces are between first name and last name, and between each pair
I'm not familiar with a MySQL solution for this.
I suggest grabbing the information you need, using explode(' ', $info); and then inserting back into the database.

Find an ID number in an SQL table that isn't listed in an array of numbers

I have a table that looks like the following;
id header
1 fruit
4 header_example
9 test
13 money
I then have an array that used to contain each of the id's, but I remove one. I now for example, have an array that looks like this:
array = [1, 9, 13]
How would I look at that array, and determine that the row with id=4 should be deleted?
Thanks!
On the MySQL side, you could just run this, using php to output the array contents into the IN clause:
DELETE FROM table WHERE id NOT IN ( <array contents> )
On the PHP side, you could generate the appropriate contents:
' . implode(', ', $arr) . '
$arr = array(1, 9, 11);
$query = 'DELETE FROM tbl WHERE id NOT IN (' . implode(', ', $arr) . ')';
mysql_query($query);
$sql = 'DELETE FROM table WHERE id not in ('.implode(',', $array).')';

MySQL - How to select rows where value is in array?

Ok, normally I know you would do something like this if you knew the array values (1,2,3 in this case):
SELECT * WHERE id IN (1,2,3)
But I don't know the array value, I just know the value I want to find is 'stored' in the array:
SELECT * WHERE 3 IN (ids) // Where 'ids' is an array of values 1,2,3
Which doesn't work. Is there another way to do this?
Use the FIND_IN_SET function:
SELECT t.*
FROM YOUR_TABLE t
WHERE FIND_IN_SET(3, t.ids) > 0
By the time the query gets to SQL you have to have already expanded the list. The easy way of doing this, if you're using IDs from some internal, trusted data source, where you can be 100% certain they're integers (e.g., if you selected them from your database earlier) is this:
$sql = 'SELECT * WHERE id IN (' . implode(',', $ids) . ')';
If your data are coming from the user, though, you'll need to ensure you're getting only integer values, perhaps most easily like so:
$sql = 'SELECT * WHERE id IN (' . implode(',', array_map('intval', $ids)) . ')';
If the array element is not integer you can use something like below :
$skus = array('LDRES10','LDRES12','LDRES11'); //sample data
if(!empty($skus)){
$sql = "SELECT * FROM `products` WHERE `prodCode` IN ('" . implode("','", $skus) . "') "
}
If you use the FIND_IN_SET function:
FIND_IN_SET(a, columnname) yields all the records that have "a" in them, alone or with others
AND
FIND_IN_SET(columnname, a) yields only the records that have "a" in them alone, NOT the ones with the others
So if record1 is (a,b,c) and record2 is (a)
FIND_IN_SET(columnname, a) yields only record2 whereas FIND_IN_SET(a, columnname) yields both records.

Categories