0

I need help constructing a Query. I think the best way to explain it is to walk through what I need.

First, I need to look in r_agent_cstm to find the ID number based on the username of the person logged in.

//Find Agent ID
$result1 = mysql_query("SELECT id_c FROM r_agent_cstm WHERE portalusername_c = '$username'");
    if (!$result1) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    $agent_ID = mysql_fetch_row($result1);
    echo $agent_ID[0]; 
    echo "<br />";

Then I need to find any related account IDs for that agent_ID.

//Find Related Accounts
$result2 = mysql_query("SELECT r_agent_accounts_1accounts_idb FROM r_agent_accounts_1_c WHERE r_agent_accounts_1r_agent_ida = '$agent_ID[0]'");
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    while ($row = mysql_fetch_assoc($result2)) { echo $row['r_agent_accounts_1accounts_idb']; }
    echo "<br />";

Somehow...for each result in step 2 (above), I need to find more account info for the IDs found. (Note: below I set the variable that should be the account ID from step 2, but I didn't know how to pass it with more than one result, so I set it instead just so I could work through the logic on it and test that this was working so far.

$testaccount = "1ec39bf0-5ce9-ea23-d362-54c979b75b0f"; //this should be the result(s) from the previous query. 

//Find Related Accounts
$result3 = mysql_query("SELECT companyname_c,status_c FROM accounts_cstm WHERE id_c = '$testaccount'");
    if (!$result3) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    while ($row = mysql_fetch_assoc($result3)) { echo $row['companyname_c']; }
    echo "<br />";

I need help piecing these queries together in a efficient way that will echo out companyname_c and status_c in a tabled format for each result (there may be only 1, none, or there may be hundreds of results).

btnkh
  • 21
  • 1
  • 7
  • 1
    [You need to prevent SQL Injection.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 30 '15 at 15:36
  • 1
    @JayBlanchard, well I wish that anyone whoever posted a tutorial using `mysql_*` functions would remove them because googling is how I've learned. :( – btnkh Apr 30 '15 at 16:03
  • Me too @BMcCarthy, there are so many which are outdated. – Jay Blanchard Apr 30 '15 at 16:05
  • So does that mean I have to start all over and scratch what I already have...or how to I convert what I have to prepared statements? – btnkh Apr 30 '15 at 16:11
  • Click the last link in my first comment. It is a step-by-step tutorial on using PDO. You'll see how much easier it makes things for you. – Jay Blanchard Apr 30 '15 at 16:12

0 Answers0