0

I feel like I'm missing something really simple here. Here's my sql query:

$getpages = "SELECT id FROM pages WHERE account = 2 ORDER BY page_order";       
$showpages = @mysqli_query ($dbc, $getpages);
$row = mysqli_fetch_array($showpages, MYSQLI_NUM);

I then print the first result:

echo $row[0];

And get a correct value, the id of the first page (by page order):

10

So I submit a form which simply turns that $row[0] into $row[1].

And nothing prints. I don't understand.

15
  • Please post more code not enough to go on. Where and what are you trying to print and how is your form set up? Commented Feb 8, 2012 at 15:01
  • 1
    "So I submit a form which simply turns that $row[0] into $row[1]." <- can you show this part? Commented Feb 8, 2012 at 15:02
  • See the FAQ on Why does my mysql_fetch_array only return one row stackoverflow.com/tags/php/info Commented Feb 8, 2012 at 15:02
  • Which form? Can you show the code of that form? Commented Feb 8, 2012 at 15:02
  • 1
    To get multiple rows you must fetch them in a loop. Commented Feb 8, 2012 at 15:02

4 Answers 4

1

You have to do this:

while ($row = mysqli_fetch_array($showpages, MYSQLI_NUM)) {
    $id = $row[id];
    // do something with the id
    echo $id . "<br/>"; // Echo every id
}

This will iterate through all of the results

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

8 Comments

No not the answer the OP is looking for please reread the question.
Are you sure? I think that's pretty much what he's asking for, although "so I submit a form which simply turns that $row[0] into $row[1]" is incomprehensible.
Similar problem though. When I substitute $row[0] for $row[id] I get all the values in the array and when I use $row[1] I get nothing
What are you expecting to happen when you write $row[1]? $row[1] will return the second column of your result, but you're only selecting one, id.
Do as I've explained, use a while loop to get the next $row. Every time you call mysqli_fetch_array you get one row (i.e. one id), so you need to keep calling that function to get the next row (i.e. the next id). @LaurenceBurke please vote my answer up :P
|
0

mysqli_fetch_array is a special function as each time you call it, it outputs the NEXT row.

So:

while ($row = mysqli_fetch_array(stuff)){
    $var = $row['sql column'];
    // In your case "$var = $row[0];" to get the id for the first row found.
}

is how you use it.

This will keep running the function until mysqli_fetch_array() eventually returns false. These each time it will output a new row. And the $row array is the array of one row in the sql table.

Use :

print_r($row);

in the while loop to see exactly what's being returned for use.

3 Comments

No he is not asking about showing all the rows in the page. He is post refreshing. Please read the question fully.
Ah... removing my answer then.
Brought this back as I think this is actually what he was asking for
0
$getpages = "SELECT id FROM pages WHERE account = 2 ORDER BY page_order";       
$showpages = @mysqli_query ($dbc, $getpages);
$row = mysqli_fetch_array($showpages, MYSQLI_NUM);

Question how many rows are fetched in this query in this case id, So MYSQL will result to 1 row affected, But since you are using

$row = mysqli_fetch_array($showpages, MYSQLI_NUM);

This code you used will not output anything try

$row[n]  as n-1

Comments

-1

$result->fetch_assoc() or mysqli_fetch_assoc($result) catch one line at a time, from a mysqli_result:

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
    echo "0 results";
}

source: http://www.w3schools.com/php/php_mysql_select.asp

Comments

Your Answer

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