Jump to content

db_query Only returns top record


NotSureILikePHP

Recommended Posts

There are 228 results in the database but it only returns the top result. When I echo the $sql statement being called and paste that into mysql I get all 228 records. Here's the example.

 

$sql='SELECT cust.id, cust.cust_name, cust.doman FROM cust_table';

return db_query($sql);

 

This is returned to another included page. However, I have taken that query and pasted it into sql and get all 228 results. If I do this...

 

 

$sql='SELECT cust.id, cust.cust_name, cust.doman FROM cust_table';

echo count(db_query($sql));

return db_query($sql);

 

 

I get 1 result. I'm stumped...

Link to comment
Share on other sites

Can you show us the db_query function? There's no such function built into PHP, so it's likely a custom function and probably isn't doing what you want it to, either because it's broken, or because you don't understand how to use it (which could easily still be the programmer's fault if (s)he didn't document it clearly)....

Link to comment
Share on other sites

That actually helps a lot and explains why I couldn't find much help on db_query. Here is the function

 

function db_query($query, $logError=true, $buffered=true) {
    global $ost, $__db;
 
    if ($__db->unbuffered_result) {
        $__db->unbuffered_result->free();
        $__db->unbuffered_result = false;
    }
 
    $tries = 3;
    do {
        $res = $__db->query($query,
            $buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);
        // Retry the query due to deadlock error (#1213)
        // TODO: Consider retry on #1205 (lock wait timeout exceeded)
        // TODO: Log warning
    } while (!$res && --$tries && $__db->errno == 1213);
 
    if(!$res && $logError && $ost) { //error reporting
        // Allow $logError() callback to determine if logging is necessary
        if (is_callable($logError) && !($logError($__db->errno)))
            return $res;
 
        $msg='['.$query.']'."\n\n".db_error();
        $ost->logDBError('DB Error #'.db_errno(), $msg);
        //echo $msg; #uncomment during debuging or dev.
    }
 
    if (is_object($res) && !$buffered)
        $__db->unbuffered_result = $res;
 
    return $res;
}
Link to comment
Share on other sites

