Jump to content

For Each Unexpected Result


Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi,

 

I have a foreach running on the results for SQL query, but only get two results appearing.

 

I am expecting three to be displayed.

 

Can anyone see any major problem with this?

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

Thanks,

 

MoFish

Edited by MoFish
Link to comment
https://forums.phpfreaks.com/topic/284200-for-each-unexpected-result/
Share on other sites

What are you expecting? 

 

What foreach?

 

Appologies,

 

It is a while statement:

while($k = $db->fetch_array($db))

There are three entries in the database and it is only returning two in the print_r statement.

 

Do I need to maybe add the values of $data into the total[] array I am returning somehow?

 

Thanks

 

MoFish

Edited by MoFish

This is because you call $db->fetch_array(); before the while loop. Each time you call this it'll return a row from the query.

 

Basically yourare losing a result for checking if the query returned anything. What you should be doing is use mysqli_num_rows (<-- mysqli) or mysql_num_rows (mysql)  to check for this.

Edited by Ch0cu3r

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;
    }
Edited by MoFish

You're looping through each item in the $data array. The number of items in the $data array depends on the fetch mode you're using.

Since you're not explicitly specifying a fetch mode (fetch num, fetch both, fetch assoc, etc) you will have to assume the default fetch mode is being used.

 

The default fetch mode, assuming you're using fetch_array() method of a mysqli_result object, is both. This will represent each column of a mysqli_result row twice; once by column index and once by column name.

Since each column is represented twice, any and all row arrays (fetch_array) fetched will have an even number of items. x columns multiplied by 2 representations will always total an even number of representations (2, 4, 6, 8). So, expecting 3 is just not possible.

 

Now that we've covered your fetch both mode paradox, there are other inhibitions that I spot in your code.

 

Caliing $db->query($sql); should, if it doesn't, result with a return value that is not being stored. You should design the query() method of your sql class to return a value that represents the query's result!

          $the_returned_result  = $db->query($sql);      // query the database and set the variable $the_returned_result to the returned value of sql::query()

          $data = $the_returned_result->fetch_array(); // fetch from the returned value

This is because you call $db->fetch_array(); before the while loop. Each time you call this it'll return a row from the query.

 

Basically yourare losing a result for checking if the query returned anything. What you should be doing is use mysqli_num_rows (<-- mysqli) or mysql_num_rows (mysql)  to check for this.

 

No need, since the query has a LIMIT clause.

    function query($sql_query)
    {
        
        $this->sql_query=$sql_query;
        $this->sql_result=mysql_query($this->sql_query);
        $this->error=mysql_error();
        if (!$this->sql_result)
        {
            echo "<b>" . $sql_query . "</b><br><br>";
            $this->error=mysql_error();
            echo $this->error;
        }
        echo mysql_error();
    }

is my query function objnoob

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

Updated code. No Joy

Yes, it would be wise.  However, you are storing the result in $this->sql_result.

So, you COULD just $data = $db->sql_result->fetch_array();   I don't recommend this because it limits you to 1 sql_result, and you will not be able to use sql::query() again while relying on sql::sql_result.

Yes, it would be wise.  However, you are storing the result in $this->sql_result.

So, you COULD just $data = $db->sql_result->fetch_array();   I don't recommend this because it limits you to 1 sql_result, and you will not be able to use sql::query() again while relying on sql::sql_result.

That would be the case if MoFish is using mysqli object.

 

The mysql_*() is procedural code only

The limit is here: 

 

 $sql = "SELECT * FROM $this->tablename WHERE `staff_id`='".clean_db($id)."' LIMIT 1";

 

This means sql::sql_result would be no more than 1 row ALWAYS!  So, checking the number of rows is moot.   Fetch once.... if the fetch is false on the first fetch, you've got 0 rows, otherwise 1. Second fetch is not necessary, but may require a free result or close cursor to free up the database's result buffer.

Yeah, I see.  He's knee deep without the basics.  I'm sure it's a copy paste snippet here and snippet there collection of code statements in an attempt.

 

Aww, I thought I had done rather well to get the results coming out from the database - I just couldn't figure out why I was missing one. So what exactly am I missing? Have i been lead astray during the cross wires of posts somehow? lol

Edited by MoFish

Here's the original...

 

Hi,

 

I have a foreach running on the results for SQL query, but only get two results appearing.

 

I am expecting three to be displayed.

 

Can anyone see any major problem with this?

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

Thanks,

 

MoFish

what the heck, lol. i'm not crazy......  this is copy + paste:  

 

Hi,

 

I have a foreach running on the results for SQL query, but only get two results appearing.

 

I am expecting three to be displayed.

 

Can anyone see any major problem with this?

public function get_league($id)
{
$db = new sql();
$sql = "SELECT * FROM $this->tablename WHERE `staff_id`='".clean_db($id)."' LIMIT 1";
$db->query($sql);
$data = $db->fetch_array();
$section_object = new section_object();
foreach($data as $k => $v)
{
if(!is_numeric($k))
$section_object->$k = $v;
}
return $section_object;
}

Thanks,

 

MoFish

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.