LesterNotTheMolestor
LesterNotTheMolestor

Reputation: 141

Connect To the MySQL Database

I have a table on PHPmyamdmin, and when i try to select it with the following code:

 *<?php
    //connect.php
     session_start();
    $server = 'localhost';
    $username   = 'zimmer';
    $password   = '';
    $database   = 'csgopit';

    if(!mysql_connect($server, $username))
    {  
      exit('Error: could not establish database connection');
    }
    if(!mysql_select_db($database))
    {   
     exit('Error: could not select the database');
     }
    ?>*

i get this error when i open the page in localhost:

Error: could not select the database

In the database i have 4 tables, one of them is users and i have manually added the user "zimmer" to it, i'm using this to create and experiment forum in my learning experiences, also, using this same code as a "connect.php" if i use it as for example on another file simply by using this line

using connect.php or include connect.php

would it work in a login or would it load only the user zimmer? I started with PHP couple of days ago so sorry for the rookie question.

Upvotes: 2

Views: 56

Answers (1)

ChadH
ChadH

Reputation: 303

Try PDO.

$handler = '';
$server = 'localhost';
$username = 'zimmer';
$password = '';
$database = 'csgopit';

try {
    $this->handler = new PDO('mysql:host='. $server .';dbname='. $database . ';charset=utf8', $username, $password);
} catch(PDOException $e) {
    echo $e->getMessage();
    die();
}

But you also mentioned you added the user "zimmer" to the users table. This is different than your $username you pass to connect to the database.

Did you set up a username and password to a database?

Upvotes: 3

Related Questions