Jump to content

"Error: Offset 7" using function and list()


tork

Recommended Posts

I'm trying to get the data, which is successfully read from the db table accessed inside the function, to be transferred via the list() below. However, I get the offset 7 error. Why is this?

Ideally, I'd like to not use list() but simply echo the $data elements. Any ideas?

 

An error occurred in script 'blah..blah' on line 203: Undefined offset: 7
Date/Time: 10-23-2013 21:16:26

 

 

$data = array();

 

function read_session($sid) {
    global $dbc;
    $q = "SELECT * FROM nm_session";
    $r = mysqli_query($dbc, $q);
    if (mysqli_num_rows($r) == 1) {
        $data = mysqli_fetch_assoc($r);
        return $data;
    } else {
        return 'return no data from read_session<br />';
    }
}
 

list($data['id'], $data['user_id'], $data['last_accessed'], $data['first_name'],
$data['user_level'], $data['paused'], $data['changed_pw'], $data['tests']) = read_session($sid); // This is line 203
 

echo $data['id']."<br />";
echo $data['user_id']."<br />";
echo $data['last_accessed']."<br />";
echo $data['first_name']."<br />";
echo $data['user_level']."<br />";
echo $data['paused']."<br />";
echo $data['changed_pw']."<br />";
echo $data['tests']."<br />";
 

 

 

Link to comment
https://forums.phpfreaks.com/topic/283223-error-offset-7-using-function-and-list/
Share on other sites

Use mysqli_fetch_row rather than mysqli_fetch_assoc. list requires that the array be numerically indexed rather than associative to work properly.

 

Alternativly you could just sip the list stuff all together and just stick with mysqli_fetch_assoc. Just assign the result of the function to $data rather than listing out each member.

Thanks Guru. Works perfectly.

 

 

$data = array();

 

function read_session($sid) {
    global $dbc;
     $q = "SELECT * FROM nm_session";
    $r = mysqli_query($dbc, $q);
    if (mysqli_num_rows($r) == 1) {
        $data = mysqli_fetch_assoc($r);
        return $data;
    } else {
        return 'return no data from read_session<br />';
    }
}
 

$data = read_session($sid);

echo $data['id']."<br />";                                                          
echo $data['user_id']."<br />";
echo $data['last_accessed']."<br />";
echo $data['first_name']."<br />";
echo $data['user_level']."<br />";
echo $data['paused']."<br />";
echo $data['changed_pw']."<br />";
echo $data['tests']."<br />";
 

I've used it with explode a few times when parsing things, but that's about it.

list($key,$value) = explode("\t", $dataLine, 2);

 

Yeah but, what's the advantage of doing that over just doing

 

$dataLine = explode("\t", $dataLine, 2);
// do something with $dataLine[0] and $dataLine[1]
I can think of several times to "use" it.. what I fail to come up with is a compelling reason to use it, as in, an instance where it's more efficient than any other way or something list can be used for that no other thing can do, etc.

 

For example.. it might be "slightly better" if instead of having to supply an argument for every variable.. if you could pass it an array of values. Example:

 

$vars = array('var1','var2','var3','var4','var5');
list($vars) = explode("\t",$dataLine,5);
But you can't do that. And I say "slightly better" because again.. what's a real advantage for even doing this? Overall I'm kinda thinking that list() actually works against elegant coding. At best (e.g. your example) it's an unnecessary extra step or waste of variable namespace. At worst.. it actually goes against all the benefits of having stuff in an array to begin with. And thing is, I usually always see it being used in a context similar or exactly the same as OP: separating grouped data. Well it's almost certain that that data relates to each other on some level, so even if you have to act on individual elements, overall that data is more than likely gonna stay "grouped" 99% of the time.

I've used it with explode a few times when parsing things, but that's about it.

list($key,$value) = explode("\t", $dataLine, 2);

 

Yeah but, what's the advantage of doing that over just doing

Self-documenting code.

 

list($key,$value) = explode("\t", $dataLine, 2);

// more code here ...

$dataOut[$key] = new clsSomething($value);
is easier to read and understand (by a human) than

$data = explode("\t", $dataLine, 2);

// more code here ...

$dataOut[$data[0]] = new clsSomething($data[1]);
particularly if $dataLine has more than 2 elements, and you are manipulating those elements in some way in the // more code here ... section.

 

I think this is the only time I ever use it.

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.