[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 731
  • Last Modified:

Cannot connect to myphpadmin database through webhost

Hello
I am trying to connect to myphpadmin database through my webhost using php.
This is the first time I have trid to connect using my web host. Before I could connect no problem
using Xp Pro's IIS server. using the following code

<?php
$db = mysql_connect('localhost', 'root', 'mypassword');
mysql_select_db("food", $db);
$query = "SELECT * FROM menu";
$result = mysql_query($query);

while($record = mysql_fetch_row($result)) {
      for ($i=0; $i<count($record); $i++) {
            echo $record[$i]."<BR>";
      }
      echo "<BR>";
}
?>

This displayed a full menu using php. I hav the same database in myphpadmin
I know I have to have my server address, username and password correct  but how do I identify these from my webhost?
0
kevinb4940
Asked:
kevinb4940
  • 8
  • 8
2 Solutions
 
gamebitsCommented:
What is happening when you are trying to connect?
0
 
gamebitsCommented:
If you go to your webhost control panel (cpanel) and you click on the mysql button on that page you should have the look to go to phpmyadmin but before you click on that link look on that page there should be an example of the connection string.
0
 
kevinb4940Author Commented:
I get the following error on the browser

Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL Server Host 'gambit.34sp.com ' (11004) in C:\inetpub\vhosts\onevisionwebdesign.co.uk\httpdocs\menu_full.php on line 2

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in C:\inetpub\vhosts\onevisionwebdesign.co.uk\httpdocs\menu_full.php on line 3

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\inetpub\vhosts\onevisionwebdesign.co.uk\httpdocs\menu_full.php on line 5

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\inetpub\vhosts\onevisionwebdesign.co.uk\httpdocs\menu_full.php on line 5

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\inetpub\vhosts\onevisionwebdesign.co.uk\httpdocs\menu_full.php on line 7


I think I am entering wrong details in the $db = mysql_connect('localhost', 'root', 'mypassword');
line because it worked fine in IIS sever

I know that,
localhost: is the hostname or or ip address ofthe computer which mysql software is running on but how do I find this out?
root: username I use connect to the mysql server
mypassword: password I use to connect to the mysql server
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
gamebitsCommented:
I see this

C:\inetpub\vhosts\onevisionwebdesign...

Is your file still on your computer? If so you need to upload the file to the server to get it to work.
0
 
kevinb4940Author Commented:
Funny That folder is not there

I also looked at my control panel and there is no mysql button just a 'create new database button'
There is no connection string example
0
 
kevinb4940Author Commented:
 Does this mean anything It is under a section called PROCESS LIST


            ID              User                Host          Database   Command   Time   Status   SQL-query  
 Kill   354520   kevinb4940   localhost:3255   None   Query   0   ---   SHOW PROCESSLIST  
0
 
gamebitsCommented:
Are you sure you created the database?
0
 
kevinb4940Author Commented:

Yes the Database is created in myphpadmin accessed through my web hosy control panel
0
 
gamebitsCommented:
Here is a quick script to make a connection

<?php
 // Report simple running errors
error_reporting (0);

   $db = mysql_connect("localhost", "USERNAME", "PASSWORD");

if (!$db)
  {
    echo "Error: Could not connect to database. Please try again later.";
    exit;
  }

  mysql_select_db("DATABASE_NAME");
 
  ?>

Replace USERNAME, PASSWORD and DATABASE_NAME with the proper info, take note that some webhost append the username you used to register to the database_name and the username you use with mysql

i.e. lets suppose I signup with a host using the name gamebits for my account and then I create a database that I want to call Test and I set up the userame as Bob the proper connection string could be gamebits_Bob for the username and gamebits_Test for the database name.
0
 
kevinb4940Author Commented:
just getting a blank screen

the localhost name must be wrong? should I have an ipaddress in there?

Sorry bout this
0
 
gamebitsCommented:
You don't have to be sorry the fact that you are getting a blank screen make me believe that you are connecting to the db.
0
 
kevinb4940Author Commented:
I got the Error: Could not connect to database. Please try again later.message up i was using the wrong password does this mean im connected??
0
 
gamebitsCommented:
The message will be displayed if you cannot connect no message (blank screen) would mean you are connected.

So if you get a blank screen add a query to the script and try it again.
0
 
kevinb4940Author Commented:
YEAH BABY YEAH!! I GOT IT WORKING THANKS A MILLION GAMEBITS!!!

I used the connection string

<?php
$db = mysql_connect('localhost', 'myphpadminusername', 'the password I use to logonto myphpadmin');
mysql_select_db("FOOD", $db);
$query = "SELECT * FROM menu";
$result = mysql_query($query);

while($record = mysql_fetch_row($result)) {
      for ($i=0; $i<count($record); $i++) {
            echo $record[$i]."<BR>";
      }
      echo "<BR>";
}
?>
0
 
gamebitsCommented:
Awesome!!!!!
0
 
kevinb4940Author Commented:
Thanks again!

You got the points! Got a bit carried away there but hey, It' s working.


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now