probleme de connection à la base
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: could not translate host name "port=" to address: Unknown host in C:\wamp\www\serializer.php on line 46
Warning: pg_query(): supplied argument is not a valid PostgreSQL link resource in C:\wamp\www\serializer.php on line 129
Warning: pg_last_error(): supplied argument is not a valid PostgreSQL link resource in C:\wamp\www\serializer.php on line 77
le fichier serializer.php :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| <?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;
}
}
?> |
et l'autre fichier
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 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'];
} |
Le but ici c'est de pouvoir sauvegarder dans la base les info parser du fichier xml
si vous avez une idée
merci