White_Lily Posted November 5, 2012 Share Posted November 5, 2012 I have a search box inside the "inbox" area of my website, i want it so that when the user types in the username of the person that sent a message to them it should be able to count the number of results, and show them in a table of results. However the problem im facing is that when you search for a message the result counter returns the correct value (say... 2), while the table only shows 1 result. I can't see or think for the life of me what is wrong with the code below... <?php include "inc/scriptstart.php"; $title = "Fusion Social | "; $page = "Inbox"; $site = $title.$page; session_start(); $u = $_SESSION["user"]; $dir = $_SESSION["dir"]; if(empty($u) || empty($dir)){ header("Location: index.php"); } $searchquery = $_GET["query"]; include "inc/check.php"; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[url="http://www.w3.org/TR/html4/loose.dtd"]http://www.w3.org/TR/html4/loose.dtd[/url]"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <?php include "inc/scripts.php"; ?> <title><?=$site?></title> </head> <body> <div class="header"> <div id="header"> <ul> <li><a href="<?=$GLOBALS["nav"]?>profile.php">Profile</a></li> <li><a href="<?//=$GLOBALS["nav"]?>#">Friends</a></li> <li><a href="<?=$GLOBALS["nav"]?>logout.php">Logout</a></li> </ul> <div id="search"> <?php if($searcherr){ echo '<div class="error">Error: '.$searcherr.'</div>'; } ?> <form action="" method="POST"> <table border="0"> <tr> <td><input type="text" name="define" id="name" class="searchField" /></td> <td><input type="submit" name="search" value="Search" class="buttons" /></td> </tr> </table> </form> </div> <div class="clear"></div> </div> </div> <div class="content"> <div class="main"> <div class="leftcol"> <h2><?=$page?></h2> <img src="<?=$GLOBALS["nav"]?>images/vertical-rule.png" /> <div class="clear"></div> <?php $results = select("*", "users, messages", "users.username = messages.to AND messages.to = '$u' AND messages.from LIKE '%$searchquery%'") or die(mysql_eror()); $numsearch = mysql_num_rows($results); $getsearch = mysql_fetch_assoc($results); echo '<p>Results Found: '.$numsearch.'</p>'; echo '<img src="'.$GLOBALS["nav"].'images/vertical-rule.png" />'; echo '<div class="clear" style="height:30px;"></div>'; echo '<table border="0">'; echo '<th width="1%">Senders Avatar</th>'; echo '<th width="25%">From</th>'; echo '<th width="25%">To</th>'; echo '<th width="25%">Options</th>'; if($numsearch > 0){ while($getfill = mysql_fetch_array($results)){ echo '<tr>'; echo '<td width="1%">'; if(empty($getfill["avatar"])){ echo '<img src="'.$GLOBALS["nav"].'images/avatar.png" />'; }elseif(!empty($getfill["avatar"])){ echo '<img src="'.$getfill["avatar"].'" />'; } echo '</td>'; echo '<td width="25%"><p>'.$getfill["from"].'</p></td>'; echo '<td width="25%"><p>'.$getfill["to"].'</p></td>'; echo '<td width="25%">'; echo '<a href="#">View Message</a>'; echo '<a href="#">Delete Message</a>'; echo '</td>'; echo '</tr>'; } }elseif($numsearch == 0){ echo '<tr>'; echo '<td colspan="4">'; echo "<p>There are currently no messages to view!</p>"; echo '</td>'; echo '</tr>'; } echo '</table>'; ?> </div> <img src="<?=$GLOBALS["nav"]?>images/home-divider.png" /> <!--<div class="rightcol"> <h3></h3> <?php// ?> <img src="<?//=$GLOBALS["nav"]?>images/vertical-rule.png" /> </div>--> <div class="clear"></div> </div> </div> <div class="footer"> <p>Copyright © <?=date("Y")?> Fusion Social</p> </div> </body> </html> Any Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/270316-searchng-for-particular-messages/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 5, 2012 Share Posted November 5, 2012 $getsearch = mysql_fetch_assoc($results); ^^^ Why do you have a line of code that is fetching a row from the result set before the start of your while(){} loop? Wouldn't that mean that your while(){} loop will start at the second record in the result set? Quote Link to comment https://forums.phpfreaks.com/topic/270316-searchng-for-particular-messages/#findComment-1390338 Share on other sites More sharing options...
White_Lily Posted November 5, 2012 Author Share Posted November 5, 2012 the assoc line will be used later after the message listing, plus, ive taken that line out because i also thought it would be the problem, but nothing changes. Quote Link to comment https://forums.phpfreaks.com/topic/270316-searchng-for-particular-messages/#findComment-1390339 Share on other sites More sharing options...
MDCode Posted November 5, 2012 Share Posted November 5, 2012 (edited) $results = select("*", "users, messages", "users.username = messages.to AND messages.to = '$u' AND messages.from LIKE '%$searchquery%'") or die(mysql_eror()); You misspelled mysql_error there may be a problem with your query. (Doubtful) Edited November 5, 2012 by ExtremeGaming Quote Link to comment https://forums.phpfreaks.com/topic/270316-searchng-for-particular-messages/#findComment-1390340 Share on other sites More sharing options...
White_Lily Posted November 5, 2012 Author Share Posted November 5, 2012 I don't think there is because a different page uses the same query (but for 2 different tables) and that works fine Quote Link to comment https://forums.phpfreaks.com/topic/270316-searchng-for-particular-messages/#findComment-1390345 Share on other sites More sharing options...
MDCode Posted November 5, 2012 Share Posted November 5, 2012 (edited) Do you have error reporting on? I see nothing other than the form not putting anything to get in the url. Btw you're opening up to sql injection by not filtering the $_GET["query"] Edited November 5, 2012 by ExtremeGaming Quote Link to comment https://forums.phpfreaks.com/topic/270316-searchng-for-particular-messages/#findComment-1390346 Share on other sites More sharing options...
White_Lily Posted November 5, 2012 Author Share Posted November 5, 2012 error reporting is always on when im building a site, and it is showing no errors. The search box is on a different page, and the script that does all the checks for different forms on the site is in a different page to. Quote Link to comment https://forums.phpfreaks.com/topic/270316-searchng-for-particular-messages/#findComment-1390347 Share on other sites More sharing options...
PFMaBiSmAd Posted November 5, 2012 Share Posted November 5, 2012 If you removed that line of code and there was no change in the output, then either you didn't save the file, upload the file to your server, or caching somewhere between the file and your browser is showing the previous results. If you are getting a mysql_num_rows value of 2, then running a total of 2 mysql_fetch_assoc statements will fetch those 2 rows. Quote Link to comment https://forums.phpfreaks.com/topic/270316-searchng-for-particular-messages/#findComment-1390349 Share on other sites More sharing options...
White_Lily Posted November 5, 2012 Author Share Posted November 5, 2012 I will clear my cache and see what happens. doe this mean that i will have to write a seperate query for the stuff i wanted under the while loop? Quote Link to comment https://forums.phpfreaks.com/topic/270316-searchng-for-particular-messages/#findComment-1390352 Share on other sites More sharing options...
PFMaBiSmAd Posted November 5, 2012 Share Posted November 5, 2012 If you mean reusing the result set, no, you can perform a seek statement to return the result pointer to the first (or any other) record - mysql_data_seek Quote Link to comment https://forums.phpfreaks.com/topic/270316-searchng-for-particular-messages/#findComment-1390354 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.