Could you give me a little code example of how to use if with functions? I haven't really got an idea where to start.

I tried getting rid of the function and including the code directly within the page where I want the code to execute but I still get the "Call to a member function query() on a non-object" error.

    Well, I do not know what you are working with, so I cannot provide any code examples.

      Id'e have to agree. Show us what your include looks like.

        This is the mysql include file:

        <?php
        $db = New DB_MySQL;
        
        // DB Details:
        $db->database = 'pg_content';
        $db->user = 'xxxx';
        $db->password = 'xxxx';
        $db->server = 'localhost';
        
        // Connect to db
        $db->connect();
        
        //////////////////////////////////////////////////////////////////////
        
        function ss($str) {
            return mysql_escape_string($str);
        }
        
        // Mysql Database Class
        class DB_MySQL {
          var $database = "";
        
          var $link_id  = 0;
          var $query_id = 0;
          var $record   = array();
        
          var $server = "";
          var $user = "";
          var $password = "";
        
          function connect() {
            global $usepconnect;
            // connect to db server
        
        if ( 0 == $this->link_id ) {
          if ($this->password=="") {
            if ($usepconnect==1) {
              $this->link_id=mysql_pconnect($this->server,$this->user);
            } else {
              $this->link_id=mysql_connect($this->server,$this->user);
            }
          } else {
            if ($usepconnect==1) {
              $this->link_id=mysql_pconnect($this->server,$this->user,$this->password);
            } else {
              $this->link_id=mysql_connect($this->server,$this->user,$this->password);
            }
          }
          if (!$this->link_id) {
            $this->halt("Link-ID == false, connect failed");
          }
          if ($this->database!="") {
            if(!mysql_select_db($this->database, $this->link_id)) {
              $this->halt("cannot use database ".$this->database);
            }
          }
        }
          }
        
          function geterrdesc() {
            $this->error=mysql_error();
            return $this->error;
          }
        
          function geterrno() {
            $this->errno=mysql_errno();
            return $this->errno;
          }
        
          function select_db($database="") {
            // select database
            if ($database!="") {
              $this->database=$database;
            }
        
        if(!mysql_select_db($this->database, $this->link_id)) {
          $this->halt("cannot use database ".$this->database);
        }
        
          }
        
          function query($query_string) {
            global $query_count,$showqueries,$explain,$querytime;
            // do query
        
        if ($showqueries) {
          echo "Query: $query_string\n";
        
          global $pagestarttime;
          $pageendtime=microtime();
          $starttime=explode(" ",$pagestarttime);
          $endtime=explode(" ",$pageendtime);
        
          $beforetime=$endtime[0]-$starttime[0]+$endtime[1]-$starttime[1];
        
          echo "Time before: $beforetime\n";
        }
        
        $this->query_id = mysql_query($query_string,$this->link_id);
        if (!$this->query_id) {
          $this->halt("Invalid SQL: ".$query_string);
        }
        
        $query_count++;
        
        if ($showqueries) {
          $pageendtime=microtime();
          $starttime=explode(" ",$pagestarttime);
          $endtime=explode(" ",$pageendtime);
        
          $aftertime=$endtime[0]-$starttime[0]+$endtime[1]-$starttime[1];
          $querytime+=$aftertime-$beforetime;
        
          echo "Time after:  $aftertime\n";
        
          if ($explain and substr(trim(strtoupper($query_string)),0,6)=="SELECT") {
            $explain_id = mysql_query("EXPLAIN $query_string",$this->link_id);
            echo "</pre>\n";
            echo "
            <table width=100% border=1 cellpadding=2 cellspacing=1>
            <tr>
              <td><b>table</b></td>
              <td><b>type</b></td>
              <td><b>possible_keys</b></td>
              <td><b>key</b></td>
              <td><b>key_len</b></td>
              <td><b>ref</b></td>
              <td><b>rows</b></td>
              <td><b>Extra</b></td>
            </tr>\n";
            while($array=mysql_fetch_array($explain_id)) {
              echo "
              <tr>
                <td>$array[table]&nbsp;</td>
                <td>$array[type]&nbsp;</td>
                <td>$array[possible_keys]&nbsp;</td>
                <td>$array[key]&nbsp;</td>
                <td>$array[key_len]&nbsp;</td>
                <td>$array[ref]&nbsp;</td>
                <td>$array[rows]&nbsp;</td>
                <td>$array[Extra]&nbsp;</td>
              </tr>\n";
            }
            echo "</table>\n<BR><hr>\n";
            echo "\n<pre>";
          } else {
            echo "\n<hr>\n\n";
          }
        }
        
        return $this->query_id;
          }
        
          function fetch_array($query_id=-1,$query_string="") {
            // retrieve row
            if ($query_id!=-1) {
              $this->query_id=$query_id;
            }
            if ( isset($this->query_id) ) {
              $this->record = mysql_fetch_array($this->query_id);
            } else {
              if ( !empty($query_string) ) {
                $this->halt("Invalid query id (".$this->query_id.") on this query: $query_string");
              } else {
                $this->halt("Invalid query id ".$this->query_id." specified");
              }
            }
        
        return $this->record;
          }
        
          function free_result($query_id=-1) {
            // retrieve row
            if ($query_id!=-1) {
              $this->query_id=$query_id;
            }
            return @mysql_free_result($this->query_id);
          }
        
          function query_first($query_string) {
            // does a query and returns first row
            $query_id = $this->query($query_string);
            $returnarray=$this->fetch_array($query_id, $query_string);
            $this->free_result($query_id);
            return $returnarray;
          }
        
          function data_seek($pos,$query_id=-1) {
            // goes to row $pos
            if ($query_id!=-1) {
              $this->query_id=$query_id;
            }
            return mysql_data_seek($this->query_id, $pos);
          }
        
          function num_rows($query_id=-1) {
            // returns number of rows in query
            if ($query_id!=-1) {
              $this->query_id=$query_id;
            }
            return mysql_num_rows($this->query_id);
          }
        
          function num_fields($query_id=-1) {
            // returns number of fields in query
            if ($query_id!=-1) {
              $this->query_id=$query_id;
            }
            return mysql_num_fields($this->query_id);
          }
        
        function sql_query($query) {
            $result = $this->query($query);
            $return_array = array();
        
            while ($row = $this->fetch_array($result)) {
                array_push($return_array, $row);
            }
        
            $this->free_result($result);
        
            return $return_array; 
        }
        
        function halt($msg) { die($msg); }
        function insert_id() { return mysql_insert_id($this->link_id); }
        function close() { return mysql_close(); }
        }
        ?>

        I'm including the file with the standard "include ('inc/mysql.php');" within a file 'review.php' which needs to call the data from the database to display a review based on the id sent to the database. The function is within review.php not mysql.php

        The code I'm using within the function is:

        <?
        function showreview($rid, $start) {
          $result = $db->query("SELECT *, DATE_FORMAT(submitted,'%D %b %Y %H:%i') AS submitted2 FROM reviews WHERE id = '$rid'");
          if ($myrow = @mysql_fetch_array($result)) { do {
              printf("<b class=\"title\">%s<b /> Posted: %s<br />\n", $myrow["title"], $myrow["submitted2"]);
              printf("%s", nl2br($myrow["review"]));
          } while ($myrow = @mysql_fetch_array($result)); } 
        } ?>
        

          The quick fix is to declare:

          global $db;

          At the start of your showreview() function.

            It's still giving the "Call to a member function query() on a non-object" error when using

            global $db;

              Show us how your using it. Also.. why, within showreview() do you reevert back to using mysql_fetch_array() when you have an equivelent within your object. Stick to one or the other.

                This new mysql file is one that has been passed on to me, I've been converting my previous code to fit with this as IMO it is easier/more efficient.

                laserlight - DB_MySQL is the class defined in the mysql.php file. I'm not too clear on the ins and outs of the file as it was just passed on to me. However when I was typing out the code every time before I would have to put this at the top of every function (where config.inc.php contained the db name, username, password and server)

                require('admin/config.inc.php');
                $db = mysql_connect("$dbhost", "$dbuser", "$dbpass");
                mysql_select_db("$dbname",$db);

                So I would have to re-define all the db details in every function and I'd prefer not to do this if at all possible.

                  Show us the code giving you the error!

                  PS: By the way, just adding this..

                  $db = mysql_connect("$dbhost", "$dbuser", "$dbpass");
                  mysql_select_db("$dbname",$db);
                  

                  to your config.php file would have been much easier than using that bloated class you have now. Objects need to be passed around while a connection resource as $db is above would have just been available to all script the config.php file was included in.

                    <?
                      session_start();
                      $url = $_SERVER['REQUEST_URI'];
                      include ('inc/mysql.php');
                      require ('inc/globals.inc.php');
                      include ("updateonline.php");
                    ?>
                    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                    <html>
                    <head>
                    <title>PALga - User Reviews</title>
                    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                    <link href="../styles.css" rel="stylesheet" type="text/css">
                    <link href="fstyles.css" rel="stylesheet" type="text/css">
                    </head>
                    <body bgcolor="#DFDFDF" text="#FFFFFF" topmargin=0 style="text-align:center;">
                    
                    <div class="comps">
                        <div style="background:url(../img/logo-bg.gif);">
                                <iframe src="http://impgb.tradedoubler.com/imp/pool/iframe/61221/892274" width="468" height="60" frameborder="0" border="0" scrolling="no" style="float:right; margin: 5px 6px 0px 0px;"></iframe>
                                <img src="img/logo.gif" width="290" height="71">
                        </div>
                    
                    <div class="verdana10px" id="pad-15">
                    <?
                        include('topbar.php');
                        if(!$_SESSION['user']) { echo $regmsg; }
                    ?>
                    
                    <?
                        if($rid) {
                              showreview($rid, $start);
                        } else {
                              listproducts();
                        }
                    ?>
                    
                     <div class="footer">
                         <? include('footer.php'); ?>
                     </div>
                    
                    </body>
                    </html>
                    
                    <?
                    
                    function showreview($rid, $start) {
                      $result = $db->query("SELECT *, DATE_FORMAT(submitted,'%D %b %Y %H:%i') AS submitted2 FROM reviews WHERE id = '$rid'");
                      if ($myrow = @mysql_fetch_array($result)) { do {
                          printf("<b class=\"title\">%s<b /> Posted: %s<br />\n", $myrow["title"], $myrow["submitted2"]);
                          printf("%s", nl2br($myrow["review"]));
                      } while ($myrow = @mysql_fetch_array($result)); } 
                    }
                    
                    function listproducts() {
                      $result = $db->query("SELECT * FROM products ORDER BY name ASC LIMIT 50");
                      if ($myrow = @mysql_fetch_array($result)) { do {
                          printf("<a href=\"review.php?rid=%s\">%s</a><br />\n", $myrow["id"], $myrow["title"]);
                      } while ($myrow = @mysql_fetch_array($result)); } 
                    }
                    
                    ?>
                      thorpe wrote:

                      Show us the code giving you the error!

                      PS: By the way, just adding this..

                      $db = mysql_connect("$dbhost", "$dbuser", "$dbpass");
                      mysql_select_db("$dbname",$db);
                      

                      to your config.php file would have been much easier than using that bloated class you have now. Objects need to be passed around while a connection resource as $db is above would have just been available to all script the config.php file was included in.

                      I tried that but the functions still needed it re-typed as I wasn't sure of another way to include the content of config.inc.php for the function to use.

                      I would consider myself a newbie to php so I'm still learning how to do a fair bit of stuff. I was hoping the mysql file passed on to me would help me streamline my code a bit.

                        As laserlight has suggested you need to use global $db in your functions. You have not. eg;

                        include ('inc/mysql.php');
                        function showreview($rid, $start) {
                          global $db;
                          // query and code here.
                        }
                        

                        However... that is not your only issue here. showreview() is completely broken, you need to read up on using a while().

                          I tried that but it didn't seem to work - perhaps it's my code.

                          Thank you both very much for your help, it's been much appreciated. I'll keep at it and see if I can sort out the problem.

                          Cheers.

                            Write a Reply...