Page 1 of 2
[Challenge - Beginner] Database Connection
Posted: Mon Aug 24, 2009 10:02 am
by klevis miho
Make a database named "test". Connect to it using PHP. If the connection is succsessful display "Connection Successful" else display "Connection Failed".
Paste the code here, including dhe SQL code of the database creation.
Re: [Challenge - Beginner] Database Connection
Posted: Mon Aug 24, 2009 10:30 am
by klevis miho
SQL CODE:
PHP CODE:
Code: Select all
<?php
mysql_connect('localhost','root',''); // Connect to the database server
$selectDB = mysql_select_db('test'); //Select the database
if($selectDB) { //Perform the logic on what should be displayed
echo "Connection Successfuly";
} else {
echo "Connection Failed";
}
?>
Re: [Challenge - Beginner] Database Connection
Posted: Mon Aug 24, 2009 11:14 am
by Eran
is this a challenge or a beginners tutorial? I would think a challenge would require something you wouldn't find in the manual. Also, whats the point in the same guy posting the challenge and the solution?
Re: [Challenge - Beginner] Database Connection
Posted: Mon Aug 24, 2009 11:45 am
by jackpf
Also, you're treating a failure of the database selection as a connection error. The connection may be fine; you could just be selecting a non existent database.
You need error handling for both mysql_connect() and mysql_select_db() imo

Re: [Challenge - Beginner] Database Connection
Posted: Mon Aug 24, 2009 11:52 am
by klevis miho
It's a challenge for beginners.
I wanted to create a question and an answer as a maybe model on how we could make challenges.
Re: [Challenge - Beginner] Database Connection
Posted: Thu Sep 10, 2009 1:26 pm
by rtnylw
You failed!

Re: [Challenge - Beginner] Database Connection
Posted: Sun Oct 25, 2009 3:00 am
by klevis miho
Why?
Re: [Challenge - Beginner] Database Connection
Posted: Sun Dec 06, 2009 4:37 pm
by Payton
The SQL code:
Code: Select all
CREATE DATABASE `test`;
CREATE TABLE `test`.`challenge` (
`connection` TEXT NOT NULL
) ENGINE = InnoDB;
INSERT INTO `test`.`challenge` (
`connection`
)
VALUES (
'Connection successful!'
);
The PHP code:
Code: Select all
<?php
$a = mysql_connect("localhost","root","password"); //Connect to MySQL.
if (!$a)
{
//If the connection was unsuccessful, we get the following message.
die('Connection failed!');
}
/*The above code was simple enough, however below gets a bit more tricky.
We will connect to the "test" database and pull information from the "challenge" table.
The SELECT * FROM SQL statement will be put into a variable, because w3schools told me to.
And you know what they say, w3schools is always right!
But seriously I would trust them, they seem to know what they're doing.
And no I'm not just stealing this, I learned from w3schools and I'm just putting what I learned into effect.
Okay so back to the script.*/
else
{
//Select a database.
mysql_select_db("test", $a);
//Make $b select from the database challenge.
$b = mysql_query("SELECT * FROM challenge");
//Help $b do something.
$c = mysql_fetch_array($b);
//And now, let's show the world we mean serious freakin' business. That connection IS successful.
echo $c['connection'];
}
mysql_close($a); //Now let's close our connection, to be on the safe side.
//THE END.
?>
Hope I did well.

Re: [Challenge - Beginner] Database Connection
Posted: Fri Dec 18, 2009 9:44 am
by AbraCadaver
FWIW, I don't use mysql_select_db(). I just use:
Anyone else?
Re: [Challenge - Beginner] Database Connection
Posted: Fri Feb 19, 2010 8:16 pm
by jthermane24
AbraCadaver wrote:FWIW, I don't use mysql_connect(). I just use:
Anyone else?
you need to use myql_connect() to open the connection to the database, your code goes into the mysql_query() function
Re: [Challenge - Beginner] Database Connection
Posted: Fri Feb 19, 2010 8:28 pm
by Eran
That query will actually attempt to connect to the database if no connection was created previously. It will only work with a root user with no password though, so it's not really recommended for a production environment
Re: [Challenge - Beginner] Database Connection
Posted: Fri Feb 19, 2010 8:29 pm
by AbraCadaver
jthermane24 wrote:AbraCadaver wrote:FWIW, I don't use mysql_connect(). I just use:
Anyone else?
you need to use myql_connect() to open the connection to the database, your code goes into the mysql_query() function
I meant, I use mysql_connect(), just not mysql_select_db() normally. No need to use mysql_select_db() if you specify the db in the query. Bad copy/paste on my part. Or too much beer, hard to tell from two months ago

Re: [Challenge - Beginner] Database Connection
Posted: Fri Mar 19, 2010 6:54 am
by anshulsaini
klevis miho wrote:SQL CODE:
PHP CODE:
Code: Select all
<?php
mysql_connect('localhost','root',''); // Connect to the database server
$selectDB = mysql_select_db('test'); //Select the database
if($selectDB) { //Perform the logic on what should be displayed
echo "Connection Successfuly";
} else {
echo "Connection Failed";
}
?>
\\out put is connection successfully
Re: [Challenge - Beginner] Database Connection
Posted: Mon May 03, 2010 10:31 am
by anshulsaini
<?php
$con=mysql_connect("localhost","root","") or die(mysql_error());
$result=mysql_select_db("dbname",$con) or die(mysql_error());
if($result=TRUE)
{
echo "connection successfuly";
}
?>
Re: [Challenge - Beginner] Database Connection
Posted: Mon May 03, 2010 1:35 pm
by tdsrogers
<?php
$con = mysql_connect('localhost', 'username', 'password');
if ( ! $con )
die("Cannot connect to server");
$db = mysql_db_select('database');
if ( ! $db )
die("Cannot connect to db");
?>