Multiple Search Pagination with PHP and MySQL - php

I have the following query below which uses the GET value of fname and lname to compare it to a database table:
$query = mysql_query("SELECT * FROM pages
WHERE `fname` = '$fname' OR `lname` = '$lname'
LIMIT $start, $perpage")
or die(mysqli_error());
When I run the code, it only puts into account the 'lname' = '$lname' statement.
What I would like it to do is search for either the fname or lname variables, or both if they both of them are set.

query with LIKE
$query = mysql_query("SELECT * FROM pages
WHERE `fname` LIKE '%$fname%' AND `lname` LIKE '%$lname%'
LIMIT $start, $perpage
");

Related

PHP order by clicks in WHILE

I am trying to order in while from higger count of clicks to the lower and I am a bit of lost so I decided to ask the qustion here.
My code :
$stmt = $db->query("SELECT DISTINCT `country` FROM `entries` ORDER by `id` ASC");
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch()) {
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
$conversations = $db->query("SELECT `id` FROM `conversations` WHERE `country` LIKE '{$row['country']}' AND `link_id` = '{$id}'")->rowCount();
}
I want the foreach give me results from highest count of $clicks to the lowest.
Any ideas what can I do ?
You can do this query entirely in SQL with something like this (depending on SQL dialect):
$countries = $db->query("SELECT `country`, count(1) AS clicks FROM `entries` GROUP BY `country` ORDER BY clicks DESC");
foreach ($countries as $country) {
echo "${country['country']} has ${country['clicks']} clicks. ";
}
put number of clicks in an array as below :
$clicks[] = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '{$row['country']}'")->rowCount();
then you can simply sort the array by rsort php function as below :
echo rsort($clicks);

If value from database on user is 0 then execute php code

Hi guys i'm quite new to PHP but I'm currently working on this site and I want it to check if the total_online_time for the user is 0 or less then it should execute the script which will create a room for the user. However after trying with this script the .php page won't load.
http://pastebin.com/XDLhFvtE
<?php
$getStats = mysql_query("SELECT total_online_time FROM `users` WHERE id = '".$_SESSION['user']['id']."'");
if ($getStats > 0)
$getRoomKit = mysql_fetch_assoc(mysql_query("SELECT * FROM room_starterkit ORDER BY RAND() LIMIT 1"));
$getRoom = mysql_fetch_assoc(mysql_query("SELECT * FROM `rooms` WHERE id = '".$randomRoomID['id']."' LIMIT 1"));
$welcomeMessage = "Welcome " . $_SESSION['user']['username'];
mysql_query("INSERT INTO `rooms` (caption,owner_id,description,model_name,wallpaper,floor) VALUES ('".$welcomeMessage."', '".$_SESSION['user']['id']."', 'Welcome to Hablore', '".$getRoomKit['room_Model']."', '".$getRoomKit['room_Wallpaper']."', '".$getRoomKit['room_Floor']."') ");
$theRoomID = mysql_fetch_assoc(mysql_query("SELECT id FROM `rooms` ORDER BY `id` DESC LIMIT 1"));
mysql_query("UPDATE `users` SET `home_room` = '".$theRoomID['id']."' WHERE `id` = '".$_SESSION['user']['id']."'");
$getRoomItems = mysql_query("SELECT * FROM room_itemkits WHERE roomKit = '".$getRoomKit['id']."'");
while($CurItem = mysql_fetch_assoc($getItems)){
mysql_query("INSERT INTO `items` (user_id, room_id, base_item, extra_data, x, y, z, rot, wall_pos) VALUES ('".$_SESSION['user']['id']."','".$lastRoomID['id']."','".$CurItem['base_item']."','".$CurItem['extra_data']."','".$CurItem['x']."','".$CurItem['y']."','".$CurItem['z']."','".$CurItem['rot']."','".$CurItem['wall_pos']."')");
}
}
?>

Query for If search value equals both rows?

