Jump to content

Can't connect to mysql db


Leshiy

Recommended Posts

Hi there, I am new to php. SO I have downloaded few examples and all that and have set up apache server and mysql server on my linux box. Independently everything seem to work fine. However when I try and connect and run simple query I get no where. Is it because I am trying to connect to the "wrong place"

Here is some code I have used:

[code]
<?php
$mysqluser = 'user';
$mysqlpasswd = 'passwd';
$mysqlhost = 'localhost';
$connID = mysql_connect($mysqlhost, $mysqluser, $mysqlpasswd);
$result = mysql_list_dbs();
echo "<p>Databases in the local MySQL server</p>\n";
echo "<p>";
while ($row = mysql_fetch_row($result)) {
  echo "<br><em>$row[0]</em>\n";
}
echo "</p>";
?>[/code]

Unfortunately I just get empty page with no results. Mysql is running on my machine and I can access it, creat and drop tables via command line.

Any thoughts on why i can't do the same via php/internet?

Thanks

Alex
Link to comment
Share on other sites

[code]<?php
$mysqluser = 'user';
$mysqlpasswd = 'passwd';
$mysqlhost = 'localhost';
$connID = mysql_connect($mysqlhost, $mysqluser, $mysqlpasswd);
$result = mysql_list_dbs();
echo "<p>Databases in the local MySQL server</p>\n";
echo "<p>";
while ($row = mysql_fetch_row($result)) {
  echo "<br><em>$row[0]</em>\n";
}
echo "</p>";
?>[/code]
First off you are trying to run, before you walk.  First get the connection, forget about list db's first.
Simply do
mysql_connect and mysql_select_db, using hte username/password you specified, lieke this

mysql_connect("localhost", "username", "password");
mysql_select_db("db_name");
echo mysql_error();

keep it simple, then you know whether or not you are even connected in the first place.

If you are, then start working with list db's, if you raen't give us the error message.
If no error message returns or outputs to the page, then you are connected.
Link to comment
Share on other sites

Here is what I got:
[code]
echo 'debug 1';
mysql_connect("localhost", "alex", "user");
echo 'debug 2';
mysql_select_db("mysql");
echo mysql_error();
[/code]

The only thing that is displayed is "debug 1" ..... so I presume I didn't manage to connect to mysql server

I have tried different varietions of localhost, 127.0.0.1,127.0.0.1:3306 same output.
Link to comment
Share on other sites

Ok, if that is the case, you are not making a connection.  What webhost do you have, call them adn see if you are suppose to use "localhost" or if it's different with that host, it's not always localhost, but it is most of the time.  Make sure what it is real quick then double check  your username/password.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.