Fetching data from DB and comparing by putting them in Arrays

I had been trying to fetch some data from DB and do soem operations on them. The operation was to find the missing values that was expected to me. I have some values in DB that are multiples of 100 but not all the numbers — there are few missing. I need those missing numbers. The output will be the list of numbers that are missing between first and last number. Thanks to Pollob for his kind help to write this script.

Functions used:
array_diff()





<?php

/*database connection */

$host = "www.mahmudfaisal.com"; //demo host name
$user = "root";
$pass = "password"; //db password here
$dbName = "database_name";// database name here

$db = mysql_connect($host,$user,$pass) or die(mysql_error());

/*finished database connection */

if ($db)
 {
echo 'Connected!<br />';
}
mysql_select_db($dbName); //selecting the database 

$q = "select index from indices"; //database query here
$res = mysql_query($q); 

$num = mysql_num_rows($res);  // the function returns number of rows in the column;
echo "total number of data was:\t". $num. "<p>";

$array1 = array();
$array2 = array();

/* the loop contains all the data from the db  */

while ($row = mysql_fetch_assoc($res))
{
$array1[]=$row['index'];
}

/* the next loop is to stores all the multiples of 100 in array 2; */

for($i=100;$i<=end($array1);$i++)
{
   $array2[] = $i;
}

$new = array_diff($array2, $array1);  //comparing two different arrays


foreach ($new as $n=>$value){
   if($value%100==0)
       echo $value.'<br>';

}

?>

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s