473,382 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Can't connect to remote MySQL server using PHP

obviously I am doing something wrong when I try to connect to my db. Please help
the code I am using is something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php function connect_db() {
  2.   global $link;
  3.  
  4. $host = 'server.root.com';  (Ps: Note I am not using www.server.root.com)
  5. $user = 'username';
  6. $pass = 'password';
  7. $dbname = 'database'; 
  8.  
  9.   $link = @mysql_connect($host, $username, $password)
  10.     or die ("Could not connect to database");
  11.  return $link; 
  12. }
  13.  
  14. ?>
  15.  
  16. <?php if (!isset($link))
  17.  
  18. connect_db();
  19.  
  20. @mysql_select_db ($dbname)
  21.     or die ("Could not select database"); 
  22.  
  23. .....
  24. .....
  25. ?>
  26.  
I am getting a message saying "No Database Selected"

I have just started using mysql and php hence the high level of ignorance. I would appreciate help in any and all forms.

Thanks in advance

shawzozo
Jun 9 '06 #1
3 10404
obviously I am doing something wrong when I try to connect to my db. Please help
the code I am using is something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php function connect_db() {
  2.   global $link;
  3.  
  4. $host = 'server.root.com';  (Ps: Note I am not using www.server.root.com)
  5. $user = 'username';
  6. $pass = 'password';
  7. $dbname = 'database'; 
  8. $link = @mysql_connect($host, $username, $password)
  9.     or die ("Could not connect to database");
  10.  return $link; 
  11. }
  12.  
  13. ?>
  14.  
  15. <?php if (!isset($link))
  16.  
  17. connect_db();
  18.  
  19. @mysql_select_db ($dbname)
  20.     or die ("Could not select database"); 
  21.  
  22. .....
  23. .....
  24. ?>
  25.  
I am getting a message saying "No Database Selected"

I have just started using mysql and php hence the high level of ignorance. I would appreciate help in any and all forms.

Thanks in advance

shawzozo
You can use the follwing code and INSALLAH it will help you in connecting to your desired database
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dbhost = 'yourHost';
  3.  
  4. $dbuser = 'yourUser';
  5.  
  6. $dbpass = 'yourPass';
  7.  
  8. $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
  9.  
  10. $dbname = 'yourDatabase';
  11.  
  12. mysql_select_db($dbname)or die("Couldn't successfully connected");
  13.  
  14. echo "Connected Successfully";
  15. ?>
  16.  
Email removed
Posting emails on our public forums is a violation of the Posting Guidelines. - Moderator
Aug 18 '07 #2
Atli
5,058 Expert 4TB
Hi, and welcome to TSDN!

Try adding the mysql_error() function to your error message, to show you what exactly went wrong.
Like so:
Expand|Select|Wrap|Line Numbers
  1. $dbLink = @mysql_connect($host, $user, $pass) 
  2.     or die("<pre><b>MySQL Error:<b/>". mysql_error() ."</pre>");
  3.  
Aug 18 '07 #3
mwasif
802 Expert 512MB
This is due to variable scope of $dbname. $dbname is a local variable in connect_db(). Its value is not available outside function. To get the required results, declare the $dbname global as you did for $link.
[PHP]<?php function connect_db() {
global $dbname;

//rest of the code
?>[/PHP]
Aug 18 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: webhead74 | last post by:
Hi, I've worked on this for a couple of days & can't figure out what I'm doing wrong. I'm trying to connect to a remote AS400 database using odbc & php. Here's my environment: Server: RedHat...
0
by: Ryan Stewart | last post by:
I'm running MySQL server on a WinXP box at home. I have a cable modem/router. I've set up port forwarding on port 3306 to the machine running MySQL. I can connect locally just fine, but when I try...
3
by: Sander Smeenk | last post by:
Hello! I'm trying to set up replication between two servers and even though I did everything according to the documentation, the slave keeps failing to connect to the master like this: |...
16
by: MLH | last post by:
Using MS Access, I have attached to MySQL servers in other states and other countries on the other side of my router. But when I use the MySQL ODBC driver 3.51 to connect to a MySQL server on my...
9
by: Greg Gursky | last post by:
Hello: I'm doing some planning on a potential project with which I need some help. The database at the focus of this question is a MS Access database that is currently accessed by DAO from a...
3
by: Dave F. | last post by:
I am using ODBC to connect to dBase data on a remote server. The ASP.NET app will be running on Win 2k or 2003 server. I have tried to make it work but all I can get is an error. ...
1
by: linux | last post by:
Sorry if this is a really dumb question. I am trying to connect to our remote MySQL server (which is running just fine). I have a perl script which worked just fine on my other Fedora 4 box (all...
47
by: Lauren Wilson | last post by:
One more try. After ransacking the Desktop AND Enterprise edition of Ken Getz Dev Handbooks on Access 2002, I STILL don't know if it is or is not possible to link a client side MDE with a WEB...
4
by: courtney.machi | last post by:
Hello, I am trying to connect to a remote mysql server through php. Here's the code: mysql_connect("my.hostname.edu", "username", "password") or die (mysql_error());...
14
by: Marcus | last post by:
I have a function that simply returns TRUE if it can connect to a particular Sql Server 2005 express, or FALSE if it cannot. I am getting some strange error codes returned when the computer that...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.