connecting to DB with php

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
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.':'.
  [EMAIL="$db_pass.'@'"]$db_pass.'@'[/EMAIL].
  $db_host.'/'.
  $db_name;
[COLOR=black][I][COLOR=darkred] $db_object = DB::connect($datasource, TRUE);[/COLOR]
[/I][/COLOR] if(DB::isError($db_object)) {die($db_object->getMessage());}
 $db_object->setFetchMode(DB_FETCHMODE_ASSOC);
 include('check_login.php');
?>
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
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
 

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
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.':'.
  [EMAIL="$db_pass.'@'"]$db_pass.'@'[/EMAIL].
  $db_host.'/'.
  $db_name;
[COLOR=darkred][I] $db_object = mysql_connect($datasource, TRUE);
 if(DB::isError($db_object))[/I][/COLOR] {
     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:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
did you give the user rights on the database you want to connect to?
 

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
oh wow i forgot to add them to that particular database awesome! it works now i can connect to the database :)
 
Top