Php/mysql search whith custom Dispaly - php

I'm trying to make a simple search engine using php & mysql . I know how get mysql result using php. Its Work.
ex : - Mysql database stored this (database table filed "meta")
I am having some difficulty with my PHP/MySQL search. It worked great
a year ago, but for some reason it has just stopped working. I have
tried searching and cannot figure it out. I have it echo my query but
it never adds in the WHERE clause. Any help is much appreciated. Form
Code: PHP Code:
Search word is searching
I Need to dispay search result like this
I have tried searching and cannot figure it out. I have it echo my
query but it never adds in the WHERE clause. Any help is much
appreciated. Form Code: PHP Code:

$query= mysql_query("SELECT meta FROM data WHERE LIKE '%searching%' ");
$count = mysqli_num_rows($query);
if($count >0){
while($row = mysqli_fetch_array($query)) {
echo "$row[id]'>$row[name]</option>";
$count =1;
}
else{
echo "<option value='' selected='selected'></option>";
}
mysqli_close($con);
From the code above here, there's a couple of issues that stand out:
The echo for the option doesn't have an open for , so it's not going to show.
When you're echoing information from the $row variable, you're referencing id and name, but the query is only outputting meta.
The query isn't searching on a field for the where clause, and is therefore failing
The code, from what I can see, should be changed to the following to work (assuming no other surrounding issues):
$query = mysql_query("SELECT meta, id, name FROM data WHERE meta LIKE '%searching%'");
$count = mysqli_num_rows($query);
if ($count > 0)
{
while ($row = $mysqli_fetch_array($query))
{
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
$count = 1; // not sure why this needs to be here, but it's in your code so I'm keeping it there
}
}
else
{
echo "<option value='' selected='selected'></option>";
}
mysqli_close($con);

Related

PHP/MySQL - how to complete dropdown selection from list

I'm cobbling together a dropdown list that I would like to display the '[ve_venue]", "[ve_town]' from a MySQL query and, upon selection set a variable that I can use to pass the ve_ID on to an update query that adds a venue ID number to a separate table as a lookup key.
So far I've got some code that I've pieced together from various places and I can get it to display the town in a dropdown - I just need to add the venue field to the dropdown so I get "venue, town" in the list and I also need to be able to pass the ve_ID to a variable, say, $ve_ID so I can call it in some separate code (that will be on the same page in a separate include).
Here's what I've got so far....
<?
include('login_admin_sucess.php');
// Connects to your Database
include '../connect_include.php';
$query = "SELECT ve_ID, ve_venue, ve_town FROM `co_venues` ORDER BY ve_ID ";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
$ve_venue = $r['ve_venue'];
$result_array[$ve_venue][] = $r['ve_town'];
}
echo "<select name='x'>";
foreach($result_array as $v_venue => $value)
{
foreach($value as $title) {
echo "<option value='" . $v_venue . "'>" . $title . "</option>";
}
}
echo "</select>";
?>
I realise that mysql_ is deprecated...I'll work on that once I've got everything functioning.
You can concat the id and venue. For example:
$ve_venue = $r['ve_venue'] . "|" . $r['ve_ID'];
When you use the variable make a split with charapter "|" with preg_split function.

Display table results

I am trying to display results from a database onto my site. I have one entry in the database, and the result of that entry should be a "1". However, when I run the following code, it returns "1111111111111..." and continues to load ones. If there are 4 entries, I want the results to be displayed like:
1
2
3
4
I have looked around on this forum and others but cannot find anywhere where somebody had the same problem, and my code looks similar to the others. What did I do wrong?
functions.php:
$sqlgroup = mysql_query ("SELECT groupid FROM groups");
$grouprow = mysql_fetch_array ($sqlgroup);
Settings.php:
<?php while( ($row = $grouprow))
{
echo "<tr>";
echo "<td>".$row['groupid']."</td>";
echo "</tr>";
} ?>
First of all don't use mysql, use mysqli isntead:
$sqlgroup = mysqli_query ("SELECT groupid FROM groups");
while($row = mysqli_fetch_array($sqlgroup))
{
echo "<tr>";
echo "<td>".$row['groupid']."</td>";
echo "</tr>";
}

PHP syntax help on if statement

