0

I understand this could appear alarming but bear with me.

I need to echo every field in every row in a table.

This is only an example - I have removed the HTML wrapped around it to improve readability.

$a = 1;

while ($a <= $count_rows) {
    $query = "SELECT col1, col2 etc.. FROM table WHERE `id`='$id'";

    $result = mysqli_query($con, $query);
    $i = 1; 

    while($i <= $count_fields) {
        $row = mysqli_fetch_array($result, MYSQL_NUM);
        echo "$row[$i]";
        $i++;       
    }

    $a++;
    $id = $a;
}

This only outputs the first field of every row? Why?

If I echo $row[2] I get nothing!

6
  • 1
    Did you know that you can retrieve multiple results using a single query? So using MySQL, you could just have a query of the form select * from table where id in ( 1,2,3,4,5,6 ) Commented May 15, 2013 at 14:47
  • Yeah i knew but anyway of doing it dynamically? I don't know how many rows i'm going to have you see Commented May 15, 2013 at 14:49
  • What is $count_fields? Commented May 15, 2013 at 14:50
  • @bobobobo how do i accept your revision? Commented May 15, 2013 at 14:51
  • Oh it's just a suggestion/tip, not an edit. Commented May 15, 2013 at 14:52

1 Answer 1

2

If I echo $row[2] I get nothing!

because it's actually third item
and there is some strange code interfering with $i variable

Anyway, to get every column from every row ou need a code like this

$query = "SELECT * FROM table";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_row($result)) {
    foreach ($row as $index => $value) {
        echo "$index => $value, ";
    }
    echo "<br>\n";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Every time he does $row = mysqli_fetch_array($result, MYSQL_NUM); he's only echo'ing $row[1]
+1 mysqli_fetch_row($result) instead of MYSQL_NUM Ill give it a try but it appears more logical, tend to shy away from foreach - it doesn't have any adverse affect on html inside the loop does it?
@bobobobo why? the $i = 1; is in the first loop not the second...?

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.