0

ok i have this and it doesn't show all the rows when fetched from mysql database its like this:

  mysql_select_db($database_config, $config);
    $query_cat = "SELECT * FROM cat";
    $cat = mysql_query($query_cat, $config) or die(mysql_error());
    $row_cat = mysql_fetch_array($cat);

    $arr = array("TIT" => $row_cat['title'],
            "DES" => $row_cat['description']);


    foreach($arr as $ar){
    echo $ar;
    }

now it only displays the first row and then stops why is it not displaying all the fields and i don't wanna use while loop for it can anyone explain me the problem??

EDIT: Well basically i want to work it like this

$p = "{TIT}{DES}";
foreach($arr as $k => $p){
$p = str_replace("{".$k."}", $v, $p);
echo $p;
}

Now the problem is not with str_replace its with the loop or database because the database rows are not incrementing and displays only one data.

2
  • mysql_fetch_array only fetches one row (php.net/mysql_fetch_array) Commented May 2, 2011 at 16:30
  • i have re edited the question please take a look... Commented May 2, 2011 at 16:53

2 Answers 2

4

This will always return the first row. you are fetching only once that will return only first row.

Instead you must fetch all the rows using fetch statement in loop

 while($row_cat = mysql_fetch_array($cat)){
       echo $row_cat['title'];
       echo $row_cat['description'];
    }
Sign up to request clarification or add additional context in comments.

Comments

0

From PHP mysq_fetch_array documentation:

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

As far as I know, you cannot retrieve every row without using a loop. You should do something similar to this:

while($row_cat = mysql_fetch_array($cat)){
  echo $row_cat['title'];
  echo $row_cat['description'];
}

Comments

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.