i have an search box on website but whenever we search, it doesn't give any output.
I want to search for page title from database. But don't know what is wrong.
<div id="siteSearch">
<h3>Site Search</h3>
<?php
if (isset($_POST['search'])) {
$search = $_POST['search'];
$query = "SELECT * FROM pages WHERE ptitle LIKE '%$search%'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
if ($count > 0) {
while ($fetch = mysql_fetch_array($result)) {
echo $fetch['ptitle'];
}
}
} else {
echo "No result found!";
}
?>
</div>
You are using the semicolon after the while loop:
while($fetch = mysql_fetch_array($result));
Please remove the semicolon in this line.
You have a misplaced closing bracket. The one above else should be 5 lines below. As it currently stands, the else clause relates to if (isset($_POST['search'])).
Related
I need some PHP coding assistance in building an echo to display the results of a database query...(list friends of the logged-in user?) I have the majority of it built, but I don't know how to echo the results data and link it to his/her profile? Any help would be greatly appreciated...maybe a code snippet of how to write it out?
Here is my code:
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if ($numrows > 0){
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
array_push($my_friends, $row["user2"]);
array_push($my_friends, $row["user1"]);
}
//remove your id from array
$my_friends = array_diff($my_friends, array($u));
//reset the key values
$my_friends = array_values($my_friends);
mysqli_free_result($query);
} else {
echo "You have no friends.";
}
// Build My friends From Results
****this is where I need help with*****
if (array_key_exists('0', $my_friends)){
$sql = "SELECT user1, user2
FROM friends
WHERE (user1='$u' OR user2='$u')
AND accepted='1'";
$result = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$my_friends = "<a href='user.php?u=".$result."'>". $row['avatar']. ' '. $row['firstname']. ' ' .$row['lastname'] ."</a>"; }
}
?>
Here is my html code:
<?php
echo "<li>$my_friends</li>";
?>
You are using the $result variable in the html link. Replace it with $row['id'] or the row attribute you use as GET parameter to see a user's detailed page
Title should make the problem clear. The search function works as intended, but what should be the first hit will not show at all and i don't know why. Here's the code:
<form action="admin_orders_search.php" method="GET">
<input type="text" name="query" value="Search"/>
</form>
<?php
$query = $_GET['query'];
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$sql = "SELECT * FROM orders WHERE 1";
$matches = preg_split('/\s+/',$query);
foreach($matches as $match){
$sql .= " AND `ship_eta` LIKE '%".$match."%'";
}
$raw_results = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results))
{
// ---- RESULTS START ---->
$res=mysql_query("select * from orders ".$qry." order by id desc ");
while($row=mysql_fetch_array($raw_results))
{ ?>
// here are the hits shown
}
// ---- RESULT END ---->
}
}
else{
echo "No hits";
}
?>
In the code:
$res=mysql_query("select * from orders ".$qry." order by id desc ");
while($row=mysql_fetch_array($raw_results))
{
//results here
}
You still use the variable $raw_results. I think you want to use the variable $res since you already have a while with the $raw_results
Ive found some unused lines in the code and after removing them it works! The affected lines are these:
...
if(mysql_num_rows($raw_results) > 0){
// ---- RESULTS START ---->
while($row=mysql_fetch_array($raw_results))
{ ?>
// here are the hits
}
// ---- RESULT END ---->
}
else{
echo "No hits";
}
}
else{
echo "error: ".$min_length." needed minimum!";
}
?>
// ---- SEARCH END ---->
<?php
}
}
?>
I got this code, which is linked to a search field on my index page:
<?php
ini_set('display_errors', 2);
$search = $_GET ['q'];
$conn = mysqli_connect("localhost", "root", "","release");
$query = mysqli_query($conn,"SELECT * FROM game WHERE game_name LIKE '%". $search ."%'");
$foundnum = mysqli_fetch_assoc($query);
$count = count($foundnum['game_name']);
if ($foundnum == 0) {
echo "No results found. Either this game doesn't exist, or we have yet to add it. Please contact us!";
}
else {
while($foundnum= mysqli_fetch_assoc($query))
{
echo "$count result(s) found!<p>";
echo"<pre/>";print_r($foundnum['game_name']);
echo"<pre/>";print_r($foundnum['game_release']);
}
}
?>
Everything's working fine without the while-loop, but because some search terms ('car' for example), should print both Project CARS and Rise of Incarnates, I need a while-loop.
I tried putting the while-loop before the if-statement as well, but that doesnt work either. What am I doing wrong?
I have made some correction in your code.. please re-veirfy using below code
I have tried the code using my db tables and it is showing correct values...
<?php
ini_set('display_errors', 1);
$search = $_GET['q'];
$conn = mysqli_connect("localhost", "root", "", "release");
$query = mysqli_query($conn, "SELECT * FROM game WHERE game_name LIKE '%" . $search . "%'");
$count = mysqli_num_rows($query); // right way to find row count
if ($count == 0)
{
echo "No results found. Either this game doesn't exist, or we have yet to add it. Please contact us!";
}
else
{
while ($foundnum = mysqli_fetch_assoc($query))
{
echo "$count result(s) found!<p>";
echo"<pre>";
print_r($foundnum['game_name']);
echo"</pre><pre>";
print_r($foundnum['game_code']);
echo"</pre>";
}
}
?>
if you want to search insensitively (i.e ignoring capital and small letters) than do let me know.. I will update the code
I have a table which displays information from a database. I added a column where I want to display a message according to row number displayed.
<form name="afisare1" method="POST" >
<input type="submit" name="opt1" value="OK"/>
<?php
if (isset($_POST['opt1'])) {
$loc=mysql_query("SELECT loc FROM program WHERE den='Option'");
//$loc result is a number
$sql=mysql_query("SELECT Col1, Col2
FROM date WHERE Opt_1='Option' OR Opt_2='Option'
ORDER BY Col2 DESC");
?>
<table>
<thead>
<tr>
<th>Col1</th> <th>Col2</th> <th>OK/NOT OK</th>
</tr>
</thead>
<?php
$num_rows = 0;
while ($row = mysql_fetch_assoc($sql)) {
$num_rows++;
if ($num_rows <= $loc) {
echo"<tr>
<td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td>
</tr>";
break;
}
if ($num_rows > $locuri_buget) {
//here i have a problem because i don't know how to display something like this :
//echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>NOT OK</td></tr>";
}
}
} ?>
</table>
</form>
For example if the result of $loc=2 i want to echo for the first 2 rows OK and for extra rows i want to echo NOT OK
To retrieve loc from MySQL, your original code does it like this:
$loc = mysql_query("SELECT loc FROM program WHERE den='Option'");
But you might want to change it like this:
$loc = 0; # default value in case of error in a query
$result = mysql_query("SELECT loc FROM program WHERE den='Option'");
while($row = mysql_fetch_assoc($result)){
$loc = $row['loc'];
}
Or if you want retrieve only one record from your databse, you can also write a SQL with LIMIT like this:
$loc = 0; # default value in case of error in a query
$result = mysql_query("SELECT loc FROM program WHERE den='Option' LIMIT 1");
if($row = mysql_fetch_assoc($result)){
$loc = $row['loc'];
}
Hope this helps.
You can try with following inside while loop
if($num_rows<=$loc){
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}else{
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>NOT OK</td></tr>";
}
$num_rows = 0;
while($row = mysql_fetch_assoc($sql)){
$num_rows++;
if($num_rows<=$loc){
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}else {
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}
}
I think your problem is with break,
break ends execution of the current for, foreach, while, do-while or switch structure.
Break
I am trying to combine two things which are already know how to do, but can't figure out how to combine them. Here is what I want to achieve:
I have a database with locations and events. There are several events in each location. I will be using PHP to query the database and output the code needed to display search results. I want something similar to the below:
<div id="location">
<p>Location1</p>
<div id="event">Event1</div>
<div id="event">Event2</div>
<div id="event">Event3</div>
</div>
<div id="location">
<p>Location2</p>
<div id="event">Event4</div>
<div id="event">Event5</div>
<div id="event">Event6</div>
</div>
I know that I can use select distinct to get the unique value of each location, and know that I can use a normal select statement to get all the events, however how do add all the events inside the location div?
My current PHP looks like this:
$sql ="SELECT location, event from events";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)){
$location = $row['location'];
$event = $row['event'];
echo "<div id="location">
<p>$location</p>
<div id="event">$event</div>
</div>";
}
My current code adds duplicates of the same location with 1 unique event in each. Even if I use select distinct I get the same results. How do I group the events have have the same location?
I think you should write something like:
$sql ="SELECT location, event from events";
$res = mysql_query($sql) or die(mysql_error());
$prevlocation = "";
while($row = mysql_fetch_assoc($res))
{
$location = $row['location'];
$event = $row['event'];
if ( $prevlocation != "" ) // Close previous div if needed
{
echo "</div>";
}
if ( $location != $prevlocation )
{
echo "<div id='location'><p>$location</p>";
$prevlocation = $location;
}
else
{
echo "<div id='event'>$event</div>";
}
}
echo "</div>"; // Close last div
If you have join of two tables, let's assume that your query looks like this:
$sql ="SELECT * FROM events
JOIN locations ON
locations.id=events.loc_id";
And then, within one loop, get events and location arrays:
$res = mysql_query($sql) or die(mysql_error());
$locations = array();
$events= array();
while($row = mysql_fetch_assoc($res))
{
$location = $row['location'];
$event = $row['event'];
$loc_id=$row['loc_id'];
$id=$row['id'];
$events[]=$loc_id.'%'.$event;
if(!in_array($id.'%'.$location,$locations)) { // avoid duplicate entries
$locations[]=$id.'%'.$location;
}
}
And, another loop (+loop inside loop):
for($i=0;$i<count($locations);$i++) {
$location=explode('%',$locations[$i]);
echo "<div class='location'>\n
<p>$location[1]</p>\n";
for($j=0;$j<count($events);$j++) {
$event=explode('%',$events[$j]);
if($event[0]==$locations[$i][0]) {
echo "<div class='event'>".$event[1]."</div>";
}
}
echo "</div>";
}
Not so elegant, but it is working, and produces valid HTML. :)
First, i wanted to make two associative arrays, and to compare keys, but i couldn't, because i couldn't convert ID keys to strings, so i made it with explode (% is separator between key and value).