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, 06:32
eRIC eRIC is offline
Awaiting Email Confirmation
 
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.
  #2  
Old 23-Apr-2004, 07: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, 07:38
eRIC eRIC is offline
Awaiting Email Confirmation
 
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");
  #4  
Old 23-Apr-2004, 07: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 24-Apr-2004, 00:56
eRIC eRIC is offline
Awaiting Email Confirmation
 
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 08:35. Reason: Please use [php] & [/php] for syntax highlighting of PHP code
 
 

Recent GIDBlogAccepted for Ph.D. program 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 05:18
Parsing PHP code that's stored in a database. JdS MySQL / PHP Forum 3 13-May-2004 10:15
Macromedia DWMX and PHP MySQL setup soulja90 MySQL / PHP Forum 1 17-Mar-2004 05:50
Connection Problems Scott013 Apache Web Server Forum 2 09-Feb-2004 16:11
WInXp Apache database? Ticoloco Apache Web Server Forum 1 27-Nov-2003 05:21

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

All times are GMT -6. The time now is 09:21.


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