Help with MySQL !

Hi, am trying to get a simple php script working:

===========================================

<?PHP $user = "maincom_test"; $pass = "test"; $data = "maincom_test"; // setup the connection $link = mysql_connect ("localhost", $user, $pass) || die("first dead"); printf("user: %s",$user); printf("db: %s",$data); // select the table mysql_select_db ($data, $link) or die("die"); // get some results o_O $result = mysql_query("SELECT * FROM employees", $link); // print results printf("

Printing results

"); ?>

===========================================

But it does not work!
I went into cpanel and i made the user named test and it automatically added maincom_test ( user ) then i also made a database named test and hence, maincom_test (database) was made.

So i am sure about the username and password because i do not get error on the connection ( when i make $link)

but when when i load that page in a browser it executes the die(“die”) and os the text is outputted and then die. NO INFORMATION FROM THE DATABASE IS RETREIVED. When i remove the || die(“die”) part it just gives error on the query line. It says:

mysql_query does not have a valid MySQL resrouce - Link.

So am thinking then it is becuase the $link is not established. I am not sure.

But for the above the user name and password are correct because the connection to the database error does not show up. And i doulbe checked to see whether the database name is correct adn it is also.

my phpsqladmin says database maincom_test on localhost

someone please help i appreciate your kindness in advance .

I’m not completely new to this but I belive it has something to do with the way your trying to connect to the DB. Try adding a line that tells you if you connect successfully. Or better yet drop the $link and $data lines all together and swap them to something like:

<?php mysql_connect ('localhost', 'user', 'pass');

mysql_select_db (‘maincom_test’);


Then put in the rest of your code. I’ve noticed that sometimes PHP dislikes some of the connection arguements. Then again, I could be completely wrong here. But hey, worth a shot right.

Don

also look into the mysql_error(). It can save you lots of headaches if you use it all the time in your code to handle errors.

http://www.php.net/manual/en/function.mysql-error.php

Here is the new one, but i still get the same problem:

=================================

<?php $user = "prestige_main"; $pass = "test"; $data = "prestige_inventory"; // setup the connection mysql_connect ('localhost', $user, $pass); echo mysql_error(); // access denied it says // select the table mysql_select_db ('prestige_inventory'); echo mysql_error(); // says no db selected printf("user: %s",$user); printf("db: %s",$data); // get some results o_O $result = mysql_query("SELECT * FROM employees"); echo mysql_error(); // this says no database selected // print results printf("

Printing results

"); printf("First Name: %s
n", mysql_result($result,0,"first")); //printf("Last Name: %s
n", mysql_result($result,0,"last")); //printf("Address: %s
n", mysql_result($result,0,"address")); //printf("Position: %s
n", mysql_result($result,0,"position")); ?>

=========================================

the output of the above is:

==========================================
Access denied for user: ‘prestige_main@localhost’ to database ‘prestige_inventory’
user: prestige_main
db: prestige_inventory
No Database Selected
Printing results

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/prestige/public_html/site/index.php on line 26
First Name:

========================================

what is the problem?
It says access denied but i am sure of the user name and password

i logged into cpanel and went to mysql databases and then i simply added new database named “inventory” ( it added the “prestige_” itself) and i also made the user “main” (it added the prestige_ part istlef).

I also tried just using the name main as user but it gives me this output:

==============================================
Warning: mysql_connect(): Access denied for user: ‘main@localhost’ (Using password: YES) in /home/prestige/public_html/site/index.php on line 13
Access denied for user: ‘main@localhost’ (Using password: YES)Access denied for user: ‘@localhost’ to database ‘inventory’
user: main
db:
No Database Selected
Printing results

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/prestige/public_html/site/index.php on line 25
First Name:

thank you for your help!

OK so the problem lies in connecting to the Database. I want you to do this:

$link = mysql_connect("$DBaddr", “$DBuser”, “$DBpass”) or die ("Error: Database Connection Error - ".mysql_error());
mysql_select_db($db, $link) or die ("Error: Database Selection Error - ".mysql_error());

That will combine your first three lines and allow the script to “die” gracefully.

Now to connect to the database… I think you are having problems with the single versus double quote. I would use the code I gave above with this:

$DBaddr = “localhost”;
$DBuser = “prestige_main”;
$DBpass = “test”;
$db = “prestige_inventory”;

Good luck!

new code


<?php // user name and pass. $DBaddr = "localhost"; $DBuser = "prestige_main"; $DBpass = "test"; $db = "prestige_inventory"; $link = mysql_connect("$DBaddr", "$DBuser", "$DBpass") or die ("Error: Database Connection Error - ".mysql_error()); mysql_select_db($db, $link) or die ("Error: Database Selection Error - ".mysql_error()); /** // setup the connection mysql_connect ('localhost', $user, $pass); echo mysql_error(); // access denied it says // select the table mysql_select_db ('prestige_inventory'); echo mysql_error(); // says no db selected */ printf("user: %s",$user); // get some results o_O $result = mysql_query("SELECT * FROM employees"); echo mysql_error(); // this says no database selected // print results printf("

Printing results

"); printf("First Name: %s
n", mysql_result($result,0,"first")); //printf("Last Name: %s
n", mysql_result($result,0,"last")); //printf("Address: %s
n", mysql_result($result,0,"address")); //printf("Position: %s
n", mysql_result($result,0,"position")); ?>

and the output is :

--------------------------------------------------------=+
Error: Database Selection Error - Access denied for user: ‘prestige_main@localhost’ to database ‘prestige_inventory’

--------------------------------------------------------=+’

thank you once again

OK we’re connecting…

check to make sure that tha DBuser is an authorized user of the prestige_inventory database through the cpanel. And remove the mysql_error statement here

(// get some results o_O
$result = mysql_query(“SELECT * FROM employees”);
echo mysql_error(); // this says no database selected )

links you should look at:
http://www.php.net/manual/en/function.die.php
http://www.php.net/manual/en/function.mysql-error.php

if it connects on the connection statement, then doesn’t that mean that the user name and password are correct? Because if i change the password and username to incorrect info then it would die on the first die statement rite?

Why are you running the mysql_connect statement twice, with different criteria?

Keith

kcomer - the second call is commented out

The User name and Password are correct to access the database server (where the databases are kept). The database (where the data is) you are trying to connect to may not allow THAT user to talk to it.

Ex:
In the MySQL Server I have 3 databases. 1 for my members list and their information (operational), 1 for the various email campaigns my company sends to the members list for promotions, and a third for testing purposes of new software.

Now I do not want my software developers or the marketing people playing with my operational database or each others stuff. So I only allow them to have access to only their respective databases, (Root user has access to all DBs) there by protecting the others databases from possible errors, mistakes, and possible hacking.

You may be here:
You may be trying to get the user from marketing into the operational database (Access denied for user: ‘prestige_main@localhost’ to database ‘prestige_inventory’ ).

Check your cpanel and varify that that particular user is authorized to access that particular database. If they are then there may be a typo in the name itself.

Sponsor our Newsletter | Privacy Policy | Terms of Service