Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

php - mysqli_fetch_array returning only one result

I'm trying to make a very, very simple query of a small mysql database, using the following code (with appropriate values in $host, etc.):

$result = mysqli_query($connection, "select university from universities_alpha");
$row = mysqli_fetch_array($result);

echo print_r($result);
echo '<br><br>';
echo print_r($row);

As you can see, I printed out the results in a human-readable way, yielding:

mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => Array ( [0] => 19 ) [num_rows] => 9 [type] => 0 ) 1

Array ( [0] => Arizona State Univ. [university] => Arizona State Univ. ) 1

There are a few example universities in that column, so I'm not sure what I'm doing wrong.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

mysqli_fetch_array works by pointers each time it's called

Imagine the following

$result = mysqli_query($connection, "select university from universities_alpha");
$row = mysqli_fetch_array($result); // this is the first row
$row = mysqli_fetch_array($result); // now it's the second row
$row = mysqli_fetch_array($result); // third row

To actually display the data the way you want it to, I suggest you do the following

$rows = array();
$result = mysqli_query($connection, "select university from universities_alpha");
while($row = mysqli_fetch_array($result)) {
    $rows[] = $row;
}

print_r($rows);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...