How to use PHP to query MySQL for certain rows, then for each row, based on a UID, query another table based on value?

Dimitri Bostrovich :

I am making an application for people to sell items and make product offers. If a user views their product offers, it must contain each offers information, as well as details of each user for that offer.
First tell the database which offers need to be queried, activated by an ajax onclick function. (ive got that)

Second, query the OFFERS table for offers with status 'o' and a PID equal to the $_GET['pid'](5).

OFFERS
------------------------------
| uid | pid | status | price |
------------------------------
|  5  |  5  |   o    |  49   |
|  7  |  5  |   o    |  45   |
------------------------------

Then query the USERS table for information based on each offer? After that, for each result, based on the the UID, query the users table for information related to the user.

USERS
-------------------------------------
| uid |  fname   |  lname  | rating |
-------------------------------------
|  5  |   John   |   Jan   |   4.3  |
|  7  |   Mark   |   Mull  |   4.2  |
-------------------------------------

Here is a snippet of my PHP statement to the database.

$stmt = $conn->prepare('SELECT offerdate,price FROM offers WHERE pid=? AND status=?');
$o = 'o';
$stmt->bind_param('is', $_POST['pid'],$o);
$stmt->execute();
$stmt->bind_result($offerdate,$price);

The other statement I need

$stmt = $conn->prepare('SELECT fname,lname,rating, FROM users WHERE uid=?');
$stmt->bind_param('i', );
$stmt->execute();
$stmt->bind_result($fname,$lname,$rating);

Any help would be awesome, thank you!

Barmar :

Do a single query that joins the two tables.

SELECT offerdate, price, fname, lname, rating
FROM offers
INNER JOIN users ON users.uid = offers.uid
WHERE offers.pid = ? AND offers.status = ?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=238360&siteId=1