[Challenge - Beginner] Database Connection
Moderator: General Moderators
-
klevis miho
- Forum Contributor
- Posts: 413
- Joined: Wed Oct 29, 2008 2:59 pm
- Location: Albania
- Contact:
[Challenge - Beginner] Database Connection
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.
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
SQL CODE:
PHP CODE:
Code: Select all
"CREATE DATABASE test;"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
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
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
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
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.
I wanted to create a question and an answer as a maybe model on how we could make challenges.
Re: [Challenge - Beginner] Database Connection
You failed! 
-
klevis miho
- Forum Contributor
- Posts: 413
- Joined: Wed Oct 29, 2008 2:59 pm
- Location: Albania
- Contact:
Re: [Challenge - Beginner] Database Connection
The SQL code:
The PHP code:
Hope I did well. 
Code: Select all
CREATE DATABASE `test`;
CREATE TABLE `test`.`challenge` (
`connection` TEXT NOT NULL
) ENGINE = InnoDB;
INSERT INTO `test`.`challenge` (
`connection`
)
VALUES (
'Connection successful!'
);
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.
?>- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: [Challenge - Beginner] Database Connection
FWIW, I don't use mysql_select_db(). I just use:
Anyone else?
Code: Select all
SELECT * FROM `dbname.tablename`
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.
- jthermane24
- Forum Newbie
- Posts: 16
- Joined: Wed Feb 17, 2010 8:15 pm
- Location: New Jersey
Re: [Challenge - Beginner] Database Connection
you need to use myql_connect() to open the connection to the database, your code goes into the mysql_query() functionAbraCadaver wrote:FWIW, I don't use mysql_connect(). I just use:
Anyone else?Code: Select all
SELECT * FROM `dbname.tablename`
Re: [Challenge - Beginner] Database Connection
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
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: [Challenge - Beginner] Database Connection
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 agojthermane24 wrote:you need to use myql_connect() to open the connection to the database, your code goes into the mysql_query() functionAbraCadaver wrote:FWIW, I don't use mysql_connect(). I just use:
Anyone else?Code: Select all
SELECT * FROM `dbname.tablename`
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
\\out put is connection successfullyklevis miho wrote:SQL CODE:PHP CODE:Code: Select all
"CREATE DATABASE test;"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"; } ?>
-
anshulsaini
- Forum Newbie
- Posts: 3
- Joined: Fri Mar 19, 2010 6:46 am
Re: [Challenge - Beginner] Database Connection
<?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";
}
?>
$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
<?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");
?>
$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");
?>