2

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);
    }
}
3
  • can you please elaborate on what is not working ie: what value are you getting - this way it helps which section we should be looking at. Commented Jul 16, 2013 at 22:10
  • Added more info above. Commented Jul 16, 2013 at 22:44
  • from_weight >= '$ship_weight' AND to_weight <= '$ship_weight' - which weight is larger and smaller? Commented Jul 17, 2013 at 0:32

3 Answers 3

2

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");
Sign up to request clarification or add additional context in comments.

Comments

0

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);
}

2 Comments

$a = false; echo empty($a); prints 1
The script I posted does run and the $result will be empty if there is nothing that meets the Query Request.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.