简体   繁体   中英

how to convert multiple row data into array and subtract it from another array?

I am fetching multiple rows from my data base like,

$query = "SELECT * FROM student_info where district = '".$admdistrict."' AND user_status = 'approved' ORDER BY id DESC";
$result = mysql_query($query) or die(mysql_error());
$delete = mysql_fetch_array($resultt);
$std_delete_array = explode(',', $delete['id']);

the value of id in database is like 4,5,6 in different rows but it is giving only first value 4.
Another array I am fetching is,

$query="SELECT * FROM events where id='$district'";
$showdata=mysql_query($query) or die(mysql_error());
$user=mysql_fetch_array($showdata);
$std_fulllist_array = explode(',', $user['std_list']);

the value in database is 4,5,6,8. But in single coloumn, so it is giving proper output. now I want to subtract $std_delete_array from $std_fulllist_array . I am using following code ,

$values = array_diff($std_fulllist_array, $std_delete_array);

which is only subtracting first value of $std_delete array.

strucutre of table student_info is id ! name ! District 4 a panipat 5 b panipat 6 c panipat

strucutre of table events is id ! district ! std_list 1 panipat 4,5,6 2 karnal 4,7,8 3 chandigarh 5,6,7

You are saying the value of id in database is like 4,5,6 in different rows that means your table is look like

id district
-- ---------
4  abc
5  def
6  geh
8  ijk

so your first query is fetching multiple records.

EDIT

So your query would be

$ret_ = array();
$query = "SELECT * FROM student_info where district = '".$admdistrict."' AND user_status = 'approved' ORDER BY id DESC";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
    // output data of each row
    while($row = mysql_fetch_array($result)) {
        echo $row['id'];
        $ret_[] = $row;  
    }
}
print_r($ret_);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM