从mysql_num_rows到mysql_fetch_assoc

时间:2015-10-08 09:36:35

标签: php database

如果我使用mysql_num_rows,那么我只能使用一个数字来检索数据(例如,ID为1将从ID为1的行中检索数据)?

是否可以使用字符/单词检索数据(例如,使用电子邮件从具有相同电子邮件的行检索数据)?     

// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["email"])) {
$email = $_GET['email'];

// get a product from products table
$result = mysql_query("SELECT * FROM products WHERE email = $email");

if (!empty($result)) {
    // check for empty result
    if (mysql_num_rows($result) > 0) {

        $result = mysql_fetch_array($result);

        $product = array();

        $product["pid"] = $result["pid"];
        $product["name"] = $result["name"];
        $product["email"] = $result["email"];

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

0 个答案:

没有答案