Comments inline, after the "hashbang" (#!) marks ...

 

 

 

 
function db_query($query, $logError=true, $buffered=true) {

    global $ost, $__db; # do $ost and $__db both exist outside this function? (Note 2 underscores)
 
    if ($__db->unbuffered_result) { #! Great ... __db is an object. We may need to see it too...
        $__db->unbuffered_result->free();
        $__db->unbuffered_result = false;
    }
 
    $tries = 3;
    do {
        $res = $__db->query($query,
            $buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);
        // Retry the query due to deadlock error (#1213)
        // TODO: Consider retry on #1205 (lock wait timeout exceeded)
        // TODO: Log warning
    } while (!$res && --$tries && $__db->errno == 1213);
 
    if(!$res && $logError && $ost) { //error reporting
        // Allow $logError() callback to determine if logging is necessary
        if (is_callable($logError) && !($logError($__db->errno))) #! Might try added brackets &&
#! a "debug echo" statement here...
            return $res;
 
        $msg='['.$query.']'."\n\n".db_error(); #db_error is also a non-standard function.
        $ost->logDBError('DB Error #'.db_errno(), $msg);
        //echo $msg; #uncomment during debuging or dev. #! You should remove the two leading slashes
#! and see if anything useful is printed.
    }
 
    if (is_object($res) && !$buffered)
        $__db->unbuffered_result = $res;
 
    return $res;
}

 

Awful lot of custom, hidden code there ... we have no clue about the $ost object or the $__db object, which are in some other classes ... probably some other files in the application.

 

You might try doing this:

 

function db_query($query, $logError=true, $buffered=true) {

    global $ost, $__db;
 
    if ($__db->unbuffered_result) {
        $__db->unbuffered_result->free();
        $__db->unbuffered_result = false;
    }
 
    $tries = 3;
    do {
        $res = $__db->query($query,
            $buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);
            } while (!$res && --$tries && $__db->errno == 1213);

//DEBUG
echo "Tries is $tries<br>\n";
 
    if(!$res && $logError && $ost) { //error reporting
    if (is_callable($logError) && !($logError($__db->errno))) {
//DEBUG
echo "returning without valid result<br>\n";
            return $res;
  }
        $msg='['.$query.']'."\n\n".db_error();
        $ost->logDBError('DB Error #'.db_errno(), $msg);
        echo "LogDBError message: ".$msg."<br>\n"; #uncomment during debugging or dev.
    }
 
    if (is_object($res) && !$buffered) {
        $__db->unbuffered_result = $res;
  //DEBUG
echo "returning in bottom loop<br>\n";
    return $res;
} else {
die("We should not be here!");
}
}
Edited by dalecosp
Link to comment
Share on other sites

Yeah this is for OSTickets which is an open source ticketing system. They use a ton of code! I'm still new to PHP so trying to sort through all of it is rather intense. I believe this is the correct function for $__db->query(). I am still getting used to not always being able to right click and go to the line. I'm using eclipse for debugging but it really doesn't work most of the time. 

 

 public function query($name, $type = 'A', $class = 'IN')
    {
        //
        // make sure we have some name servers set
        //
        $this->checkServers(Net_DNS2::RESOLV_CONF);
 
        //
        // we dont' support incremental zone tranfers; so if it's requested, a full
        // zone transfer can be returned
        //
        if ($type == 'IXFR') {
 
            $type = 'AXFR';
        }
 
        //
        // if the name *looks* too short, then append the domain from the config
        //
        if ( (strpos($name, '.') === false) && ($type != 'PTR') ) {
 
            $name .= '.' . strtolower($this->domain);
        }
 
        //
        // create a new packet based on the input
        //
        $packet = new Net_DNS2_Packet_Request($name, $type, $class);
 
        //
        // check for an authentication method; either TSIG or SIG
        //
        if (   ($this->auth_signature instanceof Net_DNS2_RR_TSIG)
            || ($this->auth_signature instanceof Net_DNS2_RR_SIG)
        ) {
            $packet->additional[]       = $this->auth_signature;
            $packet->header->arcount    = count($packet->additional);
        }
 
        //
        // check for the DNSSEC flag, and if it's true, then add an OPT
        // RR to the additional section, and set the DO flag to 1.
        //
        if ($this->dnssec == true) {
 
            //
            // create a new OPT RR
            //
            $opt = new Net_DNS2_RR_OPT();
 
            //
            // set the DO flag, and the other values
            //
            $opt->do = 1;
            $opt->class = $this->dnssec_payload_size;
 
            //
            // add the RR to the additional section.
            //
            $packet->additional[] = $opt;
            $packet->header->arcount = count($packet->additional);
        }
 
        //
        // set the DNSSEC AD or CD bits
        //
        if ($this->dnssec_ad_flag == true) {
 
            $packet->header->ad = 1;
        }
        if ($this->dnssec_cd_flag == true) {
 
            $packet->header->cd = 1;
        }
 
        //
        // if caching is turned on, then check then hash the question, and
        // do a cache lookup.
        //
        // don't use the cache for zone transfers
        //
        $packet_hash = '';
 
        if ( ($this->use_cache == true) && ($this->cacheable($type) == true) ) {
 
            //
            // open the cache
            //
            $this->cache->open(
                $this->cache_file, $this->cache_size, $this->cache_serializer
            );
 
            //
            // build the key and check for it in the cache.
            //
            $packet_hash = md5(
                $packet->question[0]->qname . '|' . $packet->question[0]->qtype
            );
 
            if ($this->cache->has($packet_hash)) {
 
                return $this->cache->get($packet_hash);
            }
        }
 
        //
        // set the RD (recursion desired) bit to 1 / 0 depending on the config
        // setting.
        //
        if ($this->recurse == false) {
            $packet->header->rd = 0;
        } else {
            $packet->header->rd = 1;
        }
 
        //
        // send the packet and get back the response
        //
        // *always* use TCP for zone transfers- does this cause any problems?
        //
        $response = $this->sendPacket(
            $packet, ($type == 'AXFR') ? true : $this->use_tcp
        );
 
        //
        // if strict mode is enabled, then make sure that the name that was
        // looked up is *actually* in the response object.
        //
        // only do this is strict_query_mode is turned on, AND we've received
        // some answers; no point doing any else if there were no answers.
        //
        if ( ($this->strict_query_mode == true) 
            && ($response->header->ancount > 0) 
        ) {
 
            $found = false;
 
            //
            // look for the requested name/type/class
            //
            foreach ($response->answer as $index => $object) {
 
                if ( (strcasecmp($object->name, $name) == 0)
                    && ($object->type == $type)
                    && ($object->class == $class)
                ) {
                    $found = true;
                    break;
                }
            }
 
            //
            // if it's not found, then unset the answer section; it's not correct to
            // throw an exception here; if the hostname didn't exist, then 
            // sendPacket() would have already thrown an NXDOMAIN error- so the host 
            // *exists*, but just not the request type/class.
            //
            // the correct response in this case, is an empty answer section; the
            // authority section may still have usual information, like a SOA record.
            //
            if ($found == false) {
                
                $response->answer = array();
                $response->header->ancount = 0;
            }
        }
 
        //
        // cache the response object
        //
        if ( ($this->use_cache == true) && ($this->cacheable($type) == true) ) {
 
            $this->cache->put($packet_hash, $response);
        }
 
        return $response;
    }
Link to comment
Share on other sites

^^^ that's not it.

 

based on this - $res = $__db->query($query, $buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);, the code is using the mysqli database extension and the query method is the mysqli->query() method.

 

the value being returned is a mysqli result object. using count() on it isn't relevant to the number of rows in the result set (the ->num_rows property would be.) you need to loop over the result set in order to process all the rows. what is your code that's trying to process the result set?

Edited by mac_gyver
Link to comment
Share on other sites

That looks like it's the problem. I was trying to use their code but it's so convoluded that I can't even figure out where I should put the query so I hard coded it into the get customers. However, I was still using their customer object that wasn't defined in my code. I got it working now.

 

Thank you!

 

I'm a .Net programmer and everything is much more simplistic with software that you actually paid for.

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.