|
Home >> FAQs/Tutorials >> MySQL Tutorials >> Index
MySQL FAQs - PHP Connections and Query Execution
By: FYIcenter.com
Part:
1
2
3
4
5
6
(Continued from previous part...)
How To Connect to a MySQL Sever with Default Port Number?
If you want to connect a MySQL server with default port number,
you can use the "mysql_connect($server)" function, where $server is the host name
or IP address where the MySQL server is running. If successful, mysql_connect() will
return a connection object for you to allow you to run other MySQL API functions.
The tutorial exercise below shows you a simple connection call to a MySQL server
on the "localhost":
<?php
$con = mysql_connect('localhost');
if (!$con) {
print("There is a problem with MySQL connection.\n");
} else {
print("The MySQL connection object is ready.\n");
mysql_close($con);
}
?>
But this will not work, if your server is running a non-default port number, 3306.
In that case, It will give this output:
PHP Warning: mysql_connect(): Can't connect to MySQL server
on 'localhost' (10061) in fyi_center.php on line 2
There is a problem with the MySQL connection.
How To Connect to a MySQL Sever with a Port Number?
If you want to connect a MySQL server with a non-default port number,
you need to use the "mysql_connect($server)" function with $server in the format of
"hostName:portNubmber". The tutorial exercise below shows you how to connect to localhost
at port number 8888:
<?php
$con = mysql_connect('localhost:8888');
if (!$con) {
print("There is a problem with MySQL connection.\n");
} else {
print("The MySQL connection object is ready.\n");
mysql_close($con);
}
?>
But this will not work, even you got the right port number, if your server requires
user account authentication. In that case, It will give this output:
PHP Warning: mysql_connect(): Access denied for user
'ODBC'@'localhost' (using password: NO) in fyi_cener.php
on line 2
There is a problem with the MySQL connection.
How To Connect to MySQL Severs with User Accounts?
If you want to connect a MySQL server with user account name and password,
you need to call mysql_connect() with more parameters like this:
$con = mysql_connect($server, $username, $password);
If your MySQL server is provided by your Internet service company, you need to
ask them what is the server hostname, port number, user account name and password.
The following tutorial exercise shows you how to connect to a MySQL server,
on host "localhost", at port "8888", with user account "dev", and with password "iyf":
<?php
$con = mysql_connect('localhost:8888', 'dev', 'iyf');
if (!$con) {
print("There is a problem with MySQL connection.\n");
} else {
print("The MySQL connection object is ready.\n");
mysql_close($con);
}
?>
If all parameters are correct, you should get a good connection object
as shown in the following output:
The MySQL connection object is ready.
(Continued on next part...)
Part:
1
2
3
4
5
6
|