Get Record from database based on field id and record id in php and mysql

Get Record from database based on field id and record id in php and mysql

Mostly we need to get all record details of foriegn key used in another table.

Such as, we have Three tables:

Orders (id, order_date, order_status, user_id)

Products (id, name, price, quantity, status)

Users (id, name, email, password, status)

in order, there is a foriegn key USER_ID.

For every order, we need to fetch user record based on user_id.

We need function using which we can fetch one record from database table

Another example, we want to fetch all records which have user_id = 4 so we can get count of orders.

In both cases,

we can use this custom made function:

function get_record_on_field($tablename, $field, $record_id)

{

$query = “select * from “.$tablename.” where “.$field.” = “.$record_id;

return mysql_assoc_fetch(mysql_query($query)); //it will return only that particular record.

}

 

I my self using this function and it reduces my and my team’s lot of efforts.

Cheers 🙂