Page 1 of 1

[HELP]Undefined Varible

Posted: Sat Aug 21, 2004 10:15 am
by bla5e
code (note starting at line 23):

Code: Select all

<?php 

error_reporting(E_ALL);  
switch ($id) {
	default:
		$dbh=mysql_connect ("localhost", "<hidden>", "<hidden>") or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("cds");
		$sql = "SELECT * FROM cds ORDER BY name";
		echo mysql_error();
		$result = mysql_query($sql, $dbh);
		echo mysql_error();
	break;
	
	case 'cdname':
		$dbh=mysql_connect ("localhost", "<hidden>", "<hidden>") or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("cds");
		$sql = "SELECT * FROM cds ORDER BY cdname";
		echo mysql_error();
		$result = mysql_query($sql, $dbh);
		echo mysql_error();
	break;
	
	case 'genre':
		$dbh=mysql_connect ("localhost", "<hidden>", "<hidden>") or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("cds");
		$sql = "SELECT * FROM cds ORDER BY genre";
		echo mysql_error();
		$result = mysql_query($sql, $dbh);
		echo mysql_error();
	break;
	
	case 'tracks':
		$dbh=mysql_connect ("localhost", "<hidden>", "<hidden>")  or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("cds");
		$sql = "SELECT * FROM cds ORDER BY tracks";
		echo mysql_error();
		$result = mysql_query($sql, $dbh);
		echo mysql_error();
	break;
	
	case 'rlsdate':
		$dbh=mysql_connect ("localhost", "<hidden>", "<hidden>") or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("cds");
		$sql = "SELECT * FROM cds ORDER BY rlsdate";
		echo mysql_error();
		$result = mysql_query($sql, $dbh);
		echo mysql_error();
	break;
	
	case 'rlsgroup':
		$dbh=mysql_connect ("localhost", "<hidden>", "<hidden>") or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("cds");
		$sql = "SELECT * FROM cds ORDER BY rlsgroup";
		echo mysql_error();
		$result = mysql_query($sql, $dbh);
		echo mysql_error();
	break;

}
---


Errors:
Notice: Undefined variable: id in c:\program files\apache group\apache\htdocs\cdview.php on line 36

Notice: Undefined variable: id in c:\program files\apache group\apache\htdocs\cdview.php on line 45

Notice: Undefined variable: id in c:\program files\apache group\apache\htdocs\cdview.php on line 54

Notice: Undefined variable: id in c:\program files\apache group\apache\htdocs\cdview.php on line 63

Notice: Undefined variable: id in c:\program files\apache group\apache\htdocs\cdview.php on line 72
----

Line 36:

Code: Select all

case 'cdname':
Line 45:

Code: Select all

case 'genre':
Line 54:

Code: Select all

case 'tracks':
Line 63:

Code: Select all

case 'rlsdate':
Line 72:

Code: Select all

case 'rlsgroup':
-------------------------

I ran the script thro Zend Dev Environment, but it didnt pickup anything - do you guys see any problems with the script?

Edit: when i run the script like "index.php?id=genre" it wont display the errors

Posted: Sat Aug 21, 2004 10:18 am
by John Cartwright
Those arn't errors just notices, saying you havn't defined the variable before you are checking it.

Posted: Sat Aug 21, 2004 10:22 am
by bla5e
Phenom wrote:Those arn't errors just notices, saying you havn't defined the variable before you are checking it.
how do i make them go away :-z

Posted: Sat Aug 21, 2004 10:30 am
by feyd
make sure the variable is set...

Code: Select all

$id = isset($id) ? $id : '';

btw, you can move all but the $sql setting lines out of those cases and place them around the switch call....

Posted: Sat Aug 21, 2004 11:31 am
by markl999
I can't even see the need for a switch at all as there's too much replicated code, i'd just do something like:

Code: Select all

<?php
$valid = array(
  'name', 'cdname', 'genre', 'tracks', 'rlsdate', 'rlsgroup'
);
$id = empty($_GET['id']) ? 'name' : $_GET['id'];
if(in_array($id, $valid)){
  mysql_connect ("localhost", "<hidden>", "<hidden>") or die(mysql_error());
  mysql_select_db ('cds') or die(mysql_error());
  $sql = 'SELECT * FROM cds ORDER BY '.$id;
  $result = mysql_query($sql) or die(mysql_error());
} else {
  echo 'Invalid id: '.$_GET['id'];
}
?>

Posted: Sat Aug 21, 2004 1:35 pm
by bla5e
markl999 wrote:I can't even see the need for a switch at all as there's too much replicated code, i'd just do something like:

Code: Select all

<?php
$valid = array(
  'name', 'cdname', 'genre', 'tracks', 'rlsdate', 'rlsgroup'
);
$id = empty($_GET['id']) ? 'name' : $_GET['id'];
if(in_array($id, $valid)){
  mysql_connect ("localhost", "<hidden>", "<hidden>") or die(mysql_error());
  mysql_select_db ('cds') or die(mysql_error());
  $sql = 'SELECT * FROM cds ORDER BY '.$id;
  $result = mysql_query($sql) or die(mysql_error());
} else {
  echo 'Invalid id: '.$_GET['id'];
}
?>
thanks