Search instruction in PHP not working - php

I'm trying to search a value in a specific mysql database column via PHP. However, it's not working. I tried many different ways and I had no success so far.
I'm trying the following code:
<?php
require('connect.php');
$search= 'search';
$query = "SELECT * FROM `user1` WHERE `description` LIKE '%'a'%'";
if($result){
$smsg = $query;
}else{
$fmsg ="Sorry, nothing was found. Try using our menu on top left.";
}
?>

Change it:
"SELECT * FROM `user1` WHERE `description` LIKE '%'a'%'";
to
"SELECT * FROM `user1` WHERE `description` LIKE '%a%'";
and try again. It will return you all the rows in which description column contains character a.

Related

Php script to search a record from database by name. (filter by name)

Can someone correct the code for filter a record by its name. I know the query but perhaps I'm not implementing it properly.
Here is my code. I want to either search by city or simply put a name in textbox to search an hospital. search-by-name is for an input field where I am supposed to write the name I want to search from database. I want to make both options available. How should I implement it correctly, as this one won't work for me.
if (isset($_POST['search'])) {
if (isset($_POST['search-by-city'])) {
$city_id = $_POST['search-by-city'];
$query = "SELECT * FROM `hospitals` WHERE `City_ID` LIKE '$city_id'";
$result = mysqli_query($con,$query);
if (isset($_POST['search-by-name'])) {
$hospital_name = $_POST['search-by-name'];
$query = "SELECT * FROM `hospitals` WHERE `Name` LIKE '$hospital_name'";
$result = filterTable($query); {
if (mysqli_num_rows($result) == 0) {
echo '<div class="col-md-12"> <h2>No recod Found</h2> </div> ';
}
}
}
while($row = mysqli_fetch_array($result)){
$city_id = $row[3];
$query = "SELECT `Name` FROM `cites` WHERE `ID` LIKE '$city_id'";
$result2 = mysqli_query($con,$query);
$row2 = mysqli_fetch_row($result2);
$city_name = $row2[0];
echo '<div class="col-md-4"><h3>'.$row[1].'</h3><h4>'.$city_name.'</h4><h4>'.$row[2].'</h4><h5>'.$row[3].'</h5><h5>'.$row[4].'</h5>
';
}
}
I'm guessing your query should be:
"SELECT * FROM `hospitals` WHERE `Name` LIKE '%$hospital_name%'
Checkout the mySQL manual on string comparison.
Also please don't use $_POST variables directly in SQL queries, that is a major security issue. (Search for sql-injection.)

PHP Echo _GET value to MySQL and filter results

I have a search result page with a simple form to filter order of results, I want the form to filter:
ORDER BY - ASC or DESC - and PER PAGE LIMIT in the MySQL query
Here is the code to filter the order by I am trying to use
$order_by = mysqli_real_escape_string($database,$_GET['order_method']);
$query = mysqli_query($database,"SELECT * FROM `products`
order by `<?php if(empty($order_by)){echo "id";}else{echo "$order_by"; ?>` ASC");
It is not working ... I get errors in >php line 22 and this line is the line of code above
The idea is that if the user comes to the default page I obviously get no $order_by so in this case the order by will be the default
echo "id"
But if the customer uses the html form to filter the results and I get the "order_by" the mysql query order by changes to the value the customer sends using the html form in this case
echo "$order_by"
I am trying many ways to do this but no one seems to work, any ideas would help a lot
Your code:
$order_by = mysqli_real_escape_string($database,$_GET['order_method']);
$query = mysqli_query($database,"SELECT * FROM `products`
order by `<?php if(empty($order_by)){echo "id";}else{echo "$order_by"; ?>` ASC");
The problem:
<?php if(empty($order_by)){echo "id";}else{echo "$order_by"; ?>
You are already in a PHP code block. You need to build up the query string using concatenation:
Try:
$order_by = mysqli_real_escape_string($database,$_GET['order_method']);
$sql = "SELECT * FROM `products` order by `";
if(empty($order_by)){
$sql .= "id";
} else {
$sql .= $order_by;
}
$sql .="` ASC";
// Now you can execute the query
$query = mysqli_query($database,$query);

How to display field from MySQL?

I'm trying to display a field from my MySQL database. It's in the table tblproducts in the row with the id is set to 1. And under the column qty.
This is the code I'm using:
<?php
mysql_connect("localhost","username","password");
mysql_select_db("database_name");
$available = "SELECT qty FROM tblproducts WHERE id = 1";
$result = mysql_query($available);
echo $result;
?>
However, I keep getting this message: Resource id #2
I've done a bit of research and seen where other people are having similar problems but most of them are trying to display their data in an HTML table whereas I just need the data from 'qty' to display. And of course I'm definitely not a MySQL guru.
Can anyone help me out with this please?
Try changing this:
$result = mysql_query($available);
To this:
$result = mysql_result(mysql_query($available), 0);
Let's start from the start. (I'll assume you have the connection set)
Form the query
$query = "SELECT `qty`
FROM `tblproducts`
WHERE `id` = 1";
Execute the query
$run = mysql_query($query);
Now, put the result in an assoc array
$r = mysql_fetch_array($run);
See the contents of the array
echo $r['qty'];
It's also advised that you move up from mysql to either mysqli, or PDO. PDO is preferred as you're not bound to the MySQL database model.
Try this:
Here you need to generate associative array and then get the resulting row.
$query = "SELECT `qty` FROM `tblproducts` WHERE `id` = 1";
$run = mysql_query($query);
$r = mysql_fetch_array($run);
echo $r['qty'];
-
Thanks

retrieve id value from sql database in php which IS NOT last inserted

I have a database that is designed for Football players and so have the table with the following fields:
Person_ID
First_Name
Surname
NicName
Address_Line_1
Contact_Number
Date_of_birth
Postcode
I need to extract the Person_ID for a player without actually entering the ID number as players will not know their individual number and is designed to be used just by the system.
I have the My sql code for selecting a player when certain values are entered:
SELECT `Person_ID` FROM `person` WHERE `First_Name` = 'A Name' and `Surname` = 'Another Name'
This does not however return very well within php when placed into a function. The function I currently have is shown below (php)
function showid($fname, $sname, $d) {
$sql = "SELECT `Person_ID` FROM `person` WHERE `First_Name` = '$fname' and `Surname` = '$sname'";
$result = mysqli_query($d, $sql);
if (!$result)
print ("$sql failed".mysqli_error($d));
else {
print ("$fname $sname is selected<br>");
}
}
$name and $sname are values which will be entered by the user and they will then be able to transfer to a different team or update their account etc but I need to have the ID selected so that further functions and queries can work fully.
If you want to fetch the ID of the selected player, use the fetch_array function
http://php.net/manual/en/mysqli-result.fetch-array.php
function showid($fname, $sname, $d) {
$sql = "SELECT `Person_ID` FROM `person` WHERE `First_Name` = '$fname' and `Surname` = '$sname'";
$result = mysqli_query($d, $sql);
if (!$result)
print ("$sql failed".mysqli_error($d));
else {
print ("$fname $sname is selected<br>");
}
$row = $result->fetch_array(MYSQLI_ASSOC);
echo "Your ID is" . $row['Person_ID'];
}
This of course assumes there is only one result (otherwise we would have to loop) so you might want to check $result->num_rows is equal to 1 and return an error if it isnt (assuming you arent using UNIQUE in your database)

mysql select query doesn't work when trying to select a single row

I am trying to create a php script that selects one row from my table by using the WHERE clause. The problem is the mysql query returns no rows. I know the variable is correct (its user submitted).
$title = mysql_real_escape_string($_REQUEST["title"]);
$query = mysql_query("SELECT * FROM links WHERE title ='$title'", $con)
or die ("Error: " . mysql_error());
I'm looking for any ideas that could fix my problem. I know the mysql is working properly because other queries execute fine. The title variable is correct; it is passed from a mysql on another page.
ps - I posted a similar question earlier, but worded it poorly and got results that didn't address the problem
Try this:
$query = mysql_query("SELECT * FROM links WHERE title ='$title' limit 1")
or die ("Error: " . mysql_error());
Sorry, I'm new here and I couldn't find the button to comment on the original question.
But you mentioned the request was user submitted. Are they typing it or is it a selection like from a select box or radio button? I'm asking because does the requested title even exist in the DB?
Anyway, what is your result if you use the following?:
$query = mysql_query("SELECT * FROM links WHERE title LIKE '%".$title."%'")
or die ("Error: " . mysql_error());
If not, then there's definitely no match in the DB.
try this query
mysql_query("SELECT * FROM links WHERE title like '$title%'", $con)
Try this query:
$query = mysql_query("SELECT * FROM links WHERE title LIKE '%{$title}%'");
or maybe this to check formatting:
$sql = sprintf("SELECT * FROM links WHERE title LIKE '%%%s%%'", $title);
$query = mysql_query($sql);
In my case I had empty space before and after the variable name, like this
$query = "select * from user where user_name = ' $user_name ' ";
and this results in comparing userName with [empty_space userName empty_space ] which doesn't exist in the database.
your query should be like this
$query = "select * from user where user_name = '$user_name' ";

Categories