Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP couldn't connect to database

Hi all,

I'm having some issues connecting my php to phpmyadmin. I'd like the user to fill in their email which is then saved into mysql on phpmyadmin.

In the index.php file I create an input form:

<!doctype html>
<html>
<head>
<form action="764/Insert.php" method="post">
<input class="input" type="email" id="mail" name="email" placeholder="Type your email">
<input type="submit" id="buttonsubmit">Submit</input>
</form>

This is then linked to inset.php file which should connect tot the database:

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$db_host = "databasename";

$conn = mysql_connect($servername, $username, $password, $db_host);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$db_selected = mysql_select_db(databasename, $link);

if (!$db_selected) {
    die('Can\'t use ' . databasename . ': ' . mysql_error());
}

$value = $_POST['email'];

$sql = "INSERT INTO databasename (email) VALUES ('$value')";

if (!mysql_query($sql)) {
    die('Error: ' . mysql_error());
}

mysql_close();
?>

My experience in using PHP is very limited. Hopefully somebody can help me solving this issue. Your help would be much appreciated.

Cheers, Mitchell

4 Answers

Geovanie Alvarez
Geovanie Alvarez
21,500 Points

databasename is supposed to be a variable??? try to change to $db_host to see what happen.

<?php
$db_selected = mysql_select_db(databasename, $link); // <--- here

if (!$db_selected) {
    die('Can\'t use ' . databasename . ': ' . mysql_error());// <--- here
}
?>

Hi Geovanie,

Thanks for your quick response. Unfortunately that's not working either. I still get an error "can't use databasename" Do you have any idea how to solve this?

Thanks

If you're using mysql, you would definitely use Geovanie's answer.

You might want to consider mysqli if you can, in which case you can combine all of your connection variables into one statement, i.e.:

$db_connect = mysqli_connect($host, $username, $password, $database_name, $port_number);

You have a little bit more security with sanitizing input, too, for example:

$first_name = mysqli_real_escape_string($db, $_POST['first_name']);

Then you would query with something like:

$query = "SELECT * FROM users;";
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
//and so forth

Just a suggestion, is all. I hope this helps!

Geovanie Alvarez
Geovanie Alvarez
21,500 Points

Here are different way of connection

<?php
// mysqli
$mysqli = new mysqli("localhost", "user", "password", "database");
$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);

// PDO
$pdo = new PDO('mysql:host=localhost;dbname=database', 'user', 'password');
$statement = $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo htmlentities($row['_message']);

// mysql
$c = mysql_connect("localhost", "user", "password");
mysql_select_db("database");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = mysql_fetch_assoc($result);
echo htmlentities($row['_message']);
?>