Jump to content

For Each Unexpected Result


MoFish

Recommended Posts

abjnoob i'm still not entirely sure what you have been reading, but from my understand we do not have a LIMIT 1 mentioned through-out the posts in the thread (hence our confusion). I also do not understand how the KISS suggestion helps me in getting the first result to display in my array lol.

 

Ch0cu3r I think your reading the correct thread. I tried your suggestion to use mysql_num_rows instead of fetch_array but this did not seem to help. Any other suggestions?

Right back on topic. Sorry MoFish for confusion.

 

 

 

Hi,

 

I updated the code as suggested - but it now returns no values.

 

Can you clarify if i got this correct?

 

Should the fetch_array still be in the while loop as I had previously?

    public function get_all_league()
    {
        $db = new sql();
        $sql = "SELECT * FROM $this->tablename";
        $db->query($sql);
        $data = $db->mysql_num_rows();
        $section_object = new section_object();
    
        if(!empty($data)){
            while($k = $db->fetch_array($db))
            {
                $total[] = $k;
            }
        }
        return $total;
    }

 

Not quite correct. Change it to

    public function get_all_league()
    {
        $db = new sql();
        $sql = "SELECT * FROM $this->tablename";
        $db->query($sql);
        $section_object = new section_object();
    
        if(mysql_num_rows()){
            while($k = $db->fetch_array($db))
            {
                $total[] = $k;
            }
        }
        return $total;
    }

However it would be better to add mysql_num_rows() to a method in your sql class instead, example

public function num_rows()
{
    return mysql_num_rows();
}

Instead of doing    if(mysql_num_rows()){ you'd use    if($db->num_rows()) instead.

Thank you for your help Ch0cu3r and objnoob.

 

You're welcome. Here's some other advice....  

  • Don't use  SELECT * to select all of the columns .  Specify the columns you're selecting.. be explicit and specific.

SELECT column1, column2, column3 FROM tblMyTable;

 

  • Don't duplicate code for a minor condition. one get leauge method that accepts the league id as 1 optional parameter is good. 

/**

*  returns a leauge with $id from $database .  if no $id is given, all leauges are returned

*/

function getLeauge($database, $id = null){

    if($id !== null && ! is_int($id)) throw new Exception('Don\'t be stupid.... validate, validate, validate. Make sure we got a integer datatype for $id'); 

    $sql = 'SELECT league.id, league.name, league.captain FROM league'. (($id)?' WHERE league.id=' .$id:null);

 

    /* finish your code here, but here's no need to fumble around with get_league, get_all_league, redundancy_code, redundancy_code_, redundancy_code__ 

       this function is nice because 1 is dual purpose, and 2 the database connection is injected which allows you test new database designs without changing this code.......

       also, if you decide to return the league division (just made this up) you can then add   league.division  to the $sql   here and not have to worry about get_league, get_all_league, redundancy_code, redundancy_code_,             redundancy_code__ 

       you could even get crazy and accept $id an array of integers that would return more than one league by IDs.

       live, learn, love life :)    don't forget to get rid of mysql_ and use mysqli_, or better yet PDO

    */

    

 

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.