Re: Problema con Classe
| From: | Marcelo Sosa | Date: | Wed, 02 Nov 2005 19:37:25 +0000 |
| Subject: | Re: Problema con Classe | ||
| References: | 1 2 3 | Groups: | php.general.es |
| Request: | Send a blank email to general-es+get-56452@lists.php.net to get a copy of this message | ||
Perdon, va corregido...
--------------------------------------------------------------------
config.php
--------------------------------------------------------------------
<?php
define("DB_HOST", "localhost");
define("DB_NAME", "nombre_db");
define("DB_USER", "usuario");
define("DB_PASS", "password");
define("DB_TIPO", "MySQL");
?>
--------------------------------------------------------------------
class_db.php
--------------------------------------------------------------------
<?php require_once("config.php");?>
<?php
class DBs{
var $host;
var $user;
var $pass;
var $db_name;
var $db_tipo;
var $id_con = 0;
var $id_q = 0;
//********* inicializo las variables **************
function DBs(){
$this->host = DB_HOST;
$this->user = DB_USER;
$this->pass = DB_PASS;
$this->db_name = DB_NAME;
if(DB_TIPO != NULL && DB_TIPO != ''){
$this->db_tipo = DB_TIPO;
}else{
$this->db_tipo = "MySQL";
}
}
//************** conectar a la BD ***************
function conectar(){
switch($this->db_tipo){
case "MySQL":
$this->id_con = mysql_connect($this->host,$this->user,$this->pass);
if($this->id_con){
mysql_select_db($this->db_name, $this->id_con);
}
break;
case "MSSQL":
$this->id_con = mssql_connect($this->host,$this->user,$this->pass);
if($this->id_con){
mssql_select_db($this->db_name, $this->id_con);
}
break;
}
return $this->id_con;
}
//***************** aqui hago las consultas *************
function consultar($SQL = ""){
switch($this->db_tipo){
case "MySQL":
$this->id_q = mysql_query($SQL, $this->id_con);
break;
case "MSSQL":
$this->id_q = mssql_query($SQL, $this->id_con);
break;
}
return $this->id_q;
}
//************ cierro la conexion ********************
function close(){
switch($this->db_tipo){
case "MySQL":
mysql_close($this->id_con);
break;
case "MSSQL":
mssql_close($this->id_con);
break;
}
}
}
?>
--------------------------------------------------------------------
index.php
--------------------------------------------------------------------
<?php require_once('includes/class_db.php');?>
<?php
if(isset($_POST['send'])){
$nombre = ($_POST['txtNom'] != '') ? "'".$_POST['txtNom']."'" : "NULL";
$SQL = "INSERT INTO idiomas(nombre) VALUES(".$nombre.");";
$res = new DBs();
$res->conectar();
$r = $res->consultar($SQL);
$res->close();
if(!$r){
echo "tiro falso!!!";
die;
}
echo "\$r=".$r;die;
header('Location: '.$_SERVER['PHP_SELF'].'?e=0');
exit();
}
?>
<html>
<body>
<form name="form1" id="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input name="txtNom" type="text" id="txtNom" />
<input type="submit" name="Submit" value="Submit" />
<input name="send" type="hidden" id="send" value="1" />
</form>
</body>
</html>
--------------------------------------------------------------------------------
La clase que enviaste no tiene un método ::close() --y me marca un warning de "propiedad no definida" con ::$id_query (@65)Marcelo Sosa wrote:Bueno, desde ya agradecido por sus ayudas... Y les paso lo que corresponde a este problema asi lo vemos todos.[···]bueno, estas son las paginas en cuestion que dan el problema, no las numere asi, si desean reproducir esto no los moleste.[···]