Jump to content

For Each Unexpected Result


MoFish
Go to solution Solved by Ch0cu3r,

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?

Edited by MoFish
Link to comment
Share on other sites

  • Solution

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.

Edited by Ch0cu3r
Link to comment
Share on other sites

Thank you objnoob. That has fixed the problem! Bravo!!

 

I have created a num_rows in my sql class as suggested - :)

function num_rows() { 
        $mysql_rows = mysql_num_rows( $this->sql_result ); 
        return $mysql_rows; 
} 
Edited by MoFish
Link to comment
Share on other sites

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

    */

    

 

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.