SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Help Req]: MySQL Fetch Problem

    I have written a db function in my lib as

    Code:
    function RunSQL($sql)
    {
        define('BASEPATH','TEMP');
        include "../../../config/database.php";
    
        /* Connect to the database */
        $link = mysql_connect($db['dbname']['hostname'], $db['dbuser']['username'], $db['pwd']['password']);
        if (!$link) {
            die('Could not connect: ' . mysql_error());
        }
        mysql_select_db($db['db']['database']) or die(mysql_error());
    
        $result = mysql_query($sql);
        while($row = mysql_fetch_assoc($result)){
            $dataset[] = $row;
        }
    
        return $dataset;
    }
    Then in my class, I call this function and try to go through records as:


    Code:
            $rs_transactions = RunSQL($sql);
            print_r($rs_transactions);
    
            foreach($rs_transactions as $row)
            {
                echo "<br><br>...in loop....";
            }
    Problem is that print_r shows there r three records returned, but in foreach loop, it goes through only for first record. It doesn't loop for all three records.

    What could be wrong?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Show us the result of print_r($rs_transactions)

    If it is an array with 3 entries, then the foreach loop would print 3 times.

  3. #3
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it is working now. I was doing something wrong in the loop (a condition) that was causing it to break after one iteration.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •