Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts

    PHP doesn't return all rows, PHPMyAdmin does

    So I'm using a MySQL database to return data, and I have 10 rows in a table, 4 of which have a g_id = 1. The returned results then should be 4 when I select all of them where g_id = 1. And that's true, at least when using PHPMyAdmin. When I use my own code, I only get 1 row returned.

    Here's the code I'm using.
    PHP Code:
    function gTLL($_GID){
        
    $_GID $this->Secure($_GID);
        
    $_MySQL_Action_Check mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `gl` WHERE `g_id` = '".$_GID."'"));
        if(
    $_MySQL_Action_Check[0] > 0){
            
    $_MySQL_Action_Select mysql_query("SELECT * FROM `gl` WHERE `g_id` = '".$_GID."'");
            return 
    mysql_fetch_array($_MySQL_Action_Select);
        }
        else{
            return 
    false;
        }
    }

    $gLL gTTL(1);
    print_r($gLL); 
    Is there a problem with my PHP code?

  • #2
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    I found that using a while loop in the function and building an array is pretty much the only way I can do it. Is there a built in function that will just give me an array of all the rows?

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    there is, but not with mysql_* functions. either use MySQLi or PDO.
    PHP Code:
    // initialising PDO object ($pdo) …
    $ps $pdo->prepare("SELECT COUNT(*) FROM `gl` WHERE `g_id` = ?");
    $ps->bindValue(1$GID);
    $ps->execute();
    $res $ps->fetchAll(PDO::FETCH_ASSOC); 
    depending on what you want to do with the array afterwards, you don’t even have to use fetchAll():
    PHP Code:
    $ps->execute();
    foreach (
    $ps as $value)
    {
        echo 
    $value"<br>";

    Last edited by Dormilich; 11-25-2010 at 06:50 AM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    iLochie (11-26-2010)

  • #4
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Dormilich View Post
    there is, but not with mysql_* functions. either use MySQLi or PDO.
    PHP Code:
    // initialising PDO object ($pdo) …
    $ps $pdo->prepare("SELECT COUNT(*) FROM `gl` WHERE `g_id` = ?");
    $ps->bindValue(1$GID);
    $ps->execute();
    $res $ps->fetchAll(PDO::FETCH_ASSOC); 
    depending on what you want to do with the array afterwards, you don’t even have to use fetchAll():
    PHP Code:
    $ps->execute();
    foreach (
    $ps as $value)
    {
        echo 
    $value"<br>";

    I'd prefer not to use a library, I'll just run a while loop to create the array.

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    the mysql extension is a library as well … (mysqlnd for instance)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    iLochie (11-26-2010)

  • #6
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Dormilich View Post
    the mysql extension is a library as well … (mysqlnd for instance)
    Well, it's a built in library which I'm not sure is true about PDO. What troubles me is I'm sure I've been able to return more than one row at a time before using the same select query.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by iLochie View Post
    Well, it's a built in library which I'm not sure is true about PDO. What troubles me is I'm sure I've been able to return more than one row at a time before using the same select query.
    PDO and MySQLi are both bundled with PHP (guessing here... 5.1+) as well (exactly like MySQL, externally included).
    You can never retrieve more than one result with just the code you have above; the query itself could return zero or more records. Each call against this function would always produce the same results (for the same arguments), since each call re-queries for information. As with any selections from resource, you need to seek it yourself (mysql_fetch_assoc can do this simply by determining if its not false, which is why you often see a while ($row = mysql_fetch_assoc($query)) type call since the false is checked on the result of the assignment). MySQLi and PDO can both do this as well. Neither list the datatypes for themselves in the api, but I believe that both MySQLi and PDO objects are classified as Traversable objects, so these can both be used in a foreach loop.
    Use PDO for the broadest capabilities with the the fewest required changes to adapt to different datasources, and use MySQLi if you intend to only ever use a MySQL database.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    iLochie (11-26-2010)

  • #8
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Fou-Lu View Post
    PDO and MySQLi are both bundled with PHP (guessing here... 5.1+) as well (exactly like MySQL, externally included).
    You can never retrieve more than one result with just the code you have above; the query itself could return zero or more records. Each call against this function would always produce the same results (for the same arguments), since each call re-queries for information. As with any selections from resource, you need to seek it yourself (mysql_fetch_assoc can do this simply by determining if its not false, which is why you often see a while ($row = mysql_fetch_assoc($query)) type call since the false is checked on the result of the assignment). MySQLi and PDO can both do this as well. Neither list the datatypes for themselves in the api, but I believe that both MySQLi and PDO objects are classified as Traversable objects, so these can both be used in a foreach loop.
    Use PDO for the broadest capabilities with the the fewest required changes to adapt to different datasources, and use MySQLi if you intend to only ever use a MySQL database.
    Ok, thanks for the help both of you.

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    Quote Originally Posted by Fou-Lu View Post
    Neither list the datatypes for themselves in the api, but I believe that both MySQLi and PDO objects are classified as Traversable objects, so these can both be used in a foreach loop.
    PDO (cf. PDOStatement class synopsis) is traversable, MySQLi (resp. MySQLi_STMT/MySQLi_Result) not.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Dormilich View Post
    PDO (cf. PDOStatement class synopsis) is traversable, MySQLi (resp. MySQLi_STMT/MySQLi_Result) not.
    I missed the PDOStatement listing for the Traversable there, so I assumed it was just not listed for the internal ones (wouldn't be the first ).
    Looks like I knew this already though >.<:
    foreach vs mysql_fetch_array()
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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