Regarding maintainability / good practice issues vs speed I have a question regarding what is the best way to SELECT some data from a database.
I have the item_id of an item bought. I want to get a customer's billing information based on the item he bought. Should I do this with two queries or one using joins?
Here is my dbase schema:
customer_info
customer_id
name
cc_num
expdate
item_bought
item_id
customer_id
function getCustomerInfo($customer_id)
{
$query = "SELECT customer_info.name, customer_info.cc_num, customer_info.expdate FROM customer_info WHERE customer_info.customer_id=$customer_id ";
$result = mysql_query($query);
$data = mysql_fetch_assoc($result);
return $data;
}
function getCustomerInfoWithItemId($item_id)
{
$query = "SELECT item_bought.customer_id FROM item_bought WHERE item_bought.item_id=$item_id ";
$result = mysql_query($query);
$data = mysql_fetch_assoc($result);
$customer_data = getCustomerInfo($data['customer_id']);
return $customer_data;
}
$data = getCustomerInfoWithItemId(400);
OR
function getCustomerInfoWithItemId($item_id)
{
$query = "SELECT customer_info.name, customer_info.cc_num, customer_info.expdate FROM customer_info, item_bought WHERE item_bought.item_id=$item_id ";
$query .= "AND customer_info.customer_id = item_bought.customer_id ";
$result = mysql_query($query);
$data = mysql_fetch_assoc($result);
return $data
}
$data = getCustomerInfoWithItemId(400);