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


how do you connect to mysql?





MonkeyWrench
read tutorials but they dont tell you where to put the file in your webspace and what to call it as, and how you run the file?
Sappho
Hmm what file u mean? I connect to mysql using PHP most of the time which is simple:

<?php
$link = mysql_connect('mysql_server', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Put the php script file with the rest of ur web files. Smile
MonkeyWrench
any specific location, then do i execute the file?

also what do i name the file?
n0obie4life
Huh?

Are you making your own script or are you stuck on some installation where it wants you to connect to the mySQL Database.
n0xvb
First create your database and assign the database user and password. For example:
db name: MonkeyWrench_testdb
db user: MonkeyWrench_dbuser
db password: Secret
(You'll need to set up the tables/fields at this point too)

Create a file (you can call it anything you want, but for this example we'll use the name mysql.inc

In this file put this code in:
Code:
<?php
$connect = mysql_pconnect("localhost", "MonkeyWrench_dbuser", "Secret");
if (!$connect){
die(mysql_error());
}
mysql_select_db("MonkeyWrench_testdb", $connect);
?>

This will open a persistent connection to your database and will re-use the MySQL thread instead of creating a new one each page you open.
We put the database code above in it's own file so we lessen the risk of mistyping the info.

Now, in each page that you want to access your database, start out the script with an include to your file. (It needs to be one of the very first things you want to do):
Code:

<?php
include ('mysql.inc');
   ...
php code here!
   ...
?>

Here's an example of some code I wrote showing how it can be used with sessions:
Code:

<?php
session_start();

if ( @$_SESSION['logged'] != "yes" )
   {
   header("Location: login.php");
   exit();
   }

include ('db.php');
   ...
?>

You don't need to use mysql_close() on every page this way also.
MonkeyWrench
the database is

monkeywr_account

but i dont know how to find the database page, i am using php my admin now to edit the database but how do i put a password on it?
n0obie4life
In cPanel (I assume you are on there because of the joining datE),

you have to create a new user under mysql databses then add it into the database.

A tutorial can be found here - http://frihost.com/forums/vt-19192.html (just read the mysql for cpanel part Wink).
MonkeyWrench
no, how do i edit the my sql database without using php admin?
Related topics
simple php problem, please help...
php can't connect to mysql
error: Can't connect to local mysql server through socket
Connect to MySql database
mysql is allowed to connect from out of frihost host
Accessing mysql database thru software on local machine
the mysql is allowed connect from other website?
connect to different mysql databases site.
Mysql wont connect Hmmm
Can I connect to frihost mysql from my home computer?
Getting host to connect to my local mysql?
MySQL in Visual Basic?
Connect mySQL to Zend or Other
MySQL wont let me connect.
how to connect php with 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.