Help - Search - Members - Calendar
Full Version: How To Connect Sql
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
Franklin
Hi,

Please help me to edit following code


<?php
$mysql_hostname = " ";
$mysql_user = " ";
$mysql_password = " ";
$mysql_database = " ";
$prefix = " ";
?>

My site name is http://genericbazaar.99k.org
please help by post/reply or directly mail me at rx.franklin@gmail.com


Thanks.....
Earpy
The only thing you are doing in that code is defining the hostname, user, password, dastabase and prefix and storing them in variables. You are not actually doing anything with them. For this, you use the mysql_connect function stored in a variable;

CODE
$dbh=mysql_connect ("$mysql_hostname", "$mysql_user", "$mysql_password") or die ('Error connecting to MySQL ' . mysql_error());


All that code does is uses the values from the variables and connects to the server, otherwise it will output a MySQL error. To choose the database, you simply use the mysql_select_db function;

CODE
mysql_select_db("$mysql_database");


If the connection is successful, it will search for the database which is defined in the variable $mysql_database. It's as simple as that smile.gif

Your full code for connecting to the database will look something like this;

CODE
<?php
$mysql_hostname = " ";
$mysql_user = " ";
$mysql_password = " ";
$mysql_database = " ";
$prefix = " ";

$dbh=mysql_connect ("$mysql_hostname", "$mysql_user", "$mysql_password") or die ('Error connecting to MySQL ' . mysql_error());

mysql_select_db("$mysql_database");

?>



If you need any more help feel free to email me smile.gif (Don't add me on any IM)
Tom
Why quote variables? tongue.gif

Anyway, I recommend reading this: http://www.zymic.com/forum/Mysql-Database-...ement-t536.html

And this should work, after creating your database and assigning it a user with the necessary privileges:

CODE
<?php
$mysql_hostname = 'localhost';
$mysql_user = 'username';
$mysql_password = 'password';
$mysql_database = 'somedatabase';

$dbh = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die ('Error connecting to MySQL ' . mysql_error());

mysql_select_db($mysql_database);

?>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.