Page 1 of 2

PHP/MYSQL Connection Problem

Posted: Wed Aug 27, 2003 12:26 pm
by sroussie
Hi,

I am relatively new to all this. My PHP server is working and it shows MYSQL enabled. MYSQL is working because I can create and use a database. But when I try to see if they are working together using this code:

<html>
<head>
<title>connect to database</title>
</head>
<body>

<?php

$linkID = mysql_connect{"localhost","", ""};

if {$linkID !=FALSE}
{
print "The connection to the server was made successfully.";
}
else
{
print "The connection to the server failed.";
}

mysql_close{$linkID};

?>

</body>
</html>

nothing shows up in the web browser. I have tried it with and without my username and password.

Versions below
PHP 4.3.2
MYSQL 4.0.14b
Windows 2000 Service Pack 4
Apache Server

I also have the appropriate dlls' in the system directory and have edited the ini file for the MYSQL extensions. I have also restarted the Apache service.

COuld someone help? I need to be able to query a MYSQL inventory database through a browser and display the results ASAP.

Thanks.

Posted: Wed Aug 27, 2003 12:33 pm
by JAM
Try this:

Code: Select all

$link = mysql_connect("localhost", "mysql_user", "mysql_password")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_close($link);
Still nothing?

Direct Link Manual

Posted: Wed Aug 27, 2003 12:41 pm
by sroussie
Hi,

That worked. I had to leave out the user and password. Is there anything I should know about doing the code for the database query?

Thanks,
Sue

Posted: Wed Aug 27, 2003 2:04 pm
by JAM
Don't take me wrong, but that would be a long discussion, so I personally think that the best solution is to redirect you to the manuals directly. It's comprehensive enough to be easely understood.

http://se.php.net/manual/en/ref.mysql.php
Check lower down for "Table of Contents" for a quick guide.

Posted: Sun Aug 31, 2003 10:44 am
by Okanogan
I have a similar problem. I cannot connect to any of my tables in the database.

Any help would be appreciated, I'm kind of new at this.

<?php

// MYSQL CONNECT STRINGS
$db_user = "aplus"; //database user
$db_password = " "; //database password
$db_database = "test"; //database definition file
$db_server = "localhost"; //database server

$conn = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
print ("Connected successfully");

mysql_select_db($db_database, $conn) or die(mysql_error());
print ("Connected scucessfully");


mysql_close($conn);
?>


The error message is -

Connected successfullyAccess denied for user: 'aplus@localhost' to database 'test'

Posted: Sun Aug 31, 2003 10:50 am
by JAM
Try:

Code: Select all

// Change
$db_password = " "; //database password 
// to...
$db_password = ""; //database password
Might do the trick? (Notice the space in your password.) If that doesn't help, check the manual of mysql to set/update user passwords.

Good Luck.

Posted: Sun Aug 31, 2003 10:56 am
by Okanogan
Thanks for your post, I have searched the web and several sites and the format seems to be right. Some vary a bit from site to site but not much.

On the $db_password = " "; //database password, I have the actual password on the page

$db_password = "passowrd"; //database password


Thanks

Posted: Sun Aug 31, 2003 11:05 am
by JAM
Access denied for user: 'aplus@localhost' to database 'test'
If I understood correctly, you should if it's not working check the access rights for that table. As the quote says, you have the wrong (or just cant) access that particular table.

http://www.mysql.com/doc/en/Access_denied.html

Posted: Sun Aug 31, 2003 11:46 am
by m3rajk
if you can't access the table, then, since i didn't see this in the codes, do any of you actually tell it WHICH database to use?

after using
$connection=mysql_connect($host, $login, $pw);

use
mysql_select_db($database, $connection);

Posted: Sun Aug 31, 2003 11:48 am
by Okanogan
Thanks for your replies and links,

I have looked through several of those pages for the answer, except the http://www.mysql.com/doc/en/Access_denied.html in my endevors to figure this out. I am not sure but http://www.mysql.com/doc/en/Access_denied.html looks like the server admisistrator information usless some of that can be run on the URL. I am using a hosting service for this and they are not real helpful.

Is there any ini files that should be included or anything. I can acess my table on line in the msqladmin with the username and password and I don't think there is a seperate password for the table.

Posted: Mon Sep 01, 2003 7:50 pm
by Okanogan
I have searched the web for more information and see several ways to connect but nothing giving me any infomation on why I can connect to the database but not the table. I have the table in there even made another page to create a table and even another table with only one field.

No Luck any help would be appreciated, the hosting service is not any help, and it may not be there problem.

// MYSQL CONNECT STRINGS
$db_user = 'aplus';
$db_password = 'password';
$db_database = 'stamporder';
$db_server = 'localhost';

//CONNECT TO MYSQL
$conn = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
print ("Connected successfully");


// SELECT DATABASE TO USE
mysql_select_db($db_database, $conn)
or die("Couldn't open $db_database: ".mysql_error());


Connected successfully

Couldn't open stamporder: Access denied for user: 'aplus@localhost' to database 'stamporder'

Posted: Tue Sep 02, 2003 4:02 am
by twigletmac
You can connect to the database server successfully because your user has the rights to do that - you cannot access the database 'stamporder' because the user does not have the necessary permissions.

Do you have phpMyAdmin - it makes it easy to see what permissions each user has. The MySQL information on this topic is here:
http://www.mysql.com/doc/en/Privileges.html
and how to use the GRANT statement is here
http://www.mysql.com/doc/en/GRANT.html

For example to grant a user called 'test' the rights to SELECT, UPDATE, DELETE, and INSERT on the 'my_database' database, you can use the following SQL statement:

Code: Select all

GRANT SELECT, INSERT, UPDATE, DELETE 
ON `my_database`.* 
TO "test"@"localhost";
Mac

Posted: Tue Sep 02, 2003 7:59 am
by Okanogan
Thank you I figured it out late last night, the database name and the user name are the same that took care of the problem. I will keep the information on the granting rights and the links posted.

$hostname="localhost";
$username="aplus";
$password="password";
$dbname="aplus";
$usertable="test";


mysql_connect($hostname,$username, $password) OR DIE

@mysql_select_db($dbname) or DIE


# Check If Record Exists

$query = "SELECT * FROM $usertable WHERE


Thank you for your posts, I am quite new to this php. :oops:

Posted: Tue Sep 02, 2003 9:01 am
by twigletmac
Okanogan wrote:Thank you for your posts, I am quite new to this php. :oops:
Everybody has to start somewhere... (we was all newbies once :wink: )

Mac

Posted: Wed Sep 03, 2003 6:00 pm
by RobertStout
try

$linkID = mysql_connect{"localhost","root", ""};

also shouldnt it single quotes and parenthesis (not curly brackets)? or am I mizzing languages, or are both valid?
i.e.

$db_conn = mysql_connect('localhost', 'root' ''); //is what I use