11-07-2003, 01:21 PM
|
#1
|
|
member
Join Date: Feb 2002
Location: London
Posts: 2,442
|
Link identifier when connecting to Database
Hello,
Just a quick question, when i am trying to select a database, and i put the name of the database, then i have to put the link identifier, is that a number or a string ?
PHP Code:
<?
$link = @mysql_connect();
if(!$link)
{
print "Error connecting to database server";
exit;
}
//at the end of this sentence is a tring or integer ?
if (@mysql_select_db("snappleflash", 1 or "1"))
{
print "Congratulations database selected";
}
else
{
print "Could not select database";
}
mysql_close($link);
?>
Thank you very mush for your help and for taking the time to read this post.
Regards, snapple
Last edited by snapple; 11-07-2003 at 01:56 PM.
|
|
|
11-07-2003, 02:07 PM
|
#2
|
|
Super Moderator
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
|
Howdy...
I am not really a good person to answer these sorts of question since I just copy something from some part of the manual and paste it into my code to fit my need...
The manual has this explanation...
Quote:
|
mysql_select_db() sets the current active database on the server that's associated with the specified link identifier.
|
and this code...
PHP Code:
<?php
$lnk = mysql_connect('localhost', 'mysql_user', 'mysql_password')
or die ('Not connected : ' . mysql_error());
// make foo the current db
mysql_select_db('foo', $lnk) or die ('Can\'t use foo : ' . mysql_error());
?>
So, I assume that your this line...
PHP Code:
mysql_select_db("snappleflash", 1 or "1")
should be something like this???
PHP Code:
mysql_select_db("snappleflash", $link)
???
|
|
|
11-07-2003, 02:29 PM
|
#3
|
|
member
Join Date: Feb 2002
Location: London
Posts: 2,442
|
Thank you very much for your quick response.
Yes, i did initially try the...
PHP Code:
mysql_select_db("snappleflash", $link)
...because it did seem the logical option, however i still could not select the database - but i now know it is me now, because i tested connecting to the 'test' database that came with MySQL when i downloaded it and i managed to connect to it fine.
I just wanted to eliminate the option for sure, because when i tackle new languages (especially one the requires installing or downloading 3 separate elements) there is so much possibility for me to make a completely retarded error (as is usually the case).
I can get so muddled that a 'power nap' is often required, that is, assuming the sleeping position in a large chair or in bed for approximately 10 minutes, note to self: any longer than the prescribed 10 minutes or moving into the foetal position (the well known curved sleep position) - and all claims to it being a 'power nap' are null and void - as one has violated rules clearly defined, and has attempted a full on sleep for the afternoon.
Quote:
|
I am not really a good person to answer these sorts of question since I just copy something from some part of the manual and paste it into my code to fit my need...
|
I think you under estimate your abilities - must be a bloody good manual. Where as I actually specialise in my inability to retain useful information, i tend to lurch from book to book like its going to do the work for me - honestly.
Take care buddy.
Thanks very much.
Regards, snapple
|
|
|
11-07-2003, 02:32 PM
|
#4
|
|
Master of Nothing
Join Date: Dec 2002
Location: San Diego, CA
Posts: 2,468
|
Unless you are connecting to multiple databases on the same page, you can omit the link identifier from mysql_select_db.
It assumes the last opened conection is to be used.
PHP Code:
mysql_select_db("snappleflash")
On another note, you should be looking at the actual database errors. Like CyanBlue showed mysql_error() will tell you why the connection failed.
I can tell you from your first post you need to pass host user and password information in the mysql_connect function.
Also prepending functions with @ supresses error messages. So it will be pretty tough to debug whats going on.
|
|
|
11-07-2003, 02:41 PM
|
#5
|
|
member
Join Date: Feb 2002
Location: London
Posts: 2,442
|
Thank you very much freddycodes, i will have a go using different syntax and experimenting.
I will also make use of actual database error checking that Cyanblue pointed out - thank you very much too Cyanblue.
About passing the pass and user identifiers, i thought that i would not have to do that because i am not on a third party host, i use localhost.
Regards, snapple
|
|
|
11-07-2003, 02:45 PM
|
#6
|
|
Super Moderator
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
|
Ah... Power nap...  I don't agree that it should be 10 minutes or less... I often get to sleep for an hour or two in the middle of brain struggling with the codes... My view is the longer, the better it is... 
Quote:
|
I think you under estimate your abilities - must be a bloody good manual. Where as I actually specialise in my inability to retain useful information, i tend to lurch from book to book like its going to do the work for me - honestly.
|
Hehe... Thanks, but I really meant it... I don't practice or try to learn things in PHP or MySQL since I am not really good at them and since my brain cannot take more than one... So, I just copy some example snippets from the manual or forums to get what I want... and I don't go any further... I always blame my lazy nature for that... 
Quote:
|
I can tell you from your first post you need to pass host user and password information in the mysql_connect function.
|
Yeah... I see that the information were missing in there... Add them to test the code, snapple... 
And follow all those freddycodes' advices... He's a good teacher... I mean it... 
Quote:
|
About passing the pass and user identifiers, i thought that i would not have to do that because i am not on a third party host, i use localhost.
|
You should supply them whether you are on the third party host or on your localhost...
|
|
|
11-08-2003, 02:03 PM
|
#7
|
|
member
Join Date: Feb 2002
Location: London
Posts: 2,442
|
Thank you very much for all your help. I am sorry to say that i require some help, agian.
Now i have searched the forums for this, but there does not seem to be whole lot on it, i also have my Steve Webster book by my side.
I am having problems selecting the database. I have made use of the mysql_error function that Cyanblue pointed out to me, but do not know why i am not able to select the databse named 'bears'
I have this php:
PHP Code:
<?
//attempt to connect to the MySQL server
$link = mysql_connect();
print ("linkID is $link<br>\n");
if(!$link)
{
print "Error connecting to database server";
exit;
}
if(mysql_select_db("bears", $link))
{
print "Database selected";
}
else
{
print "Could not connect to database<br>\n";
print mysql_error();
}
?>
I get this output:
http://www.uzi-lover.com/imageone
Er, dont know why this is, but the HTML tags are not supported, sorry.
I would appreciate it, if someone could help me out as to why this is occuring. I have the Database created (i did that in the MySQL monitor) and checked it through 'SHOW DATABASES;'
Thanks very much for any help or clarity regarding my own stupidity.
Oh - should mention that when i use the 'test' Database that i can see in the MySQL interface (along with my Bear one) - that the 'test' database can selected without a problem.
Thanks once again !
Best regards, snapple
Last edited by snapple; 11-08-2003 at 02:28 PM.
|
|
|
11-08-2003, 04:18 PM
|
#8
|
|
Master of Nothing
Join Date: Dec 2002
Location: San Diego, CA
Posts: 2,468
|
Once again you did not specify a user to connect with in your mysql_connect statement.
This has more to do with the mysql permissions system than it does with your php.
By default the user root has access to all created databases. You could create another user with permissions to view the bears database, but if this for local testing, you can just use the root user when connecting to the database. Because you didn't specify a user it is throwing an error because the "" blank user doesn't have permissions to view the database.
Do one of two things. Use this
PHP Code:
<?php
//attempt to connect to the MySQL server
$link = mysql_connect("localhost", "root", "rootpasswordorblankifroothasnopassword") or die(mysql_error());
mysql_select_db("bears") or die(mysql_error());
?>
Or login to the mysql console from the command prompt as root. And issue the following commands.
mysql>grant all on bears.* to usernameforbeardatabase@localhost identified by 'passwordforuserforbearsdatabase';
mysql>flush privileges;
PHP Code:
<?php
//attempt to connect to the MySQL server
$link = mysql_connect("localhost", "usernameforbeardatabase", "'passwordforuserforbearsdatabase'") or die(mysql_error());
mysql_select_db("bears") or die(mysql_error());
?>
|
|
|
11-08-2003, 05:07 PM
|
#9
|
|
member
Join Date: Feb 2002
Location: London
Posts: 2,442
|
Thanks tons freddycodes, i really appreciate your help -it is because of your help that i have managed to successfully create a database in php. I then checked it in the MySQL monitor...and it was there, not to mention the glorious messages that appeared in my browser, that is, "Database created" etc, etc.
Thank you for clarifying the matter, i was either putting "localhost" (and leaving it) or "localhost" followed by a username & password - although i am sure i tried both...but i was getting wrong, and now thanks to your help, i am getting it right. Thanks.
I was also very interested in the fact that i could go into the MySQL monitor and type in flush privileges and grant access.
You have certainly made clear a lot of issues i previously had. You've made my Saturday night in a productive one. I appreciate the fact that this must seem very simple for you, so thank you for your time and efforts.
I shall now continue fiddling around with php and in MySQL, i will try and create in the MySQL monitor and change with php etc.
Regards, snapple
Last edited by snapple; 11-08-2003 at 05:09 PM.
|
|
|
11-08-2003, 05:13 PM
|
#10
|
|
Master of Nothing
Join Date: Dec 2002
Location: San Diego, CA
Posts: 2,468
|
No problem.
I can tell you I have been down this road before. And its not easy to do the first time, or even the second or third. It is only easy to me now, because I have done it so damn many times.
Whats even funnier that in the grand scheme of things, mysql is a pretty weak RDBMS. And something like SQL Server is 100 times easier to administer.
Although MySQL 4.0 does have some sort of tool that makes this stuff much eaiser. I learned the hard way. On Linux with a command line. No MySQL monitor or anything, so I guess thats how I got good at it.
Anyways glad to get you on your way.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 08:28 PM.
///
|
|