|
mysql not retuning query need help
I am very new to php and mysql. I have been basically taking a crash course in it to do things for my website. I am having a proble of returning a query from mysql db table. Here is my code:
This first bit is the page where I get the variables I need then I am posting them to the next page. Here it is.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hit Entry Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php
$user = JFactory::getUser();
$ses_id = $user->get('id');
?>
<form action="search.php" method="POST">
<input type="text" name="query" />
<input type="hidden" name=$ses_id value="sesid" />
<input type="submit" value="Search" />
</form>
</body>
</html>
What it does is get what the user wants to search by and it get the user id for this user currently logged in. The jfactory is from joomla when I load the next page I lose the jfactory session so I have to grab the ID here. The next bit uses the 'query' and 'sesid' that is sent to it and puts them into variables. My query is trying to ask to search all of the table and return any that have both of these values on the same record and then display them.. I do not understand why it is not working. here it is.. some information is ** out for security reasons.
<?php
mysql_connect("*********", "******", "******") or die("Error
connecting to database: ".mysql_error());
/*
localhost - it's location of the mysql server, usually localhost
root - your username
third is your password
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("mtu1225803133696") or die(mysql_error());
/* mtu1225803133696 is the name of database */
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/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 = $_POST['query'];
$ses_id = $_POST['sesid'];
// gets value sent over search form
$min_length = 2;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM jos_chronoforms_data_hitsubmit
WHERE (`cf_user_id`='$sesid') AND (`cf_reqname`='$query')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$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['cf_reqname']."</h3>".$results['cf_reqdescr']."</p>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
}
else{ // if there is no matching rows do following
echo "No requester found by that name. Either you have not done any hits for this requester or you entered the name
incorrectly. Names must be entered the same as you stored them.";
}
}
else{ // if query length is less than minimum
echo "You must enter at least 2 characters ";
}
?>
</body>
</html>
If I search and it should be a matching search it still returns no results found. Any help is greatly appreciated.
|