Web Analytics Made Easy -
StatCounter Do I NEED a while loop for a mysql_fetch_assoc() ? - CodingForum

Announcement

Collapse
No announcement yet.

Do I NEED a while loop for a mysql_fetch_assoc() ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Do I NEED a while loop for a mysql_fetch_assoc() ?

    Usually I use a while loop when extracting data
    from a mysql query, but in this case I know that
    the array will only contain one line.

    See here:

    PHP Code:
    $sql " SELECT * FROM newsletters WHERE confirm = 'y' and status = 'c' ";
    $result mysql_query($sql)    or die("Failed query"mysql_error());  
    $num mysql_num_rows($result);
    if (
    $num == ) {
      
    $temp_body "There is no confirmed, current members newsletter !";
      
    $link1 "Error: No Confirmed, New Members Newsletter !";
      echo 
    "<div class=\"listerdiv\">
      <span class= \"lk\">
    $link1</span>
      <span class= \"by\">
    $temp_body</span>
      </div>"
    ;
      require_once(
    "footer.php"); 
      exit();
    }  
    // endif    

    if ($num ) {
        
    $temp_body "There are $num confirmed, current members newsletter !";
        
    $link1 "Error: Should only be one current newsletter !";
        echo 
    "<div class=\"listerdiv\">
        <span class= \"lk\">
    $link1</span>
        <span class= \"by\">
    $temp_body</span>
        </div>"
    ;
        require_once(
    "footer.php"); 
        exit();
    }  
    // endif 
    So now the array must only contain one row of data.
    Therefore instead of :
    PHP Code:
    while($row mysql_fetch_assoc($result)){
      
    extract($row);
      
    Blah blah
    }  // end while 
    Can I just use:


    PHP Code:
    $row mysql_fetch_assoc($result);
    extract($row);
    Blah blah 
    Is there any problem with doing this ?

    Thanks for any input
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    that line:
    PHP Code:
    while($row mysql_fetch_assoc($result)){ 
    do two things:
    1. assign the fetched values to the variable $row
    2. test if $row is true

    so you can get rid of while for 1. but must do something about 2. for example use a if statement.
    another thing is that you can be sure that everything go as you expect and you have one row, could be one or more and in this case what you intend to do will work since will get only first row but in the same time could be any row and will fail..

    best regards

    Comment


    • #3
      So I could write

      PHP Code:
      if($row mysql_fetch_assoc($result)){
        
      extract($row);
        
      Blah blah
      }  // end if 
      And in my case it would be OK because the
      if's prior to this test for num = 0 and for num > 1
      so there must only be one row ( as I see it )

      However an if statement is not much advantage
      over a while statement, so I may as well leave as it is

      Thanks for helping
      If you want to attract and keep more clients, then offer great customer support.

      Support-Focus.com. automates the process and gives you a trust seal to place on your website.
      I recommend that you at least take the 30 day free trial.

      Comment


      • #4
        look over mysql_result, maybe this will help you.

        http://www.php.net/manual/en/function.mysql-result.php

        in case it fail mysql_result return false and if you have a single field to get from mysql it's the best solution, in my opinion,

        best regards

        Comment


        • #5
          Originally posted by jeddi View Post
          So now the array must only contain one row of data
          How is it limited to one row? Is there only one that can be confirmed with a status of "c"(ie. you have logic for that elsewhere)?
          Originally posted by jeddi View Post
          Can I just use:


          PHP Code:
          $row mysql_fetch_assoc($result);
          extract($row);
          Blah blah 
          In a word, yes.
          Originally posted by jeddi View Post
          Is there any problem with doing this ?
          Nope! If you are only fetching one row you don't need the while() loop.
          Originally posted by jeddi View Post
          However an if statement is not much advantage
          over a while statement, so I may as well leave as it is
          You don't absolutely need the if statement there, but it wouldn't hurt anything either.

          The manual page for mysql_fetch_assoc() mentions that you don't need a loop for just one row in the example.

          Something else to note.. you mention that it will only return 1 row, but you only have if conditions to match 0 rows, or more than 1 row. oesxyl is correct in that if you only need one field mysql_result() is faster.
          Last edited by Inigoesdr; Apr 5, 2009, 04:30 AM.

          Comment

          Working...
          X