Magento MySQL search based on phone number I'm building a search for Magento. Magento API doesn't provide an easy way to look up a customer using a phone number so I created a custom solution. My MySQL/PHP OOP skills are limited. Did I miss anything or is there a better way to do this?

&lt;?php

    DEFINE ('DB_HOST',      'localhost');
    DEFINE ('DB_USER',      'theuser');
    DEFINE ('DB_PASSWORD',  'thepassword');
    DEFINE ('DB_NAME',      'magento');

    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    $search_query = "5555555555";




    // find customers with matching phone numbers
    $query = "SELECT value, entity_id FROM customer_address_entity_varchar WHERE value=$search_query LIMIT 100";

    $response = mysqli_query($dbc, $query);




    // save found phone numbers to $found_customers array
    $found_customers = array();
    $position = 0;

    while($row = mysqli_fetch_array($response)) {

        $found_customers[$position]['entity_id']    = $row['entity_id'];
        $found_customers[$position]['value']        = $row['value'];

        $position++;
    }

    // reset everything just in case
    unset($query, $response, $row);
    $position = 0;



    // find matching parent_ids and save to $found_customers array
    foreach($found_customers as $found_customer) {
        $friendly_entity_id = $found_customer['entity_id'];

        $query = "SELECT parent_id FROM customer_address_entity WHERE entity_id=$friendly_entity_id LIMIT 1";
        $response = mysqli_query($dbc, $query);
        $row = mysqli_fetch_array($response);
        $found_customers[$position]['parent_id'] = $row['parent_id'];

        $position++;
    }

    var_dump($found_customers);

?&gt;
you haven't missed anything (the code works as intended), but you can improve the performance a bit and make it more readable (for someone who is used to PHP and MySQL).

I've modified your code and added comments where necessary.
As on of the comments requested, I'll add a bit more detail.

The most important thing is the select query. As it was originally you had to performed another query for every row from the original query in order to get the corresponding parent_id from the customer_address_entity table. You should avoid doing this because it is not very efficient.  

The modified query selects everything you need in one go using a JOIN statement. The JOIN statement allows you to perform a SELECT on both tables that were joined, but you have to specify which column you want from each table, that's why the original SELECT value becomes SELECT customer_address_entity_varchar.value. 

When looping the return results in order to assign them to a new array, like you're doing below, it's useful to now that:
a. Using $found_customers[]['entity_id'] instead of $found_customers[$position]['entity_id'] has the same results, because the $array[] notation in PHP makes it so that every assignment appends that assignment to the end of the array, auto-incrementing the index.
b. Since you want to transfer all the data from $row into $found_customers, you can simply do $found_customers[] = $row and it will have the same effect in this case.
c. (Optional) A simple quality of life trick, use mysqli_fetch_assoc' instead of 'mysqli_fetch_array'. Whenvar_dump`ing you won't see the same values twice for each row (one with and index and one with the key, as seen in the comments), only once, with the key.

$position = 0;

while($row = mysqli_fetch_array($response)) {

    $found_customers[$position]['entity_id']    = $row['entity_id'];
    $found_customers[$position]['value']        = $row['value'];

    $position++;
}


If you want to have unique values for a column (as requested in the comments), one way to do it is to GROUP BY that column. The downside is that you will lose the data contained in the duplicate results.

&lt;?php

DEFINE ('DB_HOST',      'localhost');
DEFINE ('DB_USER',      'theuser');
DEFINE ('DB_PASSWORD',  'thepassword');
DEFINE ('DB_NAME',      'magento');

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$search_query = "5555555555";

// find customers with matching phone numbers
/* This is probably the most important part: using a JOIN. 
   It will save you having to do a separate query for each customer */

$query = "SELECT customer_address_entity_varchar.value, customer_address_entity_varchar.entity_id, customer_address_entity.parent_id 
          FROM customer_address_entity_varchar
          JOIN customer_address_entity 
              ON customer_address_entity_varchar.entity_id = customer_address_entity.entity_id 
          WHERE customer_address_entity_varchar.value = $search_query 
          GROUP BY customer_address_entity.parent_id 
          LIMIT 100";

$response = mysqli_query($dbc, $query);


// save found phone numbers to $found_customers array
$found_customers = array();

/* in PHP you can just use $found_customers[] and it will auto-increment
 no need to use $position */

/* also, since your select query on selects 3 columns, you can assign the results directly */

while($row = mysqli_fetch_assoc($response)) {
    $found_customers[] = $row;
}

/* The foreach loop is no longer necessary because the initial query  (now with a JOIN) returns all the required data */

var_dump($found_customers);

?&gt;