Select and insert in one query SQL - php

I have a web page where a user clicks on a map, the latitute and longitude are passed to text fields, and along with some other details the data is submitted to the database. I have a formula for calculating the nearest town, and I have a little sample table of towns with their latitudes and logitudes.
At the moment I can display the nearest town and distance, but I can't get it to send to the database. I think I need to put the two queries together but I don't know how. Please be kind, I know my code is terrible and I have lots of security issues which I'm going to try to address later. Thank you!
<?php
$conn = Connect();
/**
* Use the Haversine Formula to display the 100 closest matches to $origLat, $origLon
* Only search the MySQL table $tableName for matches within a 10 mile ($dist) radius.
*/
$origLat = $conn->real_escape_string($_POST['lat']);
$origLon = $conn->real_escape_string($_POST['lng']);
// This is the maximum distance (in miles) away from $origLat, $origLon in which to search
$dist = 50;
$query = "SELECT Townland, lat, lng, 3956 * 2 *
ASIN(SQRT( POWER(SIN(($origLat - lat)*pi()/180/2),2)
+COS($origLat*pi()/180 )*COS(lat*pi()/180)
*POWER(SIN(($origLon-lng)*pi()/180/2),2)))
as distance FROM townland WHERE
lng between ($origLon-$dist/cos(radians($origLat))*69)
and ($origLon+$dist/cos(radians($origLat))*69)
and lat between ($origLat-($dist/69))
and ($origLat+($dist/69))
having distance < $dist ORDER BY distance limit 1";
$result = mysqli_query($conn, $query) or die(mysql_error());
while($row = mysqli_fetch_assoc($result)) {
echo $row['Townland']." > ".$row['distance']."<BR>";
}
$User_ID = $conn->real_escape_string($_POST['User_ID']);
$Species_Name = $conn->real_escape_string($_POST['Species_Name']);
$No_Of_Birds = $conn->real_escape_string($_POST['No_Of_Birds']);
$newdate = $conn->real_escape_string($_POST['Sighting_Date']);
//Converts date to 'yyyy-mm-dd' acceptable to mysql
$newdate=date('Y-m-d',strtotime($newdate));
$Sighting_Details = $conn->real_escape_string($_POST['Sighting_Details']);
$lat = $conn->real_escape_string($_POST['lat']);
$lng = $conn->real_escape_string($_POST['lng']);
$townland = $row['Townland'];
$query = "INSERT into sighting
(User_ID, Species_Name, No_Of_Birds,
Sighting_Date, Sighting_Details,
lat, lng, Townland)
VALUES('" . $User_ID . "','" . $Species_Name . "','" . $No_Of_Birds . "','"
. $newdate . "','" . $Sighting_Details . "','"
. $lat . "','" . $lng . "','" . $townland . "')";
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
header("Location: ../View/thankYouSubmitSighting.php");
$conn->close();
exit();
while($row = mysqli_fetch_assoc($result)) {
echo $row['Townland']." > ".$row['distance']."<BR>";
}
mysqli_close($conn);
?>

If you need insert the result from query in a table eg: my_table
could be you need an insert select query ( a single sql command composed by two part )
"INSERT into my_table (Townland, lat, lng, distance)
SELECT Townland, lat, lng, 3956 * 2 *
ASIN(SQRT( POWER(SIN(($origLat - lat)*pi()/180/2),2)
+COS($origLat*pi()/180 )*COS(lat*pi()/180)
*POWER(SIN(($origLon-lng)*pi()/180/2),2)))
as distance FROM townland WHERE
lng between ($origLon-$dist/cos(radians($origLat))*69)
and ($origLon+$dist/cos(radians($origLat))*69)
and lat between ($origLat-($dist/69))
and ($origLat+($dist/69))
having distance < $dist ORDER BY distance limit 1";

Related

images pagination - how to get all params in one single query

I have a pagination system for viewing images from database.
So each time I click on a next / prev buttons I need three variables
Question - is there a way to get them in one single query and not using a separate query for each of them?
$lm = 100; // limit per page
$off = 0; // offset - click on next prev buttons
$dim = '960 x 540'; // img dimensions
function get_images($lm, $off, $dim){
global $db;
//firstly I need total number of images
$st = $db->query("select count(*) as cnt from images");
$total = $st->fetchColumn();
//then I need number of 960 x 540 images
$st = $db->query("select * from images where dim = '" . $dim . "'");
$insel = $st->rowCount();
//then I need number of viewed images on a pagination system
$st = $db->query("select * from images where dim = '" . $dim . "' order by date desc limit " . $lm . "
offset " . $off);
$inview = $st->rowCount();
$st = $db->query("select * from images where dim = '" . $dim . "' count(id) over(partition by id) as cnt count(dim) over(partition by dim) as insel order by date desc limit " . $lm . " offset " . $off);
But make sure you are using MySQL version greater than or equal to 8.0

Slow sql select. Two tables

My script is running too slow when searching zipcodes within 30 miles of 90210. It was duplicating some even though there are no dups in the zipcode database too, thus the DISTINCT u.id. Besides selecting only the columns I need, any ideas on how to speed this thing up?
<?php
$zipcode = queryDB("SELECT * FROM zipcodes WHERE zipcode='$location' LIMIT 1", 'a');
$distance = 30;
$sql = "SELECT DISTINCT zipcode, ( 3959 * acos( cos( radians(".$zipcode['lat'].") ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(".$zipcode['lon'].") ) + sin( radians(".$zipcode['lat'].") ) * sin( radians( lat ) ) ) ) AS distance FROM zipcodes HAVING distance <= $distance ORDER BY distance";
$zipcodes = queryDB($sql, 'r');
$sql = '';
while($row = mysqli_fetch_assoc($zipcodes)){
$sql .= "u.category='$category' AND u.zipcode='".$row['zipcode']."' AND u.zipcode = l.zipcode";
if($_GET['photos'] == 'y'){ $sql .= " AND photo_1!=''"; }
$sql .= " OR ";
}
$sql = rtrim($sql, ' OR ');
$profiles = queryDB("SELECT DISTINCT u.id, u.*, l.city, l.state, l.zipcode FROM user_accounts as u, zipcodes as l WHERE $sql ORDER BY date_added DESC", 'r');
while($row = mysqli_fetch_assoc($profiles)){
//this is where i display the profile information
} ?>
UPDATE: The biggest issue was not using a join on my user_accounts select. Here's my updated sql query.
$sql = '';
$zipcodes = new ZipCodesRange($location, $distance);
foreach ($zipcodes->zipCodes as $zipcode){
$sql .= "(u.category='$category' AND u.zipcode='".$zipcode['ZIPCODE']."'";
if($_GET['photos'] == 'y'){ $sql .= " AND photo_1!=''"; }
$sql .= ") OR ";
}
$sql = rtrim($sql, ') OR ');
$sql .= ")";
$profiles = queryDB("SELECT u.id, u.photo_1, u.name, u.business, u.date_added, l.city, l.state, l.zipcode FROM user_accounts as u INNER JOIN zipcodes as l ON u.zipcode = l.zipcode WHERE $sql ORDER BY date_added DESC", 'r');
Per #malias from this other question explaining the haversine formula, here is a document showing different ways to improve the speed of this sql: http://tr.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
The main goal is to limit the zipcodes being searched so that you don't have to calculate the haversine of so many records. The suggestion was to add a WHERE clause to filter the bounds of the latitude and longitude. Table indexes can be used for this rectangular filter, then the haversine can be used to filter it as a sphere.

How to optimize queries in mysql

My mysql query for search is :
$result = mysql_query("SELECT * FROM room_tb WHERE location ='{$location}' AND price BETWEEN '$minprice' AND '$maxprice' ")or die('Could not connect: ' . mysql_error()); ;
This makes a compulsion to enter both the location and min and max price in form.
I want a query that can make user to enter either location or max and min price, as well as allow user to search by both fields. What should i do?
When I am generating my queries with optional fields, I create an array of each field, then join them with implode
$query_array = array();
$queries_stringed = '';
if(strlen($location) > 0){
$query_array[] = "location ='{$location}'";
};
if(strlen($minprice) > 0 && strlen($maxprice) > 0){
$query_array[] = "(price BETWEEN '$minprice' AND '$maxprice')";
};
if(!empty($query_array)){
$queries_stringed = "WHERE ".implode(" AND ", $query_array);
};
$result = mysql_query("SELECT * FROM room_tb $queries_stringed");
Thsi ought to do it for you :
$query = "SELECT * FROM room_tb ";
if($location){
$query .= " WHERE location ='{$location}' ";
}
if(($minprice)&&($maxprice)&&(!$location)){
$query .= " WHERE price BETWEEN '$minprice' AND '$maxprice'";
}
if(($minprice)&&($maxprice)&&($location)){
$query .= " AND price BETWEEN '$minprice' AND '$maxprice'";
}
$result = mysql_query($query)or die('Could not connect: ' . mysql_error());
Cheers
As an addition to those answers - you shouldn't trust users inputs and should escape given strings or use PDO instead of mysql_ functions

Count records from multiple tables in real-time

I want to count a record by current date from different tables and return as one row with different column in the new table. The code will update a record every three hours and insert new record if current date changes. I've current date and time data (2013-05-20 14:12:12) in "created_at" column. Here my current code:
require_once('./db_connect.php');
$dbcon = new db;
//test to see if a specific field value is already in the DB
public function in_table($table,$where) {
$query = 'SELECT * FROM ' . $table . ' WHERE ' . $where;
$result = mysqli_query($this->dbh,$query);
$this->error_test('in_table',$query);
return mysqli_num_rows($result) > 0;
}
//running in background
while (true) {
$select= "SELECT (SELECT CURDATE()) AS time," .
"(SELECT COUNT(tweet_id) FROM tweets WHERE created_at= 'CURDATE() %') AS total_count," .
"(SELECT COUNT(fid) FROM fun WHERE ftime= 'CURDATE() %') AS f_count," .
"(SELECT COUNT(sid) FROM sad WHERE stime= 'CURDATE() %') AS s_count";
$results = mysqli_query( $dbcon, $select );
while($row = mysqli_fetch_assoc($result)) {
$time = $row['time'];
$total = $row['total_count'];
$fcount = $row['f_count'];
$scount = $row['s_count'];
$field_values = 'time = "' . $time . '", ' . 'total_count = ' . $total . ', ' . 'fun_count = ' . $fcount . ', ' . 'sad_count = ' . $scount;
if ($dbcon->in_table('count','time= "' . $time . '"')) {
$update = "UPDATE count SET $field_values WHEN time= '$time'";
mysqli_query( $dbcon, $update );
}
else {
$insert = "INSERT INTO count SET $field_values";
mysqli_query( $dbcon, $insert );
}
}
//update record every 3 hour
sleep(10800);
}
With this code I can't get a count record. The result return | 2013-05-18 | 0 | 0 | 0 |. How can I correct this?
I not familiar with PHP, but you can retrieve the count of all records dated any time today using:
SELECT COUNT(tweet_id)
FROM tweets
WHERE created_at >= curDate()
AND created_at < date_add(curDate(), interval 1 day)
It is equivalent to saying
..
WHERE created_at >= (today at midnight *incusive*)
AND created_at < (tomorrow at midnight *exclusive*)
Update:
The advantage of this method is it is index friendly. While using WHERE DATE(Column) = currDate() works, it can prevent the database from using indexes on that column, making the query slower.
Replace the parts where you have this:
WHERE created_at= 'CURDATE() %'
with this:
WHERE DATE(created_at) = CURDATE()
Your existing WHERE clause is comparing created_at to the string constant CURDATE() %, and they'll never match.
You are comparing against created_at= 'CURDATE() %', which is looking for that exact string, not for the result of a function. If the field created_at is a date, it will never match.
And, you are doing that for all counts.

haversine formula php / mysql

I'm trying to get a common database of geo points working with a radius search.
I've found several good tutorials on this topic, but I'm failing at the very end.
The main tutorial is here: http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates.
The basic formula, in the form of an SQL query, is
SELECT * FROM Places
WHERE (Lat => 1.2393 AND Lat <= 1.5532) AND (Lon >= -1.8184 AND Lon <= 0.4221)
AND acos(sin(1.3963) * sin(Lat) + cos(1.3963) * cos(Lat) * cos(Lon - (-0.6981)))
<= 0.1570;
I've implemented this in a simple PHP test page like this:
$R = 6371; // radius of Earth in KM
$lat = '46.98025235521883'; // lat of center point
$lon = '-110.390625'; // longitude of center point
$distance = 1000; // radius in KM of the circle drawn
$rad = $distance / $R; // angular radius for query
$query = '';
// rough cut to exclude results that aren't close
$max_lat = $lat + rad2deg($rad/$R);
$min_lat = $lat - rad2deg($rad/$R);
$max_lon = $lon + rad2deg($rad/$R/cos(deg2rad($lat)));
$min_lon = $lon - rad2deg($rad/$R/cos(deg2rad($lat)));
// this part works just fine!
$query .= '(latitude > ' . $min_lat . ' AND latitude < ' . $max_lat . ')';
$query .= ' AND (longitude > ' . $min_lon . ' AND longitude < ' . $max_lon . ')';
// refining query -- this part returns no results
$query .= ' AND acos(sin('.$lat.') * sin(latitude) + cos('.$lat.') * cos(latitude) *
cos(longitude - ('.$lon.'))) <= '.$rad;
Am I missing something here? I think I'm following the methodology exactly, but I cannot get the "fine tuning" query to return any results.
not sure but :
$R = 6371; // radius of Earth in KM
$lat = '46.98025235521883'; // lat of center point
$lon = '-110.390625'; // longitude of center point
$distance = 1000; // radius in KM of the circle drawn
$rad = $distance / $R; // angular radius for query
$query = '';
// rough cut to exclude results that aren't close
$radR = rad2deg($rad/$R);
$max_lat = $lat + radR;
$min_lat = $lat - radR;
$radR = rad2deg($rad/$R/cos(deg2rad($lat)));
$max_lon = $lon + radR;
$min_lon = $lon - radR;
// this part works just fine!
$query .= '(latitude > ' . $min_lat . ' AND latitude < ' . $max_lat . ')';
$query .= ' AND (longitude > ' . $min_lon . ' AND longitude < ' . $max_lon . ')';
// refining query -- this part returns no results
$query .= ' AND acos(sin('.deg2rad($lat).') * sin(radians(latitude)) + cos('.deg2rad($lat).') * cos(radians(latitude)) *
cos(radians(longitude) - ('.deg2rad($lon).'))) <= '.$rad;

Categories