I'm trying to determine the right syntax from what I'm trying to do with MySQL. I'm basically saying that if a value in a certain column of a row of a table is equal to some session variables, I want to echo out info.
I have a table with subject, description and user. User is set by taking the current user's first name and last name and inserting it into the table under user. This is done by the following code:
$sql="INSERT INTO tbl_name (subject, description, user)
VALUES
('$_POST[subject]','$_POST[description]','$_SESSION[firstname] $_SESSION[lastname]')";
Then once I'm calling this data back out, I want to basically allow the user to delete content that they submitted themselves. The first step for me is to be able to display it in the table. I believe this is just syntax error, but I've confused myself now with how things are set up:
$sql = "SELECT * FROM tbl_name ORDER BY subject, description
LIMIT {$startpoint},{$limit}";
$result = $mysqli->query($sql);
$num_rows = mysqli_num_rows($result);
if($num_rows>0){
$field_num = $mysqli->field_count;
echo "<h1>HERE ARE SOME EXAMPLES:</h1>";
echo "<table border='0'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->subject}</td>";
echo "<td>{$field->description}</td>";
echo "<td>{$field->user}</td>";
if('$field->user' == '$_SESSION[firstname] $_SESSION[lastname]'){
echo '<td>You can delete this</td>';
}
}
I figured the $field->user would equal $_SESSION[firstname] $_SESSION[lastname] because that's how it was initially submitted to the table (without the '.' for concatenation).
Any help would be appreciated. Thanks!
EDIT
Here's the result of my table output code. The results are actually being display with $cell instead of from within the for loop I believe. I've added the if statement in after the but it doesn't seem to recognize echo "<td>".$field->user."</td>"; which makes me think that that is where the problem lies. What I would like to do is be able to add the if statement in a immediately after `echo {$field->user}"; to keep the code clean. I think I've confused myself thoroughly:
if($num_rows>0){
$field_num = $mysqli->field_count;
echo "<h1>HERE ARE SOME JOBS:</h1>";
echo "<table border='0'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->subject}</td>";
echo "<td>{$field->description}</td>";
echo "<td>{$field->user}</td>";
if($field->user == $_SESSION[firstname]." ".$_SESSION[lastname]){
echo '<td>You can delete this</td>';
}
else{
echo "<td>".$field->user."</td>";
echo "<td>".$_SESSION[firstname]." ".$_SESSION[lastname]."</td>";
}
}
echo "</tr>\n";
while($row = mysqli_fetch_row($result))
{
echo"<tr>";
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysqli_free_result($result);
}
else{
echo 'There are no jobs!';
}
I re-wrote the code in a way that was a little bit easier for me to understand (though maybe not the shortest way to do it):
while($row = mysqli_fetch_array($result))
{
echo"<tr>";
echo"<td>".$row['subject']."</td>";
echo"<td>".$row['description']."</td>";
echo"<td>".$row['user']."</td>";
if($row['user'] == $_SESSION['firstname']." ".$_SESSION['lastname']){
echo"<td>You can delete this</td>";
}
else{
echo"<td>Code didn't work</td>";
}
echo "</tr>\n";
}
It ended up working this way. If there's way to do this shorter then feel free to post it here otherwise thanks for the help!

Display SQL database query result as link

I have a search form on a previous page where I allow users to search for $q. I then query the database for 'keys' LIKE $q. The while loop displays the 'name' and 'weblink' of each matching database entry.
This all works correctly. However, I would like the 'weblink' to display as a clickable link. Ideally it would read as if it were HTML: 'weblink'. I cannot figure out the correct combo of php and html to make both the while loop, and the HTML work.
Any help would be appreciated.
Thanks in advance.
// query database
$query = mysql_query("SELECT * FROM `forumlist` WHERE `keys` LIKE '%$q%'");
// display query results
while($row = mysql_fetch_array($query))
{
echo $row['name'];
echo "<br/>";
echo $row['weblink'];
}
while($row = mysql_fetch_array($query))
{
echo $row['name'];
echo "<br/>";
echo '' . $row['weblink'] . '';
}

Running an If/Else PHP function with SQL

Thank you for reading my question.
I am trying to make a site where information from a database is displayed onto a webpage. The end result will look like this, but for a different game.
Here is a plain HTML page of what I want it to look like.
So far I know that my connection to the database works. When I run:
mysql_select_db("DATABASE", $con);
$result = mysql_query("SELECT * FROM DATABASE");
while($row = mysql_fetch_array($result)) {
echo $row['Title'] . " " . $row['Type'];
echo "<br />";
}
It returns the Title and Type.
What I want to do is run an If/Else statement that runs a different that block of code depending on the card type.
while($row = mysql_fetch_array($result)) {
if ($row['Title'] == 'Hero') {
echo "<div>";
}
}
I tried this based on the tutorials at w3schools.com but it doesn't work.
Do any of you have any ideas for what I should do?
EDIT:
Here is what I tried running:
while($row = mysql_fetch_assoc($result)) {
if ($row['Title'] == 'Hero') {
echo $row['Title'] . " Hero.<br>";
} else {
echo $row['Title'] . " Who cares.<br>";
}
}
Here is the output (Gimli should show up as a Hero):
For Gondor! Who cares.<br>
Bilbo Baggins Who cares.<br>
Ungoliant's Spwan Who cares.<br>
Gimli Who cares.
EDIT 2: Thank you Phil for spotting the error, I now get the result I wanted using Mikushi's method. Thank you all so much.
The fetching of your mysql result seems wrong, should be like this:
while($row = mysql_fetch_assoc($result)) {
if ($row['Title'] == 'Hero') {
echo ""; }
}
mysql_fetch_array fetch the result as an indexed array (1=> data, 2=> thing) , which explains why $row['Title'] doesn't work.
The difference:
http://ca2.php.net/mysql_fetch_array
http://ca2.php.net/mysql_fetch_assoc
Please, always refer to the documentation, it's very well done and a better source than w3cschools.
Maybe it's one of those all too obvious things but...
Shouldn't it be
if ($row['Type'] == 'Hero') // "Type", not "Title"

Categories