I have this code pretty much like a search engine within the database for peoples names.
if (isset($_POST['submit'])){
$keyword = $_POST['stats'];
$orderby = $_POST['orderby'];
if (!empty($_POST['stats'])) {
$getStats = $db->query("SELECT * FROM `stats` WHERE
`lastname` LIKE '%$keyword%' OR `firstname` LIKE '%$keyword%' OR
`nickname` LIKE '%$keyword%' ORDER BY `$orderby`
DESC");
This then prints the results back into a table, I thought the table code wasn't necessary and too long.
The above query works for if I search just the last name or just the first name, or nickname
but if there is for example a user in the database with the name, John Smith
so
Firstname: John
Lastname: Smith
If just searched 'John' he would be printed into the table, which is good and same if I just searched 'Smith'
But if I search 'John Smith' he would not be printed into the table.
How can I change this query so that this will happen, I have tried this:
$getStats = $db->query("SELECT * FROM `stats` WHERE
`firstname`, `lastname` = '$keyword' OR `lastname` LIKE '%$keyword%' OR `firstname` LIKE '%$keyword%' OR
`nickname` LIKE '%$keyword%' ORDER BY `$orderby`
DESC");
WHERE CONCAT(firstname, ' ', lastname) LIKE %$keyword%
Also you should be binding parameters rather than directly interpolating user input into the query string, your current code is vulnerable to SQL injection.
$keyword = str_replace(" ", "%", $keyword);
You can try REGEXP:
$keyword = $db->real_escape_string($_POST['stats']); // escape data
$orderby = $db->real_escape_string($_POST['orderby']); // escape data
$keyword = implode("|", explode(" ", $keyword));
$getStats = $db->query("SELECT * FROM stats
WHERE firstname REGEXP '$keyword'
OR lastname REGEXP '$keyword'
OR nickname REGEXP '$keyword'
ORDER BY $orderby DESC");
try this
$sql = "SELECT *
FROM stats
WHERE
firstname LIKE '%$keyword%'
OR lastname LIKE '%$keyword%'
OR CONCAT_WS(' ',firstname,lastname,) LIKE '%$keyword%'
OR CONCAT_WS(' ',lastname,firstname) LIKE '%$keyword%'
OR nickname LIKE '%$keyword%'
ORDER BY $orderby DESC";

PHP Mysql Search Query

Hello i have a simple search query, what i'm facing is when someone writes the only first name of the user that he wants to search, my query finds it, also when someone only writes the last name in the input and posts it, it also shows that too, but when user writes first name and last name together in the input, it can't find the user even he/she exists. The last part of $q query where i wrote first name and last name like part doesnt work i know there my logic is bad, but how can i fix that
try {
$q = "SELECT * FROM `members` WHERE `first_name` LIKE :search_string OR `last_name` LIKE :search_string OR `first_name` AND `last_name` LIKE :search_string";
$q_do = $db->prepare($q);
$q_do->execute( array("search_string"=>'%'.$query.'%') );
$number = $db->query("SELECT FOUND_ROWS()")->fetchColumn();
} catch(PDOException $e) {
$log->logError($e." - ".basename(__FILE__));
}
Thank you
Try using concat:
$q = "SELECT * FROM `members` WHERE `first_name` LIKE :search_string
OR `last_name` LIKE :search_string
OR concat(`first_name` , ' ', `last_name`) LIKE :search_string";
SELECT *
FROM `members`
WHERE `first_name` LIKE :search_string
OR `last_name` LIKE :search_string
OR `first_name` AND `last_name` LIKE :search_string;
ANDis an operator not a concatenator.
SELECT *
FROM `members`
WHERE `first_name` LIKE :search_string
OR `last_name` LIKE :search_string
OR CONCAT(`first_name`,' ', `last_name`) LIKE :search_string;
So what you do no is:
User enters 'First Last'
You search :
First like '%First Last%' or Last like '%First Last%' ...
You need to use full text search index.
http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html
or something like
http://sphinxsearch.com/
Try this:
$query = explode(" ", $query);
if(count($query)>1){
$fname = $query[0];
$lname = end($query);
}else{
$fname = $query[0];
$lname = $query[0];
}
$q = "SELECT * FROM `members` WHERE `first_name` LIKE :fname OR `last_name` LIKE :lname";
$q_do = $db->prepare($q);
$q_do->execute( array('fname' => "%$fname%", 'lname' => "%$lname%") );
The simplest Search Query for you.. Try this its working man.
SELECT * FROM TableName WHERE title like '%Your Search Text%'

variable in MYSQL requery with REGEXP

HI, I am trying the below code .
$domain = $_GET['url'];
$sql = 'SELECT * FROM `domains` WHERE `domain` REGEXP CONVERT(_utf8 \'$url\' USING latin1) COLLATE latin1_swedish_ci';
$result = mysql_query($sql);
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
echo "id :{$row[0]} <br>";
}
will get the domain from user using $_GET and then regex that from database ..
This query is not working , please tell me the proper syntax
I even tried double quotes \"$domain"\
$sql = "SELECT * FROM `domains` WHERE `domain` REGEXP CONVERT(_utf8 '".$url."' USING latin1) COLLATE latin1_swedish_ci";

Categories