There are many MySQL functions in PHP. Perhaps you've taken a look at the official documentation and been intimidated by what looks like an endless task. But the good news is that to perform basic queries from within MySQL is very easy. Once you're comfortable with the basics, you can start to investigate the other functions, and you'll see that many of them are just duplicate ways of doing the same thing. Let's get started. The first thing to do is connect to the database.
All mysql functions begin with mysql_, so it comes as no surprise that the function to connect to MySQL is called mysql_connect. Let's assume you would connect to MySQL from the server you're running PHP with the following details:
* Username pee_wee
* Password let_me_in
From the command line, you'd type the following:
mysql -upee_wee -p
You'd enter the password once the prompt appears (you can also enter it directly on the command line, but get into the habit of doing it this way - it's more secure!). PHP can connect in the same way. The mysql_connect() function looks as follows:
resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]])
This function returns a resource which is a pointer to the database connection. It's also called a link identifier, or a database handle, and we'll use it in later functions.
Let's replace your connection details, and run this in a script:
$username = "pee_wee";
$password = "let_me_in";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
print "Connected to MySQL
";
// you're going to do lots more here soon
mysql_close($dbh);
?>
All going well, you should see "Connected to MySQL" when you run this script. If you can't connect to the server, make sure your password, username and hostname are correct, and that you've copied the script exactly.
The last line of the script contains another MySQL function - mysql_close(). Although this isn't strictly speaking necessary, PHP will automatically close the connection when the script ends, you should get into the habit of closing what you open. If you start developing more serious applications, or move to other, less tolerant languages, you will find the transition more difficult if you haven't learnt the basics well from the beginning.
Once you've connected, you're going to want to select a database to work with. Let's assume the database is called first_test. To start working in this database (the equivalent of typing USE first_test in MySQL), you'll need the mysql_select_db() function.
bool mysql_select_db ( string database_name [, resource link_identifier])
To change to the first_test database, add the mysql_select_db() function call to your script, as follows:
$username = "pee_wee";
$password = "let_me_in";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
print "Connected to MySQL
";
$selected = mysql_select_db("first_test",$dbh)
or die("Could not select first_test");
// you're going to do lots more here soon
mysql_close($dbh);
?>
for more info check here:
http://ir2.php.net/function.mysql-connect
2007-03-29 20:34:20
·
answer #1
·
answered by Amin Shariati 3
·
0⤊
0⤋
You will need to write a script inside of a php file that connects to the database.
Should look something like:
$username = "user";
$password = "pass";
$hostname = "localhost";
mysql_connect($hostname, $username, $password)
or die("Unable to connect");
echo "Connected to MySQL";
?>
where 'user' is your mysql username and 'pass' is your mysql password. Can get more in depth as well such as selecting the specific database, etc.
2007-03-29 20:37:51
·
answer #2
·
answered by Jen 2
·
0⤊
0⤋
If you've taken the advice of the other users, make sure that mysql is working. It could be a problem with your host. Ask them to make sure that mysql is working properly.
2007-03-31 23:45:02
·
answer #3
·
answered by b_4_he_cheats 1
·
0⤊
0⤋
from your control panel first create database user, password and a database
now specify these in your php code to access database
2007-03-29 21:19:19
·
answer #4
·
answered by Anonymous
·
0⤊
0⤋