1

I have somewhat of a problem, using the following code:

$getTS = mysql_query("SELECT * FROM book WHERE chapter='$url' && verse='$verse'");      

When I use

mysql_num_rows($getTS)

it returns 2, 3, etc (which is perfect), however, when I am printing it out I only one get row using the following code:

$rowTS = mysql_fetch_array($getTS);
$summary = $rowTS['text'];
echo $summary;

Any help would be greatly appreciated.

sliceruk
  • 127
  • 1
  • 3
  • 9

3 Answers3

6

You have to do this in a loop

while($rowTS = mysql_fetch_array($getTS)) { 
   $summary = $rowTS['text']; 
   echo $summary; 
}
shankhan
  • 6,343
  • 2
  • 19
  • 22
2

You need iterate loop when you are using mysql_fetch_array()

From documentation

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

$getTS = mysql_query("SELECT * FROM book WHERE chapter='$url' && verse='$verse'");      

while ($row = mysql_fetch_array($getTS , MYSQL_NUM)) {
       $summary = $row['text']; 
       echo $summary;
}
Moyed Ansari
  • 8,436
  • 2
  • 36
  • 57
  • This does not work. If you fetch with numerical indexes, you can't access them by string keys. – hakre Jan 01 '13 at 10:51
2

You can do that by using a so called iterator. The old mysql_* functions do not provide it, but it can be easily written (or re-used, see this earlier answer: PHP file cannot enter some part of code):

$getTS = mysql_query("SELECT * FROM book WHERE chapter='$url' && verse='$verse'");
$rows  = new MySqlResult($getTS);
foreach ($rows as $row)
{
    $summary = $row['text'];
    echo $summary;
}

You can further improve it by using a database object directly, e.g. for the query:

$db    = new MySql();
...
$rows  = $db->query("SELECT * FROM book WHERE chapter='$url' && verse='$verse'");
foreach ($rows as $row)
{
    $summary = $row['text'];
    echo $summary;
}

The next step then would be probably that you use PDO because mysql_* is deprecated and PDO comes with such kind of a functionality out of the box.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836