Page 1 of 1

Why this error?

Posted: Thu Nov 27, 2003 3:17 pm
by Perfidus
<?php
//page2
session_start();
if (session_is_registered('usuario')){
}
else{
header("Location: acceso6.html?errorusuario=si");
}
$session= session_id();
$base="";
$conexion=mysql_connect("","","");
mysql_select_db($base,$conexion);
$pegar2 = "SELECT Inmo FROM Sesiones WHERE Sesion='$session'";
$row = mysql_fetch_array($pegar2);
$Inmo = $row["Inmo"];
?>

ERROR:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /chs/p1/costa4seasons.com/home/html/panelcontrol.html on line 13
?>

Posted: Thu Nov 27, 2003 3:22 pm
by microthick
You also need to use mysql_query()...

$pegar2 = "SELECT Inmo FROM Sesiones WHERE Sesion='$session'";
$result = mysql_query($pegar2);
$row = mysql_fetch_array($result);

Posted: Fri Nov 28, 2003 2:54 am
by twigletmac
As microthick said, you need to query the database before trying to work with the result. You would also probably find it helpful to put error handling in and use non-register globals dependant code:

Code: Select all

<?php

session_start();

// check for the value in the $_SESSION array, don't use the deprecated
// function session_is_registered()
if (empty($_SESSION['usuario'])){
	header('Location: acceso6.html?errorusuario=si');
	// put the exit() call in to ensure that the code does not continue
	// executing - it's a failsafe.
	exit();
}

$session = session_id();
$base    = '';

// put error handling in to make sure that problems are easy to identify
// and deal with
$conexion = mysql_connect('', '', '') or die(mysql_error());
mysql_select_db($base, $conexion) or die(mysql_error());

$pegar2 = "SELECT Inmo FROM Sesiones WHERE Sesion='$session'";

// here's the mysql_query() call you were missing, it also has error
// handling just in case the query does not work as expected.
$result = mysql_query($pegar2) or die(mysql_error().'<p>'.$pegar2.'</p>');

// you need to check that there was a result before trying to do anything
// with the returned data
if (mysql_num_rows($result) == 1) {
	$row = mysql_fetch_assoc($pegar2);
	$Inmo = $row['Inmo'];
	// rest of code...
} else {
	// give a message if the session doesn't exist or maybe run the same 
	// redirect code as before.
	echo 'That session does not exist!';
}

?>
Mac