How to Connect to MySQL Database using PHP.
We were migrating from 5.0.45 to 5.0.67 version of MySQL in OpenSolaris.As part of this migration project had to test the working of PHP with MySQL database .
Here are the steps outlined:
- Build the MYSQL 5.0.67 DB and the SVR4 packages namely SUNWmysql5r ,SUNWmysql5u and SUNWmysql5test .
- Install the new MySQL packages on an OpenSolaris installed system.
- Make sure you have the PHP packagesSUNWphp524root,SUNWphp524usr,SUNWphp524core SUNWphp524-mysql and SUNWphp524-mysql-root already installed.These PHP packages form the base for the PHP Interpreter.
- Create a default root user in MySQL DB and connect to the database
#/usr/mysql/bin/mysqladmin -u root -password mysql
#/usr/mysql/bin/mysql -uroot -pmysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 5.0.67 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use mysql;
Database changed
mysql> CREATE TABLE `cars` (
-> `id` int UNIQUE NOT NULL,
-> `name` varchar(40),
-> `year` varchar(50),
-> PRIMARY KEY(id)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> INSERT INTO cars VALUES(1,'Mercedes','2000');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO cars VALUES(2,'BMW','2004');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO cars VALUES(3,'Audi','2001');
Query OK, 1 row affected (0.00 sec)
mysql> select id ,name,year from cars;
+----+----------+------+
| id | name | year |
+----+----------+------+
| 1 | Mercedes | 2000 |
| 2 | BMW | 2004 |
| 3 | Audi | 2001 |
+----+----------+------+
3 rows in set (0.00 sec)
mysql>
5.Write a sample php script as shown below called "test.php"
<?php
$username = "root";
$password = "mysql";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL \n";
//select a database to work with
$selected = mysql_select_db("mysql",$dbhandle)
or die("Could not select mysql:" .mysql_error());
//execute the SQL query and return records
$result = mysql_query("select id,name,year from cars");
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
$row=mysql_fetch_row($result);
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
echo $row[0];
echo $row[1];
echo $row[2];
?>
/usr/php5/bin/php -f /tmp/test.php7. The Displayed Output should look like this
[root@sa64-v20zc-blr03]/: /usr/php5/bin/php -f /tmp/test.php Connected to MySQL 1Mercedes2000
great job. best of luck
Posted by anrak nehru on September 22, 2008 at 11:38 PM PDT #
hrgrsdfgvsgsdg
Posted by keerthi on November 10, 2008 at 01:43 AM PST #
do you have some code regarding how to create a student id using year?
ex. 2009-1
Posted by Alex Rabago on February 17, 2009 at 12:58 AM PST #
hello,
please give me solution of this error,
"1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '12'', '''', ''card1.php'', ''BOX_HEADING_CARD1'', ''yes'', ''right'', 10, now(),' at line 1
INSERT INTO infobox_configuration VALUES (''12'', '''', ''card1.php'', ''BOX_HEADING_CARD1'', ''yes'', ''right'', 10, now(), now(), ''Cards We Accept'', ''infobox'', ''#FFFFFF'')
[TEP STOP]
Posted by chandresh on February 18, 2009 at 10:30 PM PST #
cxcxc
Posted by 122.163.25.38 on March 19, 2009 at 02:26 AM PDT #