0

Hi everyone, I am trying to run a select query using the results from an earlier successfull query.
See below -

                            $stmt = $conn->prepare("SELECT * FROM tab1 ORDER BY id DESC");
                            $stmt->execute();
                            $total = $stmt->rowCount();

                            $cstmt = $conn->prepare("SELECT * FROM tab2 WHERE otdc_ref='$stmt->otdq_ordernum'");
                            $cstmt->execute();
                            $total = $cstmt->rowCount();                                                                        

                                while ($row = $stmt->fetchObject()) {

how is it possible to get the results of the 2nd query to be displayed, At the moment, only the results from the first query are being displayed,
Thanks in advance

2
Contributors
1
Reply
12
Views
2 Months
Discussion Span
Last Post by cereal
2

Ok,

it does not work because you are not accessing to the returned row when you call $stmt->otdq_ordernum.

Use:

$row = $stmt->fetch();

And then:

$row->otdq_ordernum;

Or use a MySQL variable.

Also rowCount() in PDO, assuming you are using it, does not return the rows found in a SELECT statement, but only the rows affected by INSERT, UPDATE, DELETE & similar statements.

See:

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.