Bazzaah Posted May 30, 2011 Share Posted May 30, 2011 Hi Hope someone can help me please. I have constructed an audio dictionary and have discovered an error now that I have added a few entries to the database. A user can search the database and can click on a result to be taken to the content associated with the entry he chooses. This is the search function; $search=$_POST["search"]; //get the mysql and store them in $result $result = mysql_query("SELECT word FROM pro_words WHERE word LIKE '%$search%'"); //get the db content that is specified above if (mysql_num_rows($result) < 1) { echo "<br><br><h2> We didn't find anything. Sorry - we did look though.</h2>"; }else { echo '<table align="center" cellspacing="8" cellpadding="8" width="85%"><tr><td align="left"><b>Word</b></td></tr>'; echo "<br><br><h2>Success! Here's what we found:</h2><br>"; while ($r=mysql_fetch_array($result, MYSQLI_ASSOC)) echo '<h2><tr><td align="left"><a href="word.php?w=' . $r['word'] . '">' . $r['word'] . '</a></td></tr></h2>'; } You will see that content is displayed in a new file called word.php. This is relevant code from word.php $query = "SELECT word,word_type1,sentence1,word_type2,sentence2,word_type3,sentence3 FROM pro_words"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo '<div class="colmask rightmenu">'; echo '<div class="colleft">'; echo '<div class="col1">'; echo '<p>Here are some example sentences that show how we use the word; </p>'; echo '<div id="small"><i> ' . $row['word_type1'] . '</i></div>'; echo '<p><div id="small">' . $row['sentence1'] . '</p></div>'; echo '<div id="small"><i> ' . $row['word_type2'] . '</i></div>'; echo '<p><div id="small">' . $row['sentence2'] . '</p></div>'; echo '<div id="small"><i> ' . $row['word_type3'] . '</i></div>'; echo '<p><div id="small">' . $row['sentence3'] . '</p></div>'; } The problem is that every entry on the database is echoed in word.php whereas I would like only the entries for the word selected to appear. Thanks in advance for any help; do say if you need more info. Quote Link to comment https://forums.phpfreaks.com/topic/237869-all-entries-from-db-passed-to-output-file-in-error/ Share on other sites More sharing options...
chintansshah Posted May 30, 2011 Share Posted May 30, 2011 You can save your search criteria in session. $_SESSION['where_caluse'] = " WHERE word LIKE '%".$search."%'"; $result = mysql_query("SELECT word FROM pro_words ".$_SESSION['where_clause']); Get session value in word.php file your query looks like $query = "SELECT word,word_type1,sentence1,word_type2,sentence2,word_type3,sentence3 FROM pro_words".$_SESSION['where_clause']; Quote Link to comment https://forums.phpfreaks.com/topic/237869-all-entries-from-db-passed-to-output-file-in-error/#findComment-1222353 Share on other sites More sharing options...
Bazzaah Posted May 30, 2011 Author Share Posted May 30, 2011 Thanks for that. I should have said that the facility can only be used by registered users - there's already a session variable in existence for the user; where does that leave the session variable you create? I popped that code you kindly provide into the relevant files but doesn't seem to have an effect..I wonder if I'm missing something - is there any significance behind your calling 'where_clause'? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/237869-all-entries-from-db-passed-to-output-file-in-error/#findComment-1222400 Share on other sites More sharing options...
Bazzaah Posted May 31, 2011 Author Share Posted May 31, 2011 Thanks again - it does work but only if there is one search result. If there are two results, it displays two results on word.php. How do I get it so that, if there are multiple results displayed, then only the chosen entry is displayed on word.php? Quote Link to comment https://forums.phpfreaks.com/topic/237869-all-entries-from-db-passed-to-output-file-in-error/#findComment-1222882 Share on other sites More sharing options...
chintansshah Posted May 31, 2011 Share Posted May 31, 2011 have you written session_start(); at first line of the php file? If you write on both the files then debug $_SESSION variable. Quote Link to comment https://forums.phpfreaks.com/topic/237869-all-entries-from-db-passed-to-output-file-in-error/#findComment-1222898 Share on other sites More sharing options...
Bazzaah Posted May 31, 2011 Author Share Posted May 31, 2011 Yes the files have session start(). How do i debug the session variable? Sorry, I'm a bit of a noob with this... Quote Link to comment https://forums.phpfreaks.com/topic/237869-all-entries-from-db-passed-to-output-file-in-error/#findComment-1222918 Share on other sites More sharing options...
chintansshah Posted May 31, 2011 Share Posted May 31, 2011 write below code after session_start(); <?php session_start(); echo "<pre>"; print_r($_SESSION); echo "</pre>"; ?> After that, you can find out session values on screen. Quote Link to comment https://forums.phpfreaks.com/topic/237869-all-entries-from-db-passed-to-output-file-in-error/#findComment-1222921 Share on other sites More sharing options...
Bazzaah Posted May 31, 2011 Author Share Posted May 31, 2011 I ran that and no errors, it just returned this; Array ( [user_id] => 1 [first_name] => Myname [user_level] => 0 [where_clause] => WHERE word LIKE '%e%' ) which is what I'd expect; no errors though. Quote Link to comment https://forums.phpfreaks.com/topic/237869-all-entries-from-db-passed-to-output-file-in-error/#findComment-1223050 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.