cannot get the values specified inside of another file using include or require_once

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mich dobelman

    cannot get the values specified inside of another file using include or require_once

    I cannot reference the values in side of config.php
    $dbhost,$dbuser ,$dbpasswd ,$dbname why?
    <?php

    require_once('i ncludes/db.php'); //db connection functions
    require_once('c onfig.php'); //db connection parameters


    class DBRegion{
    var $db;
    function DBRegion(){

    }

    function open(){

    $this -db = new sql_db($dbhost , $dbuser , $dbpasswd , $dbname ,
    false);

    if( !$this -db -db_connect_id )
    {
    die( "Could not connect to the database" );
    }
    }

    function close(){

    $this -db -sql_close();

    }

    function updateName( $id, $name ){

    $sql = "update web_coupon_regi on set name = '$name' where id = $id ";

    $result = $this -db -sql_query( $sql ) or die( "Coun't proceed with
    the following sql: ".$sql );

    }

    //UPDATE `web_coupon_reg ion` SET `mdate` = '2006-08-16 05:05:55' WHERE
    `id` = '1' LIMIT 1 ;

    function updateModDate( $id, $date )
    {

    //$this -db = new sql_db($dbhost , $dbuser , $dbpasswd , $dbname ,
    false);
    $sql = "update web_coupon_regi on set mdate = '$date' where id = '$id'";

    $result = $this -db -sql_query( $sql ) ;

    if(!$result ){
    $error = $this -db -sql_error();
    die( "Coun't proceed with the following sql: ".$error['message'] );
    }
    }
    }

    ?>


  • Manish

    #2
    Re: cannot get the values specified inside of another file using include or require_once

    try using global keyword ...


    function open(){

    global $dbhost;
    global $dbuser;
    global $dbpasswd;
    global $dbname;


    $this -db = new sql_db($dbhost , $dbuser , $dbpasswd , $dbname ,
    false);

    if( !$this -db -db_connect_id )
    {
    die( "Could not connect to the database" );
    }
    }


    .... make sure the file config.php is included before the file in which
    the function open() is defined.

    if you don't want to use global keyword then, declare the variable in
    config.php as,

    define('dbhost' , 'somehost');
    define('dbuser' , 'someuser');
    define('dbpassw d', 'somepassword') ;
    define('dbname' , 'somedb');

    and in function open(), use them as (without $ sign)

    $this -db = new sql_db(dbhost , dbuser , dbpasswd , dbname , false);


    Thanks.


    mich dobelman wrote:
    I cannot reference the values in side of config.php
    $dbhost,$dbuser ,$dbpasswd ,$dbname why?
    <?php
    >
    require_once('i ncludes/db.php'); //db connection functions
    require_once('c onfig.php'); //db connection parameters
    >
    >
    class DBRegion{
    var $db;
    function DBRegion(){
    >
    }
    >
    function open(){
    >
    $this -db = new sql_db($dbhost , $dbuser , $dbpasswd , $dbname ,
    false);
    >
    if( !$this -db -db_connect_id )
    {
    die( "Could not connect to the database" );
    }
    }
    >
    function close(){
    >
    $this -db -sql_close();
    >
    }
    >
    function updateName( $id, $name ){
    >
    $sql = "update web_coupon_regi on set name = '$name' where id = $id ";
    >
    $result = $this -db -sql_query( $sql ) or die( "Coun't proceed with
    the following sql: ".$sql );
    >
    }
    >
    //UPDATE `web_coupon_reg ion` SET `mdate` = '2006-08-16 05:05:55' WHERE
    `id` = '1' LIMIT 1 ;
    >
    function updateModDate( $id, $date )
    {
    >
    //$this -db = new sql_db($dbhost , $dbuser , $dbpasswd , $dbname ,
    false);
    $sql = "update web_coupon_regi on set mdate = '$date' where id = '$id'";
    >
    $result = $this -db -sql_query( $sql ) ;
    >
    if(!$result ){
    $error = $this -db -sql_error();
    die( "Coun't proceed with the following sql: ".$error['message'] );
    }
    }
    }
    >
    ?>

    Comment

    • mich dobelman

      #3
      Re: cannot get the values specified inside of another file using include or require_once

      I will use the define.

      Thank you very much for your help.

      M.d
      "Manish" <yehaimanish@gm ail.comwrote in message
      news:1156222462 .776425.171470@ m73g2000cwd.goo glegroups.com.. .
      try using global keyword ...
      >
      >
      function open(){
      >
      global $dbhost;
      global $dbuser;
      global $dbpasswd;
      global $dbname;
      >
      >
      $this -db = new sql_db($dbhost , $dbuser , $dbpasswd , $dbname ,
      false);
      >
      if( !$this -db -db_connect_id )
      {
      die( "Could not connect to the database" );
      }
      }
      >
      >
      ... make sure the file config.php is included before the file in which
      the function open() is defined.
      >
      if you don't want to use global keyword then, declare the variable in
      config.php as,
      >
      define('dbhost' , 'somehost');
      define('dbuser' , 'someuser');
      define('dbpassw d', 'somepassword') ;
      define('dbname' , 'somedb');
      >
      and in function open(), use them as (without $ sign)
      >
      $this -db = new sql_db(dbhost , dbuser , dbpasswd , dbname , false);
      >
      >
      Thanks.
      >
      >
      mich dobelman wrote:
      >I cannot reference the values in side of config.php
      >$dbhost,$dbuse r ,$dbpasswd ,$dbname why?
      ><?php
      >>
      > require_once('i ncludes/db.php'); //db connection functions
      > require_once('c onfig.php'); //db connection parameters
      >>
      >>
      > class DBRegion{
      > var $db;
      > function DBRegion(){
      >>
      > }
      >>
      > function open(){
      >>
      > $this -db = new sql_db($dbhost , $dbuser , $dbpasswd , $dbname ,
      >false);
      >>
      > if( !$this -db -db_connect_id )
      > {
      > die( "Could not connect to the database" );
      > }
      > }
      >>
      > function close(){
      >>
      > $this -db -sql_close();
      >>
      > }
      >>
      > function updateName( $id, $name ){
      >>
      > $sql = "update web_coupon_regi on set name = '$name' where id = $id ";
      >>
      > $result = $this -db -sql_query( $sql ) or die( "Coun't proceed
      >with
      >the following sql: ".$sql );
      >>
      > }
      >>
      > //UPDATE `web_coupon_reg ion` SET `mdate` = '2006-08-16 05:05:55' WHERE
      >`id` = '1' LIMIT 1 ;
      >>
      > function updateModDate( $id, $date )
      > {
      >>
      > //$this -db = new sql_db($dbhost , $dbuser , $dbpasswd , $dbname ,
      >false);
      > $sql = "update web_coupon_regi on set mdate = '$date' where id =
      >'$id'";
      >>
      > $result = $this -db -sql_query( $sql ) ;
      >>
      > if(!$result ){
      > $error = $this -db -sql_error();
      > die( "Coun't proceed with the following sql: ".$error['message'] );
      > }
      > }
      > }
      >>
      >?>
      >

      Comment

      • Jerry Stuckle

        #4
        Re: cannot get the values specified inside of another file usinginclude or require_once

        mich dobelman wrote:
        I cannot reference the values in side of config.php
        $dbhost,$dbuser ,$dbpasswd ,$dbname why?
        <?php
        >
        require_once('i ncludes/db.php'); //db connection functions
        require_once('c onfig.php'); //db connection parameters
        >
        >
        class DBRegion{
        var $db;
        function DBRegion(){
        >
        }
        >
        function open(){
        >
        $this -db = new sql_db($dbhost , $dbuser , $dbpasswd , $dbname ,
        false);
        >
        if( !$this -db -db_connect_id )
        {
        die( "Could not connect to the database" );
        }
        }
        >
        function close(){
        >
        $this -db -sql_close();
        >
        }
        >
        function updateName( $id, $name ){
        >
        $sql = "update web_coupon_regi on set name = '$name' where id = $id ";
        >
        $result = $this -db -sql_query( $sql ) or die( "Coun't proceed with
        the following sql: ".$sql );
        >
        }
        >
        //UPDATE `web_coupon_reg ion` SET `mdate` = '2006-08-16 05:05:55' WHERE
        `id` = '1' LIMIT 1 ;
        >
        function updateModDate( $id, $date )
        {
        >
        //$this -db = new sql_db($dbhost , $dbuser , $dbpasswd , $dbname ,
        false);
        $sql = "update web_coupon_regi on set mdate = '$date' where id = '$id'";
        >
        $result = $this -db -sql_query( $sql ) ;
        >
        if(!$result ){
        $error = $this -db -sql_error();
        die( "Coun't proceed with the following sql: ".$error['message'] );
        }
        }
        }
        >
        ?>
        >
        >
        IMHO it's better to pass the parameters to the function, i.e.

        function open($host, $user, $passwd, $name){

        $this -db = new sql_db($host , $user , $passwd , $name ,
        false);

        if( !$this -db -db_connect_id )
        {
        die( "Could not connect to the database" );
        }
        }


        ....

        $dbregion = new $DbRegion();
        $dbregion -open($dbhost, $dbuser, $dbpasswd, $dbname);

        This gives you more flexibility in your code. Right now you may only
        use one database on one host, for instance. But what about another
        system - or later?

        Or even now, you might have one user with limited privileges for the
        general public, and another user with more privileges for admin users.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        Working...