Jump to content

Query Print Error


jermvirus

Recommended Posts

Hello all,

I am having this very frustrating issue, I am trying to print this query i got from my SQL table. However, If the query returns 10 [1,2,3,4,5,6,7,8,9] results it just prints 5 [2,4,6,8,10] :shrug:. I have no idea what to do.

 

<!--Error Reporting Production Only[start]-->
<?php 
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors','1');

//Error Reporting Production Only[end]-->

//Connect to Database [start]-->

require_once 'sqllogin.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());


mysql_select_db($db_database) 
or die("Unable to select database: " . mysql_error());
//Connect to Database [end]-->
//Process the search [start]-->

//Initialize the search output variable
$search_output = "";
$sql_command = "SELECT * FROM `Alpha` WHERE 1";

//See if the posted search field is set and has a value 
if (isset($_POST['searchquery']) && $_POST['searchquery'] !="")
	{//IF START 1
		//run code if condition is met
		//Filter the search query user input
		$searchquery = preg_replace('#[a-z 0-9 A-Z]#i', '', $_POST['searchquery']);
// Search Query*********************
		if($_POST['filter1'] == "All")
			{//all filter code goes her
				$sql_command =  "SELECT id, lastname, firstname, building, room FROM `Alpha` WHERE firstname LIKE '%$_POST[searchquery]%' OR lastname LIKE '%$_POST[searchquery]%' OR id LIKE '%$_POST[searchquery]%'";
			}
		else if($_POST['filter1'] == "Last Name")
			{//Last Name filter code goes her
				$sql_command = "SELECT id, lastname, firstname, building, room FROM `Alpha` WHERE lastname LIKE '%$_POST[searchquery]%'";
			}
		else if($_POST['filter1'] == "First Name")
			{//First Name filter code goes her
				$sql_command = "SELECT id, lastname, firstname, building, room FROM `Alpha` WHERE firstname LIKE '%$_POST[searchquery]%'";
			}
		else if($_POST['filter1'] == "First Name")
			{//ID filter code goes her
				$sql_command = "SELECT id, lastname, firstname, building, room FROM `Alpha` WHERE id LIKE '%$_POST[searchquery]%'";
			}						
	}//IF END 1

	$query = mysql_query($sql_command) or die(mysql_error());
	$count = mysql_num_rows($query);
	if ($count>0)
	{
		$search_output.= "<hr /> $count results for <strong>$_POST[searchquery]</strong><hr /> $sql_command <hr />**IF**POST = $_POST[searchquery] Count=$count, Query= $query<hr />";
		/*for ($j = 0 ; $j < $rows ; ++$j)
		{

						$id = $row["id"];
			$lastname = $row["lastname"];
			$firstname = $row["firstname"];
			$building = $row["building"];
			$room = $row["room"];
			$search_output .= "$id  \t $firstname \t\t $lastname \t\t $building \t$room<br />";
		}



		***************
			Can be used instead but not here try putting it where $search_output is
			echo 'ID: ' 		.	$row[0] . '<br />';
			echo 'Last Name: ' 	.	$row[1] . '<br />';
			echo 'First Name: ' .	$row[2] . '<br />';
			echo 'Building: ' 	.	$row[3] . '<br />';
			echo 'Room: ' 		.	$row[4] . '<br /><br />';
		***********************	
		*/

		while($row = mysql_fetch_array($query))
		{
			$row = mysql_fetch_array($query);
			$id	= $row[0];
			$lastname=$row[1];
			$firstname=$row[2];
			$building=$row[3];
			$room=$row[4];
			$search_output .= "$id   $firstname  $lastname  $building $room<br />";

		}
	}
	else
	{
		$search_output ="<hr /> 0 results for <strong>$searchquery</strong><hr /> $sql_command <hr />****ELSE**POST = $_POST[searchquery] Count=$count, Query= $query<hr /" ;
	}

?>

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MC Reslife WEB APP Developed by; JB</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>

    
<body>
<div id="Header">Monroe College Resident's Life Web App</div>
<div id="Page">
  <div id="Menu">
    <h1>Search</h1>
    	<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
   			<input name="searchquery" type="text" maxlength="19" size="19"/>
        	<input name="myBTN" type="submit" />
            <br />
            Search By:
        	<select name="filter1">
            <option value="All">All</option>
            <option value="Last Name">Last Name</option>
            <option value="First Name">First Name</option>
            <option value="ID Number">ID Number</option>
        	</select>
    	</form>
        <hr />
        
    <h1>Menu</h1>
    	<p><a href="#">Alpha List</a></p>
    	<p><a href="#">Lock Out Log</a></p>
    	<p><a href="#">Current Probations</a></p>
  </div>
  <div id="Content">
    <h1>Alpha List</h1>
    <?php
echo $search_output;

?>
    <p>*************************</p>
    <?php echo "this is what in post $_POST[searchquery]"?>
  </div>
</div>
<div id="Footer">Content for  id "Footer" Goes Here</div>

</body>
</html>

 

please help me

Link to comment
Share on other sites

Try take this out from inside your while statement (since the while statement means your are already looping through the results):

 

$row = mysql_fetch_array($query);

 

Then, as a side note, you should look into SQL injection and XSS (cross site scripting) and how to overcome these vulnerabilities in your code.  There are quite a few of these, including the use of $_SERVER['PHP_SELF'] to set your form's action URL (which introduces a means for malicious users to inject code into your page).

Link to comment
Share on other sites

It's possible your HTML contains a repeated broken tag somewhere and so it just looks like only some results are showing.  View the generated page's source to make sure all the results aren't actually in the output.

 

For example, this line of yours has a broken <hr /> tag at the end:

 

$search_output ="<hr /> 0 results for <strong>$searchquery</strong><hr /> $sql_command <hr />****ELSE**POST = $_POST[searchquery] Count=$count, Query= $query<hr /" ;

 

Otherwise, try inspecting the query results before printing any HTML to the page using something like:

 

<?php
$results = array();
while($row = mysql_fetch_array($query, MYSQL_NUM))
{
$results[] = $row;	
}
print_r($results);
exit;
?>

 

Are they all there?

Link to comment
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.