Question: I am trying to connect my PHP code to MySQL safely with the following code:

```
<html>
    <?php
        $con = mysql_connect("localhost:3306","root","password");
        if(!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        else
        {
            echo "Connection established!";
        }
        mysql_close($con);
    ?>
</html>

```

But I keep getting the following error message:

> 
> Warning: mysql\_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061) in C:\xampp\htdocs\database\_connect.php on line 5
>  Could not connect: Can't connect to MySQL server on 'localhost' (10061)
> 
> 
> 

Here are the troubleshooting steps I took:

* Checked whether mysqld is running in Windows Task Manager Processes - it is
* Checked whether MySQL was running on the host by typing in Windows command prompt: "telnet 192.0.0.1 3306" and got the message "Could not open connection to the host, on port 3306: connection failed"
* Checked whether Windows Firewall was blocking MySQL - MySQL is an exception.

How do I get this code to work safely? And how do I check basic useful information about my MySQL like username?

Answer: 
Ok everybody, here is my very first attempts at connecting MySQL and PHP using PDO as suggested by Outis. It worked. 

```
<?php 
$user = root;
$pass = password;
try
{
    $dbh = new PDO('mysql:host = localhost; dbname=databaseName', $user,$pass);
    if($dbh)
    {
        print "Connected successfully";
    }
}
catch (PDOException $e)
{
    print "Error: " . $e->getMessage(). "<br/>";
    die();
}
?>

```

Here is my second attempt - this time I am trying to do a query

```
<?php 
$user = root;
$pass = password;
try
{
    $dbh = new PDO('mysql:host = localhost; dbname=databaseName', $user,$pass);
    foreach($dbh->query('SELECT * FROM tableName') as $row)
{
    print_r($row);
}
$dbh = null; 
}
catch (PDOException $e)
{
    print "Error: " . $e->getMessage(). "<br/>";
    die();
}
?>

```

After running this code, I get the following results:

> 
> Array ( [exo\_flowers\_ID] => 1 [0] => 1 [name] => Dendroseris Neriifolia [1] => Dendroseris Neriifolia [country] => Chile [2] => Chile [env\_workers\_id] => 1 [3] => 1 ) Array ( [exo\_flowers\_ID] => 2 [0] => 2 [name] => Snowdonia Hawkweed [1] => Snowdonia Hawkweed [country] => North Wales [2] => North Wales [env\_workers\_id] => 1 [3] => 1 )
> 
> 
> 

All I wanted was to display the contents of a row or column , but instead I got all of the contents plus all these brackets. How do I display only the contents of a table's row or column? 

And finally, how do I so a query like: SELECT \* FROM tableName WHERE 'columnName1' = 'somename' AND 'columnName2' = 'someothername'; ?
I solved the same problem "Can't connect to MySQL server on 'localhost'" with the following steps (Windows 7, XAMPP v.3.2.1 installed, php connection to MySQL didn’t work):

1. Open XAMPP Control Panel (my version is v.3.2.1). Press button "Shell" on the right side of the panel.
2. Window opens with a title:
"Administrator: XAMP for Windows – mysql –u root" and with the following text:

> 
> 
> ```
> Setting environment for using XAMPP for Windows
> comp@COMP c:\xampp
> 
> ```
> 
> 

3. Input: `#mysql`

Obtained answer: `Welcome to the MySQL monitor…`

Then input: `mysql>create database mdb;` 

Answer: `ERROR 1044 (42000): Access denied for user ‘’@’localhost’ to database ‘drawdb’`

Input: `#mysql –u root` 

Answer: `Welcome to the MySQL monitor…`

Input: `mysql>create database mdb;` 

Answer: `Query OK, 1 row affected (0.00 sec)`

Input: `mysql>grant all on mdb.* to user@localhost identified by ‘password’;` 

Answer: `Query OK, 0 rows affected (0.06 sec)`

I.e. I allowed access to the manually created database to the specified user. After that php connection to MySQL with user “user” and password “password” could be successfully created:

```
> $servername = "localhost";  
> $username = "user";  
> $password = "password"; 
> // Create connection 
> $conn = new mysqli($servername, $username, $password);

```