This topic is locked
[SOLVED]

The correct way to loop through data in a array

1/30/2025 11:23:51 AM
PHPRunner General questions
P
PK author

This is probably more related to php than PHPRunner, but I could really use some help here:

if I do this:

$sql="select col1, col2, col3 from table1";
$rs = db_query($sql, $conn);

If I want to loop through the data in $rs and do something with each row,
do I do (1)

while ($data = db_fetch_array($rs)) {
$col1 = $data["col1"];
}

OR (2)

$data = db_fetch_array($rs)
foreach ($data as $rec) {
$col1 = $rec["col1"];
}

Thank you

Sergey Kornilov admin 1/30/2025

Ask ChatGPT?

C
copper21 1/30/2025

What exactly are you looking to do with the data? What do you want to do with each row?

P
PK author 1/31/2025

Thanks guys I used a different method to acheive what I wanted.
The idea was to loop through the data into a prepare statement so I can insert into my db in one trip, but that was just incredibly stupid!
I used a insert into...select query to achieve the same thing with several joins and aggregates but I got it working.

Thanks again.