Foros del Web » Programando para Internet » PHP »

Require_once me produce "undefined variable"

Estas en el tema de Require_once me produce "undefined variable" en el foro de PHP en Foros del Web. Hola buenas, pues tengo un problemilla y es que no me reconoce la conexión a la base de datos en una determinada función y no ...
  #1 (permalink)  
Antiguo 04/07/2016, 16:48
 
Fecha de Ingreso: mayo-2014
Mensajes: 15
Antigüedad: 9 años, 10 meses
Puntos: 0
Require_once me produce "undefined variable"

Hola buenas, pues tengo un problemilla y es que no me reconoce la conexión a la base de datos en una determinada función y no sé por qué es. Ilustro para ver si alguien me puede dar una ayudita:

Esto es lo que tengo en "/Connections/conexionidiomas.php":

Código PHP:
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_conexionidiomas "p:localhost:3307";
$database_conexionidiomas "idiomasbd";
$username_conexionidiomas "root";
$password_conexionidiomas "asdasdf";
$conexionidiomas mysqli_connect($hostname_conexionidiomas$username_conexionidiomas$password_conexionidiomas$database_conexionidiomas); 
y esto lo que tengo en "preguntas-frecuentes.php":

Código:
<?php require_once('Connections/conexionidiomas.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($conexionidiomas, 
  								$theValue) : mysqli_escape_string($conexionidiomas,$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;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO tblfrecuentes (strPregunta,fchFecha) VALUES (%s,NOW())",GetSQLValueString($_POST['strPregunta'], "text"));

    $Result1 = mysqli_query($conexionidiomas,$insertSQL) or die(mysqli_error($conexionidiomas));
}
?>
Y estos son los problemas que me dan:

1) Notice: Undefined variable: conexionidiomas in C:\wamp64\www\idiomas\preguntas-frecuentes.php on line 10

2) Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\wamp64\www\idiomas\preguntas-frecuentes.php on line 11

Alguien que me ilumine?

Saludos!
  #2 (permalink)  
Antiguo 04/07/2016, 17:07
Avatar de chulifo  
Fecha de Ingreso: abril-2009
Ubicación: perdido en codigos del PHP, pero aprendo rapido!
Mensajes: 524
Antigüedad: 15 años
Puntos: 18
Respuesta: Require_once me produce "undefined variable"

Pregunta, el primer fichero tienes los datos contenidos en <?php ... ?>
__________________
Solo soy un simple fanático que mata el tiempo de la mejor manera.
  #3 (permalink)  
Antiguo 04/07/2016, 17:10
 
Fecha de Ingreso: mayo-2014
Mensajes: 15
Antigüedad: 9 años, 10 meses
Puntos: 0
Respuesta: Require_once me produce "undefined variable"

Cita:
Iniciado por chulifo Ver Mensaje
Pregunta, el primer fichero tienes los datos contenidos en <?php ... ?>
Sí, perdón, se me pasó incluirlo ya que la totalidad del fichero es php, pero sí están contenidos
  #4 (permalink)  
Antiguo 04/07/2016, 17:16
Avatar de chulifo  
Fecha de Ingreso: abril-2009
Ubicación: perdido en codigos del PHP, pero aprendo rapido!
Mensajes: 524
Antigüedad: 15 años
Puntos: 18
Respuesta: Require_once me produce "undefined variable"

Cita:
Iniciado por sevillano665 Ver Mensaje
Sí, perdón, se me pasó incluirlo ya que la totalidad del fichero es php, pero sí están contenidos
Siguiente pregunta, comprobaste que se realizo la conexion?
Código PHP:
Ver original
  1. if (!$conexionidiomas ) {
  2.     echo "Error: No se pudo conectar a MySQL." . PHP_EOL;
  3.     echo "errno de depuración: " . mysqli_connect_errno() . PHP_EOL;
  4.     echo "error de depuración: " . mysqli_connect_error() . PHP_EOL;
  5.     exit;
  6. }
__________________
Solo soy un simple fanático que mata el tiempo de la mejor manera.
  #5 (permalink)  
Antiguo 04/07/2016, 17:25
 
Fecha de Ingreso: mayo-2014
Mensajes: 15
Antigüedad: 9 años, 10 meses
Puntos: 0
Respuesta: Require_once me produce "undefined variable"

Cita:
Iniciado por chulifo Ver Mensaje
Siguiente pregunta, comprobaste que se realizo la conexion?
Código PHP:
Ver original
  1. if (!$conexionidiomas ) {
  2.     echo "Error: No se pudo conectar a MySQL." . PHP_EOL;
  3.     echo "errno de depuración: " . mysqli_connect_errno() . PHP_EOL;
  4.     echo "error de depuración: " . mysqli_connect_error() . PHP_EOL;
  5.     exit;
  6. }
He puesto lo que me comentas después del 'require_once' y no me imprime nada distinto, por lo que sí que debe conectar
  #6 (permalink)  
Antiguo 04/07/2016, 17:37
Avatar de chulifo  
Fecha de Ingreso: abril-2009
Ubicación: perdido en codigos del PHP, pero aprendo rapido!
Mensajes: 524
Antigüedad: 15 años
Puntos: 18
Respuesta: Require_once me produce "undefined variable"

Ya lo he pillado, a ver, esta llamando a la variable conexionidiomas dentro de una funcion, es decir que dentro de esa funciona no se ha definido, asi que debes hacer un llamado global.


Código PHP:
Ver original
  1. $GLOBALS['conexionidiomas'];
o
Código PHP:
Ver original
  1. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  2. {
  3. global $conexionidiomas;
__________________
Solo soy un simple fanático que mata el tiempo de la mejor manera.
  #7 (permalink)  
Antiguo 04/07/2016, 17:54
 
Fecha de Ingreso: mayo-2014
Mensajes: 15
Antigüedad: 9 años, 10 meses
Puntos: 0
Respuesta: Require_once me produce "undefined variable"

Cita:
Iniciado por chulifo Ver Mensaje
Ya lo he pillado, a ver, esta llamando a la variable conexionidiomas dentro de una funcion, es decir que dentro de esa funciona no se ha definido, asi que debes hacer un llamado global.


Código PHP:
Ver original
  1. $GLOBALS['conexionidiomas'];
o
Código PHP:
Ver original
  1. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  2. {
  3. global $conexionidiomas;
Ahora funciona perfecto! No había caído en eso, muchas gracias! Aunque tengo otra pregunta, es posible que el uso de las 'global" afecten de alguna manera a la seguridad? Quiero decir, al ser una variable global puede ser más accesible en general?

Etiquetas: fecha, html, mysql, sql, undefined, variable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:50.