How can this be undefined

I have a page which uses the PDO method to pull results from a database.
I tried adding a search form and everything looks good.
But when I submit the form I get

Notice : Undefined variable: pdo in C:\xampp\htdocs\DCT\2\racks\index.php on line 73

Heres the PHP which im using

if(isset($_GET['Search'])) {
	$search = strtolower($_GET['Search']);
	preg_replace("#[^0-9a-z]#i","",$search);	
	$query = 'SELECT rack_id,title,room_id,row,bay
			FROM racks
			WHERE title LIKE "%'.$search.'%"';

	$result2 = $pdo->query($query);

}

The pdo variable seems to work fine at the beginning

$result = $pdo->query($sql);

can I not use it again?

Beginning of what?

so, the page loads perfect (showing the results of a query, which looks like this is

	try{
		$pdo = new PDO("mysql:host=localhost;dbname=dct", "root", "");
		// Set the PDO error mode to exception
		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	} catch(PDOException $e){
		die("ERROR: Could not connect. " . $e->getMessage());
	}
 
// Attempt select query execution
try{


$sql = 'SELECT rack_id,title,manufacturer,model,slots,created_date,title,room_id,row,bay
			FROM racks';	
//echo $sql;
$result = $pdo->query($sql);

However I get
Notice : Undefined variable: pdo in C:\xampp\htdocs\DCT\2\racks\index.php on line 72
when I try to perform a search using

if(isset($_GET['Search'])) {
	$search = strtolower($_GET['Search']);
	$sql = 'SELECT rack_id,title,room_id,row,bay
			FROM racks
			WHERE title LIKE :search';

   $query = $pdo->prepare($sql);
   $query->bindValue(':search', '%' . $search . '%');
   $query->execute();
   //$result2 = $pdo->query($query);

}

How can it be undefined if im using the pdo variable when the page first loads?

Show the rest of the code where $pdo is set.

so, the page loads perfect (showing the results of a query, which looks like this is

	try{
		$pdo = new PDO("mysql:host=localhost;dbname=dct", "root", "");
		// Set the PDO error mode to exception
		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	} catch(PDOException $e){
		die("ERROR: Could not connect. " . $e->getMessage());
	}
 
// Attempt select query execution
try{


$sql = 'SELECT rack_id,title,manufacturer,model,slots,created_date,title,room_id,row,bay
			FROM racks';	
//echo $sql;
$result = $pdo->query($sql);

However I get
Notice : Undefined variable: pdo in C:\xampp\htdocs\DCT\2\racks\index.php on line 72
when I try to perform a search using

if(isset($_GET['Search'])) {
	$search = strtolower($_GET['Search']);
	$sql = 'SELECT rack_id,title,room_id,row,bay
			FROM racks
			WHERE title LIKE :search';

   $query = $pdo->prepare($sql);
   $query->bindValue(':search', '%' . $search . '%');
   $query->execute();
   //$result2 = $pdo->query($query);

}

How can it be undefined if im using the pdo variable when the page first loads?

I think what @benanamen means is to show all the code between where it works successfully, and where it gives the error.

Does your search go through the code that defines the $pdo instance, or is that inside some other if() clause? Some more details on the structure of the code would be useful. Either it doesn’t define it in the first place, or it is clearing it. Just seeing the individual code snippets doesn’t allow anyone to see where that is happening. On the face of it, there’s nothing wrong with the second bit of code (presuming that’s where line 72 or 73 is, you don’t confirm that), so the problem must lie elsewhere.

1 Like

Aside of your question…

preg_replace("#[^0-9a-z]#i","",$search);

Should be…

$search = preg_replace("#[^0-9a-z]#i","",$search);

Otherwise you get no changes in $search.

1 Like

@lurtnowski, it would be really helpful if you could put your app on Github. If you don’t want the code accessible to the world just make it a private repo and give access to it as you see fit. Having it on the repo will allow us to review the code as a whole. You have other issues besides what you posted about.

2 Likes

ok, thanks.
About the pdo thing, I deleted the unset() on it and it worked.