• PHP Help PHP Newbies
  • Newbies questions: Warning: Missing argument 1 for Db_conn::__construct(), called in.

Get warnings on top of page, if you can help, please give an instruction what to do exactly.

Warning: Missing argument 1 for Db_conn::__construct(), called in /homepages/4/d251536907/htdocs/admin/include/ready_post_guide.php on line 12 and defined in /homepages/4/d251536907/htdocs/admin/include/db_conn.php on line 18

    It means that the constructor for your Db_conn object requires at least 1 argument (parameter), but you have not given any. Then it tells you which file and at what line number you are making that call, followed by the file and line number where that class's __constuct() method is defined. So now you need to open those file in your code editor, look at those line numbers (and perhaps a few lines to either side) and figure out what's missing. (If you can't, then show us those lines around the specified lines here so we don't have to speculate or use a crystal ball to figure out what they are. 😉 )

      The only changes recently is the host changed the php global version from php4.2 to php5.4 and I changed the .htaccess from php5 to php6.

      Weedpacket replied earlier saying:

      PHP:

      /**
      * construct method
      * set or init database info
      * @param $dbConn
      */
      function __construct($dbConn){

      ---------
      You're not supplying this info. See:

      PHP:

      [code=php]$db_conn = new Db_conn();[/code]


      Have not finish reading all my php books. I just don't know what to do to fix it. If you run out your patient and do not reply i understand, but if you or somebody can help, I am very grateful.
      The defined file - db_conn.php copied below:

      <?php
      	class Db_conn{
      		var $mysql_server_name="xxxxxxxxxx"; //database server name
      		var $mysql_username="xxxxxxxxx"; // database username
      		var $mysql_password="xxxxxxxxx"; // database password
      //		var $mysql_server_name="xxxxxxxx"; //database server name
      //		var $mysql_username="xxxxxx"; // database username
      //		var $mysql_password="xxxxxx"; // database password
      		var $mysql_database="xxxxxxxxx"; // database name
      		var $conn; // connection
      		var $result; // database resource
      
         /**
          * construct method
          * set or init database info
          * @param $dbConn
          */
         function __construct($dbConn){
         		if(!empty($dbConn["mysql_server_name"]))
      		  	$this->mysql_server_name = $dbConn["mysql_server_name"];
      [/QUOTE]	   		if(!empty($dbConn["mysql_username"]))
      		 		$this->mysql_username = $dbConn["mysql_username"];
      	   		if(!empty($dbConn["mysql_password"]))
      		  		$this->mysql_password = $dbConn["mysql_password"];
      	   		if(!empty($dbConn["mysql_database"]))
      		  		$this->mysql_database = $dbConn["mysql_database"];
      	   }
      	   function Db_conn($dbConn){
      
         }
      	/**
      	 * open connection
      	 * select database
      	 */
         function open_conn(){
      		$this->conn=mysql_connect($this->mysql_server_name, $this->mysql_username,$this->mysql_password);
      		mysql_select_db($this->mysql_database, $this->conn);
         }
      
         /**
          * free resource
          * @param $result
          */
         function free_result($result){
      		mysql_free_result($result);
         }
      
         /**
          * close the connection
          */
         function close_conn(){
      	   mysql_close($this->conn); 
         }
      
         /**
          * execute query 
          * @param $queryStr
          * @return database resource
          */
         function query_db($queryStr){
      		//if error say error..
      		if (!$this->conn) {
      			die('Could not connect: ' . mysql_error());
      		}			
      		$result = mysql_query($queryStr,$this->conn);
      		if(!$result){
      			die('mysql error :'.mysql_error());
      		}
      //echo $queryStr."<br/>";
      //var_dump($result);
      			return $result;
      	   }
      
         /**
          * search by str
          * @param $str
          * @param $key
          * @return result list of search 
          */
         function search_by_str($str,$key,$is_array){
      //echo $str."<br/>";	   	
      	   		$result = $this->query_db($str);
      			$list = array();
      			while($row=mysql_fetch_array($result)){
      				if($row[$key])
      					if($is_array)
      						$list[$row[$key]][] = $row;
      					else
      						$list[$row[$key]] = $row;
      				else
      					$list[] = $row;
      			}
      			$this->free_result($result);
      			return $list;
      	   }
      
         /**
          * add modify delete by str
          * 
          * @param $str
          */
         function execute_by_str($str){
         		$result = $this->query_db($str);
         		$this->free_result($result);
         }
      
         /**
          * get the last insert id number
          * @return last insert id
          */
         function get_last_id(){
         		$id = mysql_insert_id();
      		return $id;
         }
      
         /**
          * get count
          * @param $table_name
          * @return table count
          */
         function get_count($table_name,$param=""){
         		$sql = "select count(*) from `$table_name`";
         		if(!empty($param))
         			$sql .= $param;
         		$list = $this->search_by_str($sql);
         		return pos(pos($list));
         }
      }
      ?>
        5 Star;11020445 wrote:

        I changed the .htaccess from php5 to php6.

        This part doesn't make much sense for a couple of reasons:

        1. In what way does your .htaccess have anything to do with a PHP version?

        2. There is no such product named "PHP6" right now.

        5 Star;11020445 wrote:

        I just don't know what to do to fix it.

        The constructor for the Db_conn class requires one parameter (an array, based on the way it's used inside the constructor). You arne't supplying this parameter. Generally speaking, the solution is simple - start supplying the required parameter. The constructor is quite simple, so it should be fairly straightforward what you'll want to make that parameter look like.

        EDIT: For example, since you've x'ed out the default values of the class properties, I'm guessing that they are the values you'd like to use. However, you still have to pass the constructor some sort of array (a variable containing an empty array, an empty array itself, etc.).

          Thank you sooo much. I followed some advise and change the php5 to php6 in htaccess file. The reason to change htaccess file is that you don't need to change the file one by one; As advised, it should change to php6 and it will cover the version php5.4.

          bradgrafelman;11020513 wrote:

          This part doesn't make much sense for a couple of reasons:

          1. In what way does your .htaccess have anything to do with a PHP version?

          2. There is no such product named "PHP6" right now.

          The constructor for the Db_conn class requires one parameter (an array, based on the way it's used inside the constructor). You arne't supplying this parameter. Generally speaking, the solution is simple - start supplying the required parameter. The constructor is quite simple, so it should be fairly straightforward what you'll want to make that parameter look like.

          EDIT: For example, since you've x'ed out the default values of the class properties, I'm guessing that they are the values you'd like to use. However, you still have to pass the constructor some sort of array (a variable containing an empty array, an empty array itself, etc.).

          Can I understand that I can add

          $db_conn = new Db_conn();

          in the constructor to make it work?

            No, that's not what is being suggested, and it wouldn't work anyway, because you're not doing anything to correct the problem the error message is telling you about.

            The advice is in the part you quoted:

            However, you still have to pass the constructor some sort of array (a variable containing an empty array, an empty array itself, etc.).

              5 Star;11020579 wrote:

              Can I understand that I can add

              $db_conn = new Db_conn();

              No, that's what you seem to be doing now: you're not supplying any arguments to the constructor.

              <?php
              
              $args = array(
                  'mysql_server_name' => "database server name"
                 ,'mysql_username'    => 'database username'
                 ,'mysql_password'    => 'database password'
                 ,'mysql_database'    => 'database name'
              );
              
              $db_conn = new Db_conn( $args );

              If you want to use the class' default values, you need to pass an [man]empty[/man] array:

              <?php
              
              $db_conn = new Db_conn( array() );

              Is this your own database connection class, or are you just trying to use it?

              I'm asking because the mysql extension is severely outdated and should not be used where at all possible
              - mysqli or PDO are recommended instead.

                Thank you very much for helping. You are right, both the php and mysql are probably somehow out of date. :o I just set the host server's option back, changed the Global php version on server back to php4 and the error messages are all disappeared. So, it must be some code out of date and giving trouble when I opted php5.4. The host has said it will not support the version older than php5.4 from April 2012, so I have 4 months to learn and to sort the problem out. Thank you again for your help and wish you a very happy 2013.

                traq;11020591 wrote:

                No, that's what you seem to be doing now: you're not supplying any arguments to the constructor.

                <?php
                
                $args = array(
                    'mysql_server_name' => "database server name"
                   ,'mysql_username'    => 'database username'
                   ,'mysql_password'    => 'database password'
                   ,'mysql_database'    => 'database name'
                );
                
                $db_conn = new Db_conn( $args );

                If you want to use the class' default values, you need to pass an [man]empty[/man] array:

                <?php
                
                $db_conn = new Db_conn( array() );

                Is this your own database connection class, or are you just trying to use it?

                I'm asking because the mysql extension is severely outdated and should not be used where at all possible
                - mysqli or PDO are recommended instead.

                  Thank you for trying to help again. I tried to switch back the global php version choice on host from php5.4 back to php4 and all the error message disappeared. So, I guess it might just as traq below suggested, my php and mysql are out of date code. I have 4 months before the host - 1and1 stop supporting the version older than php5.4, hopefully, by that time, I am much better on code and can solve these kind problem easily. Appreciated all you help so far, and maybe I will see you next time somewhere.

                  Weedpacket;11020589 wrote:

                  No, that's not what is being suggested, and it wouldn't work anyway, because you're not doing anything to correct the problem the error message is telling you about.

                  The advice is in the part you quoted:

                    To clarify, I did not say (nor mean to imply) that the error we're discussing is a result of using ext/mysql or an old version of PHP.

                    Yes, you should upgrade to current versions, but it's a separate issue.

                      Write a Reply...