GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Apr-2004, 05:32
eRIC eRIC is offline
Junior Member
 
Join Date: Nov 2003
Location: Kyrgyzstan
Posts: 31
eRIC is on a distinguished road
Question

Help with Database Connection


Hi people. i can connect to my mySQL database server via this code below:
PHP Code:

<?
$cnt=mysql_connect("localhost","admin","somewords");
if($cnt==0)
{
echo "Couldn't connect";
exit;
}
mysql_select_db("mydb");
......
.....
?>

It works fine, but i will use it in all files. i want to write to file which is config.inc where will be database server,user and password information.
I did it but some problems occured.
index.php
PHP Code:

<?
include("myclass.inc");
$param=new veritabani();
$cnt=mysql_connect(trim($param->host),trim($param->user),trim($param->psw));
if($cnt==0)
{
echo "Couldn't connect";
exit;
}
mysql_select_db("db"); /// error from here.
...
.....
.....
?>

myclass.inc
PHP Code:

<?
class  veritabani{
  var $host;
  var $user;
  var $psw;  
  function varitabani() {
       if(file_exists("config.inc"))
       { $fp=fopen("config.inc","r");
         $this->host=fgets($fp);
         $this->user=fgets($fp);
                  $this->psw=fgets($fp);
                 fclose($fp);
       }
   }  
};
?>

It connects to database but i get error that :
1046: No Database Selected.

How can i do this ? Or i should use another way? (without class) Please help me.
Thanks.
__________________
Copyright by eRIC 2003 © !!!
  #2  
Old 23-Apr-2004, 06:29
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Eric,

I am confused... I wouldn't READ my config.inc file usually but that's another story...

Very quickly, this worked: mysql_select_db("mydb"); but this didn't: mysql_select_db("db");? Look at the different DB names, maybe it's just this typo in the name?
  #3  
Old 23-Apr-2004, 06:38
eRIC eRIC is offline
Junior Member
 
Join Date: Nov 2003
Location: Kyrgyzstan
Posts: 31
eRIC is on a distinguished road

no


Quote:
Originally Posted by JdS
Eric,

I am confused... I wouldn't READ my config.inc file usually but that's another story...

Very quickly, this worked: mysql_select_db("mydb"); but this didn't: mysql_select_db("db");? Look at the different DB names, maybe it's just this typo in the name?


no db may be another like mysql_select_db("ericdb");
__________________
Copyright by eRIC 2003 © !!!
  #4  
Old 23-Apr-2004, 06:53
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
OK, try something like this instead...

PHP Code:

<?php

include("myclass.inc");
$param=new veritabani();
$cnt=mysql_connect(trim($param->host),trim($param->user),trim($param->psw));
if( !is_resource($cnt) )
{
  echo "Couldn't connect";
  exit;
}
mysql_select_db("ericdb", $cnt );


?>

  #5  
Old 23-Apr-2004, 23:56
eRIC eRIC is offline
Junior Member
 
Join Date: Nov 2003
Location: Kyrgyzstan
Posts: 31
eRIC is on a distinguished road

yes,thanks


Thanks for help
config.inc
PHP Code:

<?
class DB {
   function DB() {
       $this->host = "localhost";
       $this->db = "mydb";
       $this->user = "root";
       $this->pass = "somewords";
       $this->link = mysql_connect($this->host, $this->user, $this->pass);
       mysql_select_db($this->db);
       register_shutdown_function($this->close);
   }
   function close() {
       mysql_close($this->link);
   }
}
?>


in index.php
PHP Code:

if(file_exists("config.inc"))
{include("config.inc");
$DB_admin = new DB();
if($DB_admin==0){
echo 'Couldn\'t connect to MySqle ';
exit;
}
}
else
{echo "<H2>Error!</H2>\n";
 echo "<H2>Couldn't find Config.inc, control it!</H2>\n";
} 


Last edited by admin : 17-Jun-2004 at 07:35. Reason: Please use [php] & [/php] for syntax highlighting of PHP code
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Clickable web-urls from database. giobbi MySQL / PHP Forum 15 08-Jan-2008 04:18
Parsing PHP code that's stored in a database. JdS MySQL / PHP Forum 3 13-May-2004 09:15
Macromedia DWMX and PHP MySQL setup soulja90 MySQL / PHP Forum 1 17-Mar-2004 04:50
Connection Problems Scott013 Apache Web Server Forum 2 09-Feb-2004 15:11
WInXp Apache database? Ticoloco Apache Web Server Forum 1 27-Nov-2003 04:21

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:31.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.