[Challenge - Beginner] Database Connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

[Challenge - Beginner] Database Connection

Post 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.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: [Challenge - Beginner] Database Connection

Post by klevis miho »

SQL CODE:

Code: Select all

"CREATE DATABASE test;"
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";
}
?>
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Challenge - Beginner] Database Connection

Post 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?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [Challenge - Beginner] Database Connection

Post 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 ;)
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: [Challenge - Beginner] Database Connection

Post 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.
rtnylw
Forum Newbie
Posts: 4
Joined: Thu Sep 10, 2009 3:00 am

Re: [Challenge - Beginner] Database Connection

Post by rtnylw »

You failed! :idea:
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: [Challenge - Beginner] Database Connection

Post by klevis miho »

Why?
User avatar
Payton
Forum Commoner
Posts: 33
Joined: Sun Dec 06, 2009 4:03 pm

Re: [Challenge - Beginner] Database Connection

Post 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. :mrgreen:
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: [Challenge - Beginner] Database Connection

Post by AbraCadaver »

FWIW, I don't use mysql_select_db(). I just use:

Code: Select all

SELECT * FROM `dbname.tablename`
Anyone else?
Last edited by AbraCadaver on Fri Feb 19, 2010 8:40 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
jthermane24
Forum Newbie
Posts: 16
Joined: Wed Feb 17, 2010 8:15 pm
Location: New Jersey

Re: [Challenge - Beginner] Database Connection

Post by jthermane24 »

AbraCadaver wrote:FWIW, I don't use mysql_connect(). I just use:

Code: Select all

SELECT * FROM `dbname.tablename`
Anyone else?
you need to use myql_connect() to open the connection to the database, your code goes into the mysql_query() function
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Challenge - Beginner] Database Connection

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: [Challenge - Beginner] Database Connection

Post by AbraCadaver »

jthermane24 wrote:
AbraCadaver wrote:FWIW, I don't use mysql_connect(). I just use:

Code: Select all

SELECT * FROM `dbname.tablename`
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 :-)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
anshulsaini
Forum Newbie
Posts: 3
Joined: Fri Mar 19, 2010 6:46 am

Re: [Challenge - Beginner] Database Connection

Post by anshulsaini »

klevis miho wrote:SQL CODE:

Code: Select all

"CREATE DATABASE test;"
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
anshulsaini
Forum Newbie
Posts: 3
Joined: Fri Mar 19, 2010 6:46 am

Re: [Challenge - Beginner] Database Connection

Post 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";
}
?>
tdsrogers
Forum Newbie
Posts: 11
Joined: Sat Mar 06, 2010 12:44 pm

Re: [Challenge - Beginner] Database Connection

Post 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");
?>
Post Reply