View Full Version : Link identifier when connecting to Database
snapple
11-07-2003, 02:21 PM
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 ?
<?
$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 :)
CyanBlue
11-07-2003, 03:07 PM
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... :D
The manual has this explanation...mysql_select_db() sets the current active database on the server that's associated with the specified link identifier.and this 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...mysql_select_db("snappleflash", 1 or "1")should be something like this???mysql_select_db("snappleflash", $link)???
snapple
11-07-2003, 03:29 PM
Thank you very much for your quick response.
Yes, i did initially try the...
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.
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 :)
freddycodes
11-07-2003, 03:32 PM
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.
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.
snapple
11-07-2003, 03:41 PM
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 :)
CyanBlue
11-07-2003, 03:45 PM
Ah... Power nap... :D 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... ;)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... :(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... ;)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... ;)
snapple
11-08-2003, 03:03 PM
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:
<?
//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 :)
freddycodes
11-08-2003, 05:18 PM
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
//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
//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());
?>
snapple
11-08-2003, 06:07 PM
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 :)
freddycodes
11-08-2003, 06:13 PM
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.
CyanBlue
11-08-2003, 06:31 PM
Totally agreed on what freddycodes just said... :Di also have my Steve Webster book by my side.You are wrong... I'd rather print out freddycodes' whole thread and leave them by my side... I think that's more valuable... (No offence to the author of the book though...)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.I think that's very true... I think I used to do that, but I sorta got more lazier by the time when I get my foot into the MySQL/PHP pool... I use phpMyAdmin and I just copy and paste codes WITHOUT thinking... Shame on you, Jason... :(
BTW, snapple... If you want to get spoiled like me, you can get the phpMyAdmin from this page... :D
http://www.phpmyadmin.net/
I am not going to hijack the thread, but let me ask a quick question since freddycodes mentioned it... :D
I have used MSSQL a couple of times with CF before, and there was something called Stored Procedure in MSSQL... Is there anything equivalent to that in MySQL???
freddycodes
11-08-2003, 07:23 PM
No MySQL does not support Stored Procedures.
Which is of course a shame. In say SQL Server Stored procedures are compiled so of course they are much faster than inline sql like you would do with MySQL.
Other benefits include the ability to change field names, and database strcuture with minimal impact on business logic, ie. PHP code.
MySQL also has no support for Foreign key constraints which is another shame.
An interesting look into some of the major problems with MySQL.
http://www.sitepointforums.com/showthread.php?s=&postid=109868#post109868
snapple
11-08-2003, 07:30 PM
An interesting thread, as i saw my scroll bar decreasing in size as the page loaded i thought "I'll make myself a cup of tea". So i got comfortable and read it.
Although i am not as 'up on the in's and out's' of MySQL (such as stored procedures) i did enjoy reading it, and i did find it very informative.
Thanks.
Regards, snapple :)
CyanBlue
11-08-2003, 08:55 PM
Wow... Very informative thread it is... Thank you freddycodes... ;)
Of course, I hardly understand what is in that thread... This line is very touchy thoug...Object Oriented design is not going to save the world and cure cancer. It is not the programmers “silver bullet” to shave 10 years of off the product life cycle.
...
...
Too many people are anti-this and anti-that so some good products get the shaft. Research what is best for your particular use, then perhaps use the open-source litmus test AFTER you narrow down your choices.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.