PDA

View Full Version : php mysql 1st time connection


djbluem
12-2-02, 09:56 AM
Hello All,
I have just setup my account and tables via phpmyadmin.

I am running a script index.php which has my hostname, username and pw for the mysql db.

I cannot seem to get it to connect. I am accessing the variables via a include file db.inc. I have echo'd the variables to a page and it's definitely there...
All the files are in my htdocs folder. They are all chmod 755.

Am i missing something basic? Does mysql have to be called in a certain dir on powweb?

Thanks in advance for any help!
-Mike

HalfaBee
12-2-02, 05:36 PM
To connect to mysql you basically have to do this


$link = mysql_connect( "server.powweb.com","mysql-username","mysql-passwd" );
if( $link )
{
if( !mysql_select_db( "database namef" ) ) die( "Wrong db");

echo "Do mysql stuff here";
}
else
die( "Could not connect to DB" );


}

If you include the connection part to protect name/passwd make sure it is call db.inc.php. This causes the info to be parsed and invisible to hackers.
If you just call it db.inc and a hacker finds it it just gets sent as plain text.

If you can't get your script to work post it and someone will tell you what is wrong.

HalfaBee

Atomic-Design
12-3-02, 11:27 PM
This works for me:


$user = "YOUR USERNAME";
$pass = "YOUR PASSWORD";
$db = "DATABASE HERE";

$link = mysql_connect("localhost",$user,$pass);

if (!$link)
die("Could not connect to MySQL");

mysql_select_db($db,$link)
or die("Could not select database: ".mysql_error());

// Following these comments add all your MySQL stuff


Whichever looks more understanding to you.

HalfaBee
12-4-02, 02:24 AM
Your probably right.

It's the old C programmer in me not wanting to let go. :)

I always forget to use the 'or' statement at the end.

The other reason for not ending the script that way is that you should really close html tags and things sometimes, but if theres an error who really cares.

HalfaBee