How to connect database?

cathyf

New Member
Messages
2
Reaction score
0
Points
0
How to connect and create database at x10hosting?Please I need your suggestions and comments for my thesis project...Thanks!
 

rajat44

Banned
Messages
24
Reaction score
0
Points
0
<?

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name


//Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");

?>

thats basically it, to connect to the database.. :)
 

vv.bbcc19

Community Advocate
Community Support
Messages
1,524
Reaction score
92
Points
48
mysql_connect("$host", "$username", "$password")or
die("cannot connect to server");
will this echo "cannot connect to server" "
 

cbirks

New Member
Messages
2
Reaction score
0
Points
0
the mysql_connect function connects to the server, mysql_select_db chooses the database and the die("cannot connect to server"); says that if there's an error connecting then it will display the message "cannot connect to server".

Thanks, Chris :)
 

jbitkill95

New Member
Messages
5
Reaction score
0
Points
0
My way of doing it (that's what she said):

Put this in the page where you want to query, ex. index.php:
PHP:
<?php require_once('mysqlconnect.php'); ?>
<?php mysql_select_db("your_db", $mysql);
$mysql_query = sprintf("SQL SYNTAX HERE");
$mysql_queried = mysql_query($mysql_query,$mysql) or die(mysql_error());
?>

Then put this in mysqlconnect.php and change it to 644 permissions (thanks rajat44)
PHP:
<?php

$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 

//Connect to server and select database.
$mysql = mysql_connect($host, $username, $password)or die("cannot connect to server"); 

?>

This way, the user/pass is only readable by the server. Also, you can use the "require_once" for every page, instead of copying user and pass to every page. Also, it's site-wide so when you change the MySQL server, you can change 1 file, and it's site-wide.

Hope this helps,
Josh
 
Top