0

I'm not very good yet with sessions or arrays.

Here is what I have so far but I am getting an error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.

Here is my code:

$checkgroupadmin = $this->db->query("SELECT group_id FROM groupadmin 
  WHERE contact_id = $contactid");
while ($adminrow = mysql_fetch_array($checkgroupadmin, MYSQL_ASSOC)) {
  $_SESSION[group_admin] = array('group'=>$adminrow['group_id']);
}

Any help would be appreciated. :)

4
  • are you using a php framework like codeigniter? If so you should be using the result generation functions. Commented Oct 29, 2012 at 18:09
  • Add var_dump($checkgroupadmin) after the query and show the result Commented Oct 29, 2012 at 18:09
  • The query has failed. Always check the result if (!$checkgroupadmin) echo mysql_error(); Commented Oct 29, 2012 at 18:09
  • 1
    Are you using any framework ? Why $this->db->query to query and mysql_fetch_array to fetch ? Commented Oct 29, 2012 at 18:10

2 Answers 2

1

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.

You are getting your error because $checkgroupadmin is not an actual query resource that mysql_fetch_array() requires.

$checkgroupadmin = $this->db->query("SELECT group_id FROM groupadmin WHERE contact_id = $contactid");`

In other words the above object oriented call is not returning a mysql resource query.

Why are you mixing object oriented style with procedural style anyways. Secondly the above code $this>db->query should be happening inside a Class' method

Sign up to request clarification or add additional context in comments.

Comments

0

mysql_fetch_array() returns an associative array of the current results, but in your code you would just be overwriting the same value in the $_SESSION for each result. You likely need to create your own array of resulting IDs and then assign that to a $_SESSION variable.

// query database
$checkgroupadmin = $this->db->query("SELECT group_id FROM groupadmin WHERE contact_id = $contactid");
// array to hold each group_id
$result_array = array();
// varify that $checkgroupadmin returned results 
if ($checkgroupadmin){
    // get results into associative array
    while ($adminrow = mysql_fetch_array($checkgroupadmin, MYSQL_ASSOC)) {
        // put the group id into your results array
        $result_array[] = $adminrow['group_id'];
    }
}
// set results array to the 'group_admin' key in $_SESSION
$_SESSION['group_admin'] = $result_array;

Please note that the mysql_* extensions have been deprecated and you should be using mysqli or PDO instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.