Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/09/2007, 14:49
Avatar de sumolari
sumolari
 
Fecha de Ingreso: mayo-2006
Ubicación: localhost
Mensajes: 1.367
Antigüedad: 18 años
Puntos: 18
no logro crear un funcion que conecte a mi bd

Bien, explico mi problema:

Cada vez que hago alguna página dinámica cargo datos desde la base de datos (mysql), pero hay muchos queries que son iguales, así que he pensado que en lugar de poner el código de cada querie (por ejemplo) para obtener el nombre de la web, creo un archivo llamado core.php que contenga una función site_config que se conecte a la base de datos y extraiga el nombre de la web y la almacene en una variable llama sitename.

Cada página que use el nombre de la web (sitename) cargará core.php con un include y llamará a la función site_config(); y con ello podrá mostrar el nombre con tan sólo un par de líneas de código, con lo que es más claro el código y si detecto un fallo en los queries sólo he de actualizar un archivo.

Bien, me pongo a ello, el Dreamweaver es el encargado de crear los queries, y crea el siguiente código:
Código PHP:
<?php require_once('Connections/SCMS.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

mysql_select_db($database_SCMS$SCMS);
$query_site_config "SELECT * FROM site";
$site_config mysql_query($query_site_config$SCMS) or die(mysql_error());
$row_site_config mysql_fetch_assoc($site_config);
$totalRows_site_config mysql_num_rows($site_config);
/*La variable sitename */
$sitename $row_site_config['name'];

mysql_free_result($site_config);
?>
Ese código funciona a la perfección, me explico, si le pido que me muestre la variable sitename me lo muestra bien.

Así que creo la función site_config();
Código PHP:
<?php require_once('Connections/SCMS.php'); ?>
<?php
function site_config()
{
    if (!
function_exists("GetSQLValueString")) {
    function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
    {
      
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    
      
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
      switch (
$theType) {
        case 
"text":
          
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;    
        case 
"long":
        case 
"int":
          
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case 
"double":
          
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
          break;
        case 
"date":
          
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;
        case 
"defined":
          
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
          break;
      }
      return 
$theValue;
    }
    }
    
    
mysql_select_db($database_SCMS$SCMS);
    
$query_site_config "SELECT * FROM site";
    
$site_config mysql_query($query_site_config$SCMS) or die(mysql_error());
    
$row_site_config mysql_fetch_assoc($site_config);
    
$totalRows_site_config mysql_num_rows($site_config);
    
/*La variable sitename */
    
$sitename $row_site_config['name'];
    echo 
$sitename;
    
    
mysql_free_result($site_config);
}
?>
Y bien, ahora creo la página que cargará la función, en este caso test.php.
Código PHP:
<?php include('core.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php site_config(); ?></title>
</head>
<body>
<br /><div style="z-index:3" class="smallfont" align="center">LinkBacks Enabled by <a rel="nofollow" href="http://www.crawlability.com/vbseo/">vBSEO</a> 3.0.0</div><br /><div style="z-index:3" class="smallfont" align="center">LinkBacks Enabled by <a rel="nofollow" href="http://www.crawlability.com/vbseo/">vBSEO</a> 3.0.0</div></body>
</html>
Pero me da un error,
Cita:
Notice: Undefined variable: database_SCMS in C:\Servidor\www\Sumolari-CMS\core.php on line 34

Notice: Undefined variable: SCMS in C:\Servidor\www\Sumolari-CMS\core.php on line 34

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in C:\Servidor\www\Sumolari-CMS\core.php on line 34

Notice: Undefined variable: SCMS in C:\Servidor\www\Sumolari-CMS\core.php on line 36

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\Servidor\www\Sumolari-CMS\core.php on line 36
Y así estoy, si cargo la función en core.php ocurre lo mismo. El problema me lo dan las variables, estoy seguro, pero no se cómo arreglarlo. ¿Cómo puedo hacer que el querie vaya?