Class, Connecting to mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kenno66
    New Member
    • Oct 2007
    • 3

    Class, Connecting to mysql

    Hey, i have been programming php for a month now, well learning. And a friend asked me if i have done classes, as in class function etc and i said no. So he said its easy and that.
    Spent awhile looking for help but no where actually gives you the info i need.
    Anywayi have a class file called class.inc.php. And because i am using mysql i want a DB class.
    Here is my code
    [PHP]
    <?
    class db
    {
    var $db;
    var $con;

    function getdb()
    {
    $db['dbhost'] = 'localhost';
    $db['dbusername'] = 'root';
    $db['dbpassword'] = '';
    $db['dbname'] = 'logg';
    return $db;
    }
    function connect()
    {
    $host = $db['dbhost'];
    $db = $db['dbname'];
    $user = $db['dbusername'];
    $pass = $db['dbpassword'];
    mysql_connect($ host, $user, $pass);
    mysql_select_db ($db);
    return $con;
    }
    $db = new db();
    ?>[/PHP]
    So my next file index.php basically shorten down to look like this
    [PHP]
    require('class. inc.php');
    $db->con;
    $result = mysql_query("SE LECT * FROM news");

    while($row = mysql_fetch_arr ay($result))
    {
    echo $row['id'];
    echo "<br />";
    }
    [/PHP]
    And nothing appears, and i have 7 records and not even 1 shows.
    This is the error that appears
    Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localho st' (using password: NO) in C:\ini\htdocs\r yan\2\index.php on line 46

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\ini\htdocs\r yan\2\index.php on line 46

    Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource in C:\ini\htdocs\r yan\2\index.php on line 48
    Any help would be appreaciated alot! Thanks
  • kenno66
    New Member
    • Oct 2007
    • 3

    #2
    Any help please???
    I been looking everywhere for answers

    Comment

    • jgentes
      New Member
      • Jul 2007
      • 32

      #3
      Originally posted by kenno66
      [PHP]
      require('class. inc.php');
      $db->con;

      [/PHP]
      And nothing appears, and i have 7 records and not even 1 shows.
      This is the error that appears
      Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localho st' (using password: NO) in C:\ini\htdocs\r yan\2\index.php on line 46

      Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\ini\htdocs\r yan\2\index.php on line 46

      Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource in C:\ini\htdocs\r yan\2\index.php on line 48
      Any help would be appreaciated alot! Thanks
      you have 2 variables trying to do two multiple things...
      scope wise this poses a problem, because you are declaring $db in the beginning, using it as 1 thing in your getdb() function than using it entirely different in your connect() function. if you had initiated them inside the functions it would be fine, but you are decarling it outside the function(s) thus giving it global scope to the class.

      I would first just change db to dbname in the connect() function to take out any confusion on that.

      then you also want to be careful of declaring db as the same name of the class. if you want the getdb() function to run every time the class is initiated, you can make it a __constructor.

      Comment

      • amitabhcy
        New Member
        • Nov 2007
        • 4

        #4
        Originally posted by kenno66
        [PHP]
        class db
        {
        var $db;
        var $con;

        function getdb()
        {
        $db['dbhost'] = 'localhost';
        $db['dbusername'] = 'root';
        $db['dbpassword'] = '';
        $db['dbname'] = 'logg';
        return $db;
        }
        function connect()
        {
        $host = $db['dbhost'];
        $db = $db['dbname'];
        $user = $db['dbusername'];
        $pass = $db['dbpassword'];
        mysql_connect($ host, $user, $pass);
        mysql_select_db ($db);
        return $con;
        }
        $db = new db();
        ?>[/PHP]
        So my next file index.php basically shorten down to look like this
        [PHP]
        require('class. inc.php');
        $db->con;
        $result = mysql_query("SE LECT * FROM news");

        while($row = mysql_fetch_arr ay($result))
        {
        echo $row['id'];
        echo "<br />";
        }
        [/PHP]
        Within the getdb function call $this->db instead of $db.

        Comment

        Working...