Jump to content

Undefined Variable


Go to solution Solved by taichi56,

Recommended Posts

I recieved this coding on another site and changed it a little to get it to work with MYSQL database. I am able to connect to my localhost database but when I put in a search term I get this error:

 

! ) Notice: Undefined variable: Get in C:\wamp\www\search.php on line 15 Call Stack # Time Memory Function Location 1 0.0003 373624 {main}( ) ..\search.php:0

minimum length is 3

 

The error is showing on line 15: $query = $_GET['query']

 

Here is my code:

<?php  
	mysql_connect("localhost", "root", "") or die("Error connecting to the database: ".mysql_error());
	mysql_select_db("magicgathering") or die(mysql_error());
?>

<!DOCTYPE html Public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR.xhtml/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Search Results</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php
	$query = $_Get['query'];
	//Gets value sent over search form
	
	$min_length = 3;
	//you can set minimum length of the query if you want
	
	if(strlen($query) >= $min_length){ // if query length is more or equal.
	
	$query = htmlspecialchars($query);
	// Changes characters used in html to their equivalents, for example: < to &Gt;
	
	$query = mysql_real_escape_string($query);
	// makes sure nobody uses SQL injection
	
	$raw_results = mysql_query("SELECT * FROM tblmtg WHERE ('name' LIKE '%".$query."%') OR ('type' LIKE '%".$query."%')") or die(mysql_error());
	//* means that it selects all fields, you can also write: 'id', 'name', 'type'
	// tblmtg is the name of our table
	
	// '%$query' is what we're looking for , % means anything, for example if $query is Hello 'name'='$query'
	// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR... '$query %' ...OR...  '% $query'
	
	if(mysql_num_rows($raw_results) > 0) { // if one or more rows are returned do following
	
	while($results = mysql_fetch_array($raw_results)) {
		// results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
		
		echo "<p><h3>".$results['name']. "</h3>".$results['type']."</p>";
		// posts results gotten from database (name and type) you can also show id ($results['id'])
	}
	}
	else { // if there is no matchig rows do the following 
		echo "No results";
	}
	}
	else { // if query length is less than minimum 
		echo "minimum length is ".$min_length;
	}
?>
</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/279136-undefined-variable/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.