+ Reply to Thread
Results 1 to 5 of 5

Thread: connecting to DB with php

  1. #1
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    connecting to DB with php

    Ok I have a new php problem. I read a tutorial on connected to a database with php but I am having some problems. I get this error message when I try to run the script

    Fatal error: Class 'DB' not found in /home/frostbit/public_html/php/protected/db_connect.php on line 12

    idk what is wrong with it also how do i make a file protected so only I can use it?

    Code:
     
    <?php
     $db_engine = 'mysql';
     $db_user = 'admin';
     $db_pass = '**';
     $db_host = 'localhost';
     $db_name = '**';
     $datasource = $db_engine.'://'.
      $db_user.':'.
      $db_pass.'@'.
      $db_host.'/'.
      $db_name;
     $db_object = DB::connect($datasource, TRUE);
     if(DB::isError($db_object)) {die($db_object->getMessage());}
     $db_object->setFetchMode(DB_FETCHMODE_ASSOC);
     include('check_login.php');
    ?>

  2. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: connecting to DB with php

    if you take all of that above and put it in a function it can only be accessed from within your script. It's really pretty safe either way.

    I think what you're trying to do is $db_object = mysql_connect(....) because DB::Object doesn't exist
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  3. #3
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Re: connecting to DB with php

    ok so if i put it all in a function say

    function dbconnect()

    i would have to do

    require 'protected/db_connect.php';
    dbconnect()

    on any page that i need to connect to the database right?

    ok so i made the change and i get these errors now:

    Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 113 in /home/frostbit/public_html/php/protected/db_connect.php on line 12

    Fatal error: Call to undefined function iserror() in /home/frostbit/public_html/php/protected/db_connect.php on line 13

    Code:
     
    <?php
     $db_engine = 'mysql';
     $db_user = 'admin';
     $db_pass = '**';
     $db_host = 'localhost';
     $db_name = '**';
     $datasource = $db_engine.'://'.
      $db_user.':'.
      $db_pass.'@'.
      $db_host.'/'.
      $db_name;
     $db_object = mysql_connect($datasource, TRUE);
     if(DB::isError($db_object)) {
         die($db_object->getMessage());
     }
     $db_object->setFetchMode(DB_FETCHMODE_ASSOC);
     include('check_login.php');
    ?>
    i see what i am doing wrong, i reread the tutorial and i am missing something important: require_once 'DB.php';
    Edit:
    ok ive changed my pace and i tried byron's tutorial this is my code:

    Code:
    <?php 
    $username = "frostbit_admin";
    $password = "**"; 
    $server = "localhost";
    $database="frostbit_members";
    $mysqlconnection = mysql_connect($server, $username, $password); 
    if (!$mysqlconnection) { 
       die('There was a problem connecting to the mysql server. Error returned: '. mysql_error()); 
    } 
    $databaseconnection = mysql_select_db($database); 
    if (!$databaseconnection) { 
       die('There was a problem using that mysql database. Error returned: '. mysql_error()); 
    } 
     ?>
    it pretty much works but i am getting denied access to the local host even though i set up the mySQL database through cpanel
    Last edited by garrensilverwing; 04-28-2009 at 03:00 PM. Reason: Automerged Doublepost

  4. #4
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: connecting to DB with php

    did you give the user rights on the database you want to connect to?
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  5. #5
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Re: connecting to DB with php

    oh wow i forgot to add them to that particular database awesome! it works now i can connect to the database

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. currently have an application pending php
    By biomasti in forum Free Hosting
    Replies: 1
    Last Post: 09-03-2008, 01:58 PM
  4. php errors galore
    By DMG Online in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 05-17-2008, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers