FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


how to connect php with mysql?





cyberbuddy
any body could help me ..how to connect PHP with mysql ......iam using mysql 5.1 and familar with java database connectivity.i tried with php but my efforts went in vain!!


thank u
SnowFlower
Code:

<?php
$DB_USERNAME  = "put_your_user_name_here" // Username
$DB_PASSWORD = "put_your_pass_here" // Password

// Attempt a connection with your credentials
// This assumes the server address is "localhost", which it is in 99% of all cases;)
$link = mysql_connect('localhost', $DB_USERNAME, $DB_PASSWORD);
if (!$link) {
    die('Failed to connect, error: ' . mysql_error());
}

echo "DB Successfully connected"

// do some things here

// close the connection (optional for non persistent connections, but good practice)
mysql_close($link);
?>


you can send queries to the database with "mysql_query(...)" and fetch results with "mysql_fetch_array(...)" , "mysql_fetch_assoc(...)" etc.

The easiest way would have been to just refer to the (excellent) PHP documentation, its all in there with nice examples, really easy to understand Wink
BillysProgrammer
Heres another way..

Code:

define('SERVER', 'localhost');
define('DB_USER', 'your_user_name');
define('DB_PASS', 'your_password');
define('DB', 'database_name');

class MYSQL {
   var $connection;   // Used for the connection to MYSQL

       /* Class Constructor */
   function __construct() {
      $this->connection = mysql_connect(SERVER, DB_USER, DB_PASS) or die(mysql_error());
      mysql_select_db = mysql_select_db(DB) or die(mysql_error());
   }
}

$database = new MYSQL;


Though, this is only for OOP, and should only be used if there is going to be a lot more then just that.
rvec
even then I wouldn't use it like that. What's the point of having a mysql class if you use static variables.

And also the die, wouldn't it be better to catch it in a nicer way?

If you want to use classes do it the oop way and not the procedural way.
Related topics
PHP, MySQL...
Build an online dictionary by PHP/MySQL
ASP +PHP+MySQL Tutorials
what is your php mysql apache installer package
Need some php/mysql dlls
The Basics (php, mysql etc)
php can't connect to mysql
What is useful way in this php+mysql problem?
PHP/Mysql - beginner!
Connect to MySql database
A very good PHP MySQL Tutorial
Setup php,mysql and others...
php, mysql and iis5
php + mysql
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.