Reputation: 516
I need to check which of my addressbook users are using my iOS app (using their emails). To do this, I send a contacts list to my PHP server and check for emails, which are already registered. Then, I return an array of users and show them in an iOS TableView. The problem is that when I have 100-200 contacts, the retrieval is lightening fast, but when I have 1000-1500 contacts it takes at least 3-4 minutes. How can I improve it?
PHP code:
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
// Decoding JSON into an Array
$array = json_decode($jsonInput,true);
if (empty($array)) {
echo '<h1>Array is Empty!!!<h1>';
return;
}
require('DBconnection.php');
$DBconnection = new DBconnection;
$link = $DBconnection->connectToDatabase();
if ($link)
{
$DBconnection->selectDatabase();
foreach($array as $subArray)
{
foreach($subArray['email'] as $email)
{
$query="SELECT email,picture from User WHERE email='$email'";
$result= mysql_query($query);
$row = mysql_fetch_assoc($result);
if(mysql_num_rows($result) > 0)//if it finds any row
{
$Contact = array(
"fullname" => $subArray['fullname'],
"email" => $email,
"picture" => "http://www.mysite.co/Project/Users/Images/".$email."."."jpg"."?".time()
);
$ReturningArray[] = $Contact;
}
}
}
echo json_encode($ReturningArray);
}
Upvotes: 0
Views: 289
Reputation: 6581
If you think of performance the first thing you need to check is how many db calls are there because they are slow. So all you need to do in your case is to perform a single mysql request with all the emails you got from ios call:
SELECT email, picture FROM User WHERE email IN ('email1', 'email2', ...)
Then you need to iterate through the results of sql call, so that you know which of passed emails exist in your database.
And you should consider switching to PDO for sure.
Upvotes: 0