0

I need to grab a result from one query and pop it into another.

first query

$query = 'SELECT * FROM singleprop.jos_mls WHERE MSTMLSNO = ' . $mlsnum . ';';
$result = mysql_query($query);
$row = mysql_fetch_row($result);

second query

$aquery = 'SELECT * FROM singleprop.jos_agents WHERE AGTBRDIDMM = ' . $row[0] . ';';
$aresult = mysql_query($aquery);
$agent = mysql_fetch_row($aresult);

I know about JOIN, but don't know how to apply it with a 3rd table. Does my model have something to do with $this->?

4
  • mysql_* function have been deprecated in PHP. Please update your code to use either mysqli_* of pdo_mysql http://php.net/manual/en/function.mysql-query.php Commented Jan 18, 2013 at 4:15
  • On a side note, it is suggested that you stop using mysql_* and start using PDO - php.net/manual/en/book.pdo.php as the former is being deprecated. Commented Jan 18, 2013 at 4:16
  • On a further side note, you can just probably use a JOIN. Posting schemas helps Commented Jan 18, 2013 at 4:17
  • I changed everything over to mysqli and everything stopped working. ...so that sucks. Commented Jan 18, 2013 at 4:35

1 Answer 1

2

The code looks good. You could write a query using join, which you are aware of. What is the question?

SELECT * 
FROM   singleprop.jos_mls as mls JOIN singleprop.jos_agents 
          ON singleprop.jos_mls.KEY = singleprop.jos_agents.KEY
WHERE mls.MSTMLSNO = $mlsnum

where KEY is the join key

OR

SELECT * 
FROM   singleprop.jos_agents 
WHERE  AGTBRDIDMM = (
                     SELECT COL_NAME 
                     FROM   singleprop.jos_mls 
                     WHERE MSTMLSNO = ' . $mlsnum . '
                    )

where COL_NAME is the column name for AGTBRDIDMM in the first table

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, but the problem is, I run a 3rd and 4th table and I don't know how to JOIN those in. I need the second query to contain a value from the first query. I just don't know how to get it there.
take a different variable other than query for each query..!
The code that you originally posted, you yourself included a result from the first query in the second query. That is correct. As I asked in my answer, having done that, what is your question. You seem to be in the right track.
I'm not getting a result from the second query. If I'm on the right track, I guess I have to comb through my syntax and see if something's out of place. But I do't have to do something like $this->$row[0];, do I? (I tried that and it didn't work)
Missing quotes on string declaration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.