Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Advice on DB Abstraction Class - Conversion to MySQLi Functions (PHP5)

    Okay, a while back I wrote a database abstraction class which allows me to use a universal set of functions while I could change however it handles it in the class. When a query was executed, it'd use the query's return reference as the key in an associative array for working easily with result sets.

    This works fine with the MySQL extension, but after upgrading to PHP5 and attempting to use MySQLi (MySQL Improved extension), it returns an object. An object does not like to be used as the key in an array. Thus, I posted here for any advice you may have. Would it be possible to salvage this method? Or should I begin thinking about other ways to solve this problem?

    Example function from my class:
    PHP Code:
        public function sql_fetchrow($query_id 0) {
            if (!
    $query_id) {
                
    // $query_id is the value of the return of a mysqli_query()
                
    $query_id $this->query_result;
            }

            if (
    $query_id) {
                
    /* This was where I'd use the reference to fetch the row.
                It retrieves the data correctly as I pass the query result outside the function.
                However with MySQLi won't set $this->row[$query_id] as it returns an object now. */
                
    $this->row[$query_id] = mysqli_fetch_assoc($query_id)
                return 
    $this->row[$query_id];
            } else {
                return 
    FALSE;
            }

    Last edited by Velox Letum; 01-10-2006 at 01:27 AM.
    "$question = ( to() ) ? be() : ~be();"

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    I think the confusion stems from mixing the procedural and OO styles.. stick with OO and (I think) you can do:

    PHP Code:
    foreach ($query_id->fetch_assoc() as $key->$value)
    {
        
    $this->row[$key] = $value;

    Sorry I can't test this right now, hope it at least partially works.

    edit: assuming $this->query_result or the passed var is equal to $yourmysqliobject->query('foo'), either of which becomes $query_id
    Last edited by ralph l mayo; 01-10-2006 at 01:45 AM.

  • #3
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I could return it like that, but that kind of reduces the portability I enjoyed before. I could pass the return reference of a query to custom functions so I could manipulate the data sets stored in the rows easily. Such as to get a single field, I could pass the query reference to a fetch field function which would execute mysql_result if the results didn't exist, or if they did return $rowset[$query_id][$field]. I'm simply looking to keep my functionality. Its looking to me like I'll somehow have to pass a custom reference for the queries.
    "$question = ( to() ) ? be() : ~be();"

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    What did $query_id used to be? Isn't it arbitrary? Could you just keep a counter?
    $this->resultarray[++$this->query_id] = $this->query_result->fetch_array();

  • #5
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm, right you are. I keep a query count anyways, so I used that. Thanks!
    "$question = ( to() ) ? be() : ~be();"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •