Vous n'êtes pas identifié(e).
Pages : 1
bonjour,
j'obtient cet erreur quand j'essaie de se connecter à une base postgres
Warning: pg_connect() [function.pg-connect]: Unable to connect to PostgreSQL server: FATAL: aucun nom d'utilisateur PostgreSQL n'a été spécifié dans le paquet de démarrage in C:\wamp\www\evostreamclass\serializer.php on line 46
Warning: pg_query(): supplied argument is not a valid PostgreSQL link resource in C:\wamp\www\evostreamclass\serializer.php on line 129
Warning: pg_last_error(): supplied argument is not a valid PostgreSQL link resource in C:\wamp\www\evostreamclass\serializer.php on line 77
<?php
/*
* Class for parsing XML file
* Version 18-01-2010
*/
class Serializer
{
// Variable for XML object
var $urlXML;
var $host;
var $username;
var $password;
var $port;
var $dbname;
var $error = null;
var $dbconnect;
var $query;
var $result;
var $persistent;
//Constructor
public function Serializer($Db="mabase",$Host="127.0.0.1",$PgPort=5432,$User="postgres",$pass="admin",$persist=0)
{
$this->dbname=$Db;
$this->host=$Host;
$this->port=$PgPort;
$this->username=$User;
$this->password=$pass;
$this->persistent=$persist;
$this->Connect();
}
//method for connecting into the database
public function Connect ()
{
$connect="host='".$this->host."' port='".$this->port."' dbname='".$this->dbname."' user='".$this->username."'";
if (!empty($this->password))
$connect.=" password=".$this->password;
if ($this->persistent)
$this->dbconnect=pg_pconnect($connect);
else
$this->dbconnect=pg_connect($connect);
if (!$this->dbconnect)
$this->error="cannot connect to database ".$this->dbname;
}
public function ExecuteQuery($sql)
{
$this->query = new Query($sql,$this->dbconnect);
$this->result = $this->query->Execute();
$this->error = $this->query->Error();
return $this->result;
}
public function FetchResult (&$row, $assoc=PGSQL_BOTH)
{
if (!$this->error)
{
@$arr=pg_fetch_array ($this->result, $row, $assoc);
return $arr;
}
else
{
echo "An error occured, $this->error";
return null;
}
}
public function Error ()
{
if (version_compare(phpversion(), "4.2.0", "ge")>0)
$this->error=pg_last_error ($this->dbconnect);
return $this->error;
}
//Methode for parsin the element
public function parser($urlXMLtoparse)
{
//Passed the urlXML to the object
$this->urlXML = $urlXMLtoparse;
//Charging data of the XML
$xml = simplexml_load_file($this->urlXML);
// if xml charger
if($xml)
{
// for debugging
/*echo "<table border='1'>";
foreach($xml as $valeur)
{
echo "<tr>";
foreach($valeur as $cle=>$val)
{
echo "<td>".$val."</td>";
}
echo "</tr>";
}
echo "</table>";*/
return $xml;
}
}
}
class Query
{
var $_sql;
var $_result;
var $_field;
var $_dbconnection;
var $_error;
public function Query($sql,$dbconnection)
{
$this->_sql = $sql;
$this->_dbconnection = $dbconnection;
}
public function Execute()
{
$this->_result = pg_query($this->_dbconnection,$this->_sql);
$this->_error = pg_result_error($this->_result);
return $this->_result;
}
public function Error()
{
return $this->_error;
}
}
?>
require_once("serializer.php");
$parseur = new Serializer($dbname,$host,$port,$username,$password,$persistent);
$retourParser = $parseur->parser("monfichier.xml");
//la requete sql
$sql = "select * from clients";
if(!$parseur->ExecuteQuery($sql))
{
die ($parseur->Error());
}
for ($row=0; $result=$parseur->FetchResult($row, PGSQL_BOTH); $row++)
{
echo $result['firstname'];
}
PS :
j'ai installé en local postgresql 8.4
j'ai mis en place aussi phpgadmin pour une interface convivial comme phpmyadmin
merci d'avance
Hors ligne
À priori, je dirais que le $username de :
$parseur = new Serializer($dbname,$host,$port,$username,$password,$persistent);
est une chaîne vide.
Un affichage de $connect dans la méthode Connect() en apporterait la preuve.
Guillaume.
Hors ligne
Que vaut exactement la variable $connect ?
Marc.
Hors ligne
Oups
Marc.
Hors ligne
merci beaucoup pour votre réponse
j'ai du modifier comme ça le code car il ne trouve pas les paramètres dans la méthode ou constructeur serialier
class Serializer
{
// Variable for XML object
var $urlXML;
var $host;
var $username;
var $password;
var $port;
var $dbname;
var $error = null;
var $dbconnect;
var $query;
var $result;
var $persistent;
//Constructor
public function __construct()
{
$this->Connect();
}
//method for connecting into the database
public function Connect ($Db="evostream_logs",$Host="127.0.0.1",$PgPort=5432,$User="postgres",$pass="admin",$persist=0)
{
// Passed the variable to objet
$this->dbname=$Db;
$this->host=$Host;
$this->port=$PgPort;
$this->username=$User;
$this->password=$pass;
$this->persistent=$persist;
$connect="host='".$this->host."' port='".$this->port."' dbname='".$this->dbname."' user='".$this->username."'";
// if password is not empty
if (!empty($this->password))
{
$connect.=" password=".$this->password;
}
//Connection persistent
if ($this->persistent)
{
$this->dbconnect=pg_pconnect($connect);
}else{
$this->dbconnect=pg_connect($connect);
}
//if not connected
if (!$this->dbconnect)
{
$this->error="cannot connect to database ".$this->dbname;
}
}
Hors ligne
Pages : 1