...

View Full Version : Connecting to a mySQL DB



Ramesiv
01-03-2006, 02:23 AM
When I try to connect to my database (myhomepageUsers) i get this error:

Warning: mysql_connect() [function.mysql-connect (http://www.php.net/function.mysql-connect)]: Unknown MySQL server host 'myhomepageUsers' (11001) in c:\program files\e-novative\WAMP\www\mySQL\index.php on line 5

with this code:
mysql_connect("myhomepageUsers");
I don't understand why this happens because the database exists. Can anybody please help me?


EDIT:
Also, how would i retreive data from a table in a database?

Velox Letum
01-03-2006, 02:25 AM
Read: mysql_connect() (http://www.php.net/function.mysql-connect) and mysql_select_db() (http://www.php.net/function.mysql-select-db)

cronic5
01-03-2006, 07:16 AM
Your connection should look something like this:



$connection = mysql_connect('localhost here','username here','password here');
if (!$connection) {
die( '<p>Access denied! Database Connection Error: ' . mysql_error() . '</p>');
}

Velox Letum
01-03-2006, 07:44 AM
You can simplify that a lot with a simple or.


<?php
$connection = mysql_connect('localhost','username','password') or die('Error connecting to database: ' . mysql_error());

// Also have to select the database.
mysql_select_db('database');
?>

Ramesiv
01-03-2006, 09:31 PM
Yes i know, i reframed my code to something similar to that, I can connect to the server, but I cannot connect to the db, this i do not understand, do i need to export it or somthing?

eksob
01-03-2006, 09:33 PM
post your connection code (without your u/p, or other personal data)

Ramesiv
01-03-2006, 09:39 PM
$user = "username";
$pass = "password";
$db = "homepage_users";
$link = mysql_connect ( "localhost", $user, $pass );
if ( ! $link ) {
die ( "Cound not connect ".mysql_error() );
}
print "<h2>Successful Connection to server</h2>";
mysql_select_db( $db, $link )
or die ( "Cound not connect to database." );
print "<h2>Successful Connection to database</h2>";
mysql_close( $link );

Prikid
01-03-2006, 09:43 PM
Try this way:


<?

$host = 'localhost';
$user = 'user';
$pass = 'password';
$db = 'db';

$m_connect = @mysql_connect($host,$user,$pass) or die ("Error while trying to connect to mysql: " . mysql_error());

$select_my_db = @mysql_select_db($db,$m_connect) or die ("Error selecting database: " . mysql_error());

?>

Ramesiv
01-03-2006, 09:52 PM
~ Never mind, I've got it sorted, thank you all very much for your help ~



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum