PHP Select Value in MySQL Using Greater Than and Less Than - php

I have been racking my brains trying to get something that seems simple to work. I have a TABLE "weight". Weight has 3 columns "shipping_to", "shipping_from", and "shipping_cost".
Shipping_to and Shipping_from are the weight values and shipping cost holds the shipping cost if the value is greater than or equal to X AND less than or equal to X.
I have written this a million different ways and for the life of me it won't work.
UPDATED:
The Script works "kind of" but it never returns a success response of 1 and it never fimds the value of X even though I have manually put these values into my MySQL db.
PHP SCRIPT:
if($The_Function=="SHIPPING_COST"){
$response = array();
require_once __DIR__ . '/db_connect.php';
$con = new DB_CONNECT();
$ship_weight = 1;
$result = mysql_query("SELECT * FROM weight WHERE from_weight >= '$ship_weight' AND to_weight <= '$ship_weight'");
if(!empty($result)){
if (mysql_num_rows($result) > 0) {
$response["userID"] = array();
while ($row = mysql_fetch_array($result)) {
$custID = array();
$custID["shipping_cost"] = $row["shipping_cost"];
array_push($response["userID"], $custID);
}
$response["success"] = 1;
echo json_encode($response);
}else {
$response["success"] = 0;
$response["message"] = "No shipping found";
echo json_encode($response);
}
}else {
$response["success"] = 0;
$response["message"] = "No shipping found";
echo json_encode($response);
}
}

The error was in the Query itself. All I had to do was give the last Column something to compare to so I just added $ship_weight again. Here is the code.
$result = mysql_query("SELECT * FROM weight WHERE '$ship_weight' >= from_weight AND '$ship_weight' <= to_weight");

I think the problem is that $result will never be empty()
If the query works if will be a resource handle and if it fails it will be false.
So try this:
$result = mysql_query("SELECT * FROM weight WHERE from_weight >= '$ship_weight' AND to_weight <= '$ship_weight'");
if($result !== FALSE){
if (mysql_num_rows($result) > 0) {
$response["userID"] = array();
while ($row = mysql_fetch_array($result)) {
$custID = array();
$custID["shipping_cost"] = $row["shipping_cost"];
array_push($response["userID"], $custID);
}
$response["success"] = 1;
echo json_encode($response);
}else {
$response["success"] = 0;
$response["message"] = "No shipping found";
echo json_encode($response);
}
}else {
$response["success"] = 0;
$response["message"] = "No shipping found";
echo json_encode($response);
}

Is this a copy and paste of your code? If so, look at this line:
$result = mysql_query("SELECT * FROM weight WHERE from_weight >= '$ship_weight' AND to_weight <= '$ship_weight'");
PHP is considering your $ship_weight variable as part of the string. Change it to:
$result = mysql_query("SELECT * FROM weight WHERE from_weight >= '".$ship_weight."' AND to_weight <= '".$ship_weight."'");
Also, mysql_* is deprecated. Take a look at the mysqli_* extension.

Related

PHP JSON not displaying MySQL result

The PHP I created retrieve data from a MySQL database and turns it into a JSON which is then echoed. I had 300 records and the PHP was able to display the JSON and was visible when viewed.
However, I added another 100 records to the same table and for some reason the JSON isn't being displayed. It just appears blank with no error. But when I remove the 100 records, the JSON displays as normal.
I haven't touched the PHP file during this. What could the reason be?
<?PHP
include_once("connection.php");
$query = "select id,mosque_name,latitude,longitude from mosques;";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
$response["mosques"] = array();
while ($row = mysqli_fetch_array($result)) {
$mosque = array();
$mosque["id"] = $row["id"];
$mosque["mosque_name"] = $row["mosque_name"];
$mosque["latitude"] = $row["latitude"];
$mosque["longitude"] = $row["longitude"];
array_push($response["mosques"], $mosque);
}
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No mosques found";
echo json_encode($response);
}
?>
Try This:-
<?PHP
include_once("connection.php");
$query = "select id,mosque_name,latitude,longitude from mosques;";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) == 0) {
echo json_encode("");
}
else{
while ($row = mysqli_fetch_array($result)) {
/*$mosque = array();
$mosque["id"] = $row["id"];
$mosque["mosque_name"] = $row["mosque_name"];
$mosque["latitude"] = $row["latitude"];
$mosque["longitude"] = $row["longitude"];*/
$fetchRow[]=$row;
// array_push($response["mosques"], $mosque);
}
echo json_encode($fetchRow);
}
/* $response["success"] = 1;
echoing JSON response
echo json_encode($response);*/
?>
and check your result from network

database query, compare each two rows but getting only first two rows

Hi i am querying my db so that i can compare every two rows. ex 1 and 2, then 2 and 3, then 3 and 4. and so on and so forth. but it only compares the first two rows. any ideas? here is my code:
$result = mysql_query("SELECT *FROM attendance ORDER BY pid ASC") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// $response["nominees"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$prev_sid = $row["sid"];
$prev_pid = $row["pid"];
$row2 = mysql_fetch_array($result);
if($row2["pid"] == $prev_pid){
if(($row2["sid"] - $prev_sid) == 1){
$attended_elec = mysql_query("SELECT pid FROM election_attendance where election_id = '$election_id'");
if(mysql_num_rows($attended_elec) > 0){
$not_officer = mysql_query("SELECT usertype FROM users WHERE pid = '$prev_pid'");
if(mysql_result($not_officer, 0) == "member"){
// echo "PID" . $prev_pid;
// $nominee["pid"] = $row2["pid"];
$user_details = mysql_query("SELECT *FROM users WHERE pid = '$prev_pid'");
if(mysql_num_rows($user_details) > 0){
$response["nominees"] = array();
while ($row3 = mysql_fetch_array($user_details)) {
$nominee = array();
$nominee["pid"] = $row3["pid"];
$nominee["firstname"] = $row3["firstname"];
$nominee["lastname"] = $row3["lastname"];
$nominee["gender"] = $row3["gender"];
$nominee["contact"] = $row3["contact"];
$nominee["email"] = $row3["email"];
$nominee["address"] = $row3["address"];
$nominee["institution"] = $row3["institution"];
$nominee["points"] = $row3["points"];
$nominee["usertype"] = $row3["usertype"];
// push single product into final response array
array_push($response["nominees"], $nominee);
}
}
}
}
}
}
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "There is no candidate yet from the records.";
// echo no users JSON
echo json_encode($response);
}
thanks in advance, have a nice day
Theres a problem right at the while(), you're fetching the 1st row, and is correct.
Then, inside it you fetch the 2nd, which is what you intend, but when the iteration is repeating, the while will fetch the 3rd, and the inner the 4th, so you lost there the comparisson between 2nd and 3rd.
// fetch data from DB
$results = "...";
if (mysql_num_rows($result) < 1)
{
// no products found tell your users
return;
}
// lets make some variables to walk through the list
$previous = mysql_fetch_array($results);
$actual = null;
// and now lets walk through the list
while($actual = mysql_fetch_array($results))
{
// execute your logic here
// than at the end move the actual row to become the previous
$previous = $actual;
}
NOTICE you shouldn't use mysql_ * methods they're are deprecated. you can use the brother mysqli_ * or PDO
I would do something like this? But there is almost certainly a less resource intensive way to do it.
$x = 0;
$y = 1;
$total = mysql_num_rows(mysql_query("SELECT * FROM `the_place`");
while ($x < $total AND $y < total){
$query1 = "SELECT * FROM `the_place` LIMIT $x,1";
$query2 = "SELECT * FROM `the_place` LIMIT $y,1";
$row1 = mysql_fetch_array(mysql_query($query1);
$row2 = mysql_fetch_array(mysql_query($query2);
// Do comparison here
if ($row1 == $row2){
// etc
}
$x = $x++;
$y = $y++;
}

MySQL query returns null results when used in php

I am getting null results when I use the following Like query in PHP Script
$MasjidName = $_GET['MasjidName'];
$Percent = "%";
$search = $Percent.$MasjidName.$Percent;
echo $search;
$sql = "SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '".$search."'";
// get a product from products table
$result = mysql_query($sql) or die(mysql_error());
I have tried the following too
$result = mysql_query("SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%moh%'") or die(mysql_error());
The following is the null result I have been getting
{"masjids":[{"MasjidName":null,"Address":null,"Latitude":null,"Longitude":null}],"success":1,"masjid":[]}
whole code added below the following is the script i have been trying to get work
<?php
$response = array();
require_once dirname(__FILE__ ). '/db_connect.php';;
$db = new DB_CONNECT();
if (isset($_GET["MasjidName"]))
{
$MasjidName = $_GET['MasjidName'];
$MasjidName = mysql_real_escape_string($MasjidName); // you have to escape your variable here.
$sql = "SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%$MasjidName%'";
$result = mysql_query($sql) or die(mysql_error());
$response["masjids"] = array();
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$row = mysql_fetch_array($result);
$masjid = array();
$masjid["MasjidName"] = $row["MasjidName"];
$masjid["Address"] = $row["Address"];
$masjid["Latitude"] = $row["Latitude"];
$masjid["Longitude"] = $row["Longitude"];
// success
$response["success"] = 1;
// user node
$response["masjid"] = array();
array_push($response["masjids"], $masjid);
}
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
Try this:
$MasjidName = $_GET['MasjidName'];
$MasjidName = mysql_real_escape_string($MasjidName); // you have to escape your variable here.
$sql = "SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%$MasjidName%'";
$result = mysql_query($sql) or die(mysql_error());
Try using
$sql = "SELECT * FROM MasjidMaster WHERE MasjidName LIKE '".$search."'";
I tried SELECT * FROM Customers WHERE City LIKE 's%'; and it gave me perfectly fine result. But when i tried SELECT * FROM 'Customers' WHERE 'City' LIKE 's%'; it gave me a null result.
Just remove '' and give it a try. Hope it helps.

MySQL like Query fails in PHP

$result = mysql_query("SELECT * FROM MasjidMaster WHERE MasjidName LIKE ('%moh%')") or die mysql_error();
The error i get is
Parse error: syntax error, unexpected T_STRING in /home/maximtec/public_html/masjid_folder/MasjidFinderScripts/find_by_name.php on line 24
This query does work when i use it in MySQL but it doesn't when I place it in a PHP Script
Please suggest a solution
------------EDIT :After changing query from the received answers-------------------------------------
Well I updated my query but now I am getting null results
{"masjids":[{"MasjidName":null,"Address":null,"Latitude":null,"Longitude":null}],"success":1,"masjid":[]}
Following is my full script :
<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// include db connect class
//require_once __DIR__ . '/db_connect.php';
require_once dirname(__FILE__ ). '/db_connect.php';;
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["MasjidName"])) {
$MasjidName = $_GET['MasjidName'];
// get a product from products table
$result = mysql_query("SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%moh%'") or die(mysql_error());
$response["masjids"] = array();
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$row = mysql_fetch_array($result);
$masjid = array();
$masjid["MasjidName"] = $row["MasjidName"];
$masjid["Address"] = $row["Address"];
$masjid["Latitude"] = $row["Latitude"];
$masjid["Longitude"] = $row["Longitude"];
// success
$response["success"] = 1;
// user node
$response["masjid"] = array();
array_push($response["masjids"], $masjid);
// array_push($response["masjid"], $masjid);
}
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
$result = mysql_query("SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%moh%'") or die(mysql_error());
A little more tweaking, for good practice wrap table names and table columns in ``.
You also shouldn't need () around ('%moh%')
Try this-
$result = mysql_query("SELECT * FROM MasjidMaster WHERE MasjidName LIKE ('%moh%')") or die(mysql_error());
You forget parentheses with die(mysql_error())
try this:
$result = mysql_query("SELECT * FROM MasjidMaster WHERE MasjidName LIKE '%moh%' ") or die(mysql_error());
use die() like this die(mysql_error()) .

select statement always return the last inserted row in php mysql

When I wrote the select statement it always return the last inserted row in the database. What is the problem, and how can I fix it?
Important NOTE: A friend of mine took the same code and it worked for her properly!
if (isset($_GET["name"])) {
$pid = $_GET['name'];
// get a product from products table
//)or die(mysql_error()
$result = mysql_query("SELECT * FROM food WHERE name = $pid");
//mysql_query($result,$con);
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["name"] = $result["name"];
$product["unit"] = $result["unit"];
$product["calory"] = $result["calory"];
$product["carbohydrate"] = $result["carbohydrate"];
$product["category"] = $result["category"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No item found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
} */
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["name"] = $result["name"];
$product["unit"] = $result["unit"];
$product["calory"] = $result["calory"];
$product["carbohydrate"] = $result["carbohydrate"];
$product["category"] = $result["category"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
}
replace this with
while(mysql_num_rows($result) > 0 && ($result = mysql_fetch_array($result))) {
$product = array();
$product["name"] = $result["name"];
$product["unit"] = $result["unit"];
$product["calory"] = $result["calory"];
$product["carbohydrate"] = $result["carbohydrate"];
$product["category"] = $result["category"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
}
the result is array and you are not looping through it
so it givesonly one element in the array
This is because mysql_fetch_array is not in a loop, place it into the while loop and check.
Simply putting everything in a loop will not fix this. The code your gave will give the same result.. the last one.
$product needs to be declared BEFORE the loop otherwise, it will always be reset. Also, in order to populate the $product array without overwriting you will need to make it multidimensional
$product[]['name'] = $result["name"];
The ideal way of storing the products would be like this.. in my opinion.
$product = array();
while($result = mysql_fetch_array($result)) {
$product[$result['id']] = $result;

Categories