4

This is my connect page to sql server

<?php
$user="test";
$parola="test1";
$bazadate="test";  //Numele bazei de date sql//
$host="localhost"; //In general se foloseste localhost//
mysql_connect($host,$user,$parola);
@mysql_select_db($bazadate) or die( "Nu ma pot conecta la baza ta de date! <br/ > 
Sigur ai completat userul, parola si numele bazei de date?");
?>

I've tried the down code, but i receive "No database selected":

<?php
/* SQL connect PDO */
$dsn = 'mysql:dbname=test;host=localhost';
$user = 'test';
$password = 'test1';

try {
$dbh = new PDO($dsn, $user, $password);

} catch (PDOException $e) {
echo 'Conectare SQL nereusita: ' . $e->getMessage();
}
?>

PDO class kills me completley...

Domuta Marcel
  • 509
  • 5
  • 16

2 Answers2

2

In

$dsn = 'mysql:dbname=test;host=localhost';

the parameters are in the wrong order

The order in documentation is

$dbn = new PDO('mysql:host=$host;dbname=$bazadatet', $user, $parola);

Where

$user="test";
$parola="test1";    //table
$bazadate="test";  //Numele bazei de date sql//
$host="localhost"; //In general se foloseste localhost//

Change the order to host & dbname

david strachan
  • 7,174
  • 2
  • 23
  • 33
-2

http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html

Taken from: Are there good tutorials on how to use PDO?

Community
  • 1
  • 1
John
  • 87
  • 4