MySQLi和PHP:将SELECT查询与WHERE子句组合在一起

时间:2015-04-05 11:27:46

标签: php android mysqli

这是从代码表中选择所有数据的PHP代码。

<?php

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

/* CONNECTION SETTINGS */
$DB_HOST = '192.168.0.200';
$DB_UNAME = 'college';
$DB_PWD = 'TEST1234';
$DB_DATABASE = 'college_mgmt';

/* Connecting to mysql database */
$mysqli = new mysqli($DB_HOST, $DB_UNAME, $DB_PWD, $DB_DATABASE);

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* CONSTRUCT THE QUERY */
$query = "SELECT * FROM book_mst";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

if ($result === false)  {
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else  {

    $response["book_mst"] = array();

    while($row = $result->fetch_assoc())    {

        $book = array();

        /* ADD THE TABLE COLUMNS TO THE JSON OBJECT CONTENTS */
        $book["BookID"] = $row['BookId'];


        $book["BookName"] = $row['BookName'];

    $book["Author"] = $row['Author']; 
    $book["Publication"] = $row['Publication']; 
    $book["BookBarCode"] = $row['BookBarCode']; 



        array_push($response["book_mst"], $book);

        // $response[] = $row;
    }
    // success
    $response["success"] = 1;

    echo(json_encode($response));
}

/* CLOSE THE CONNECTION */
mysqli_close($mysqli);
?>

这是在表中插入行的PHP代码。

<?php

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

/* CONNECTION SETTINGS */
$DB_HOST = '192.168.0.200';
$DB_UNAME = 'college';
$DB_PWD = 'TEST1234';
$DB_DATABASE = 'college_mgmt';

/* Connecting to mysql database */
$mysqli = new mysqli($DB_HOST, $DB_UNAME, $DB_PWD, $DB_DATABASE);

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

// check for required fields
if (isset($_POST['BookName'])&&isset($_POST['Author'])&&isset($_POST['Publication'])&&isset($_POST['BookBarCode'])) {

    $BookName = $_POST['BookName'];
    $Author = $_POST['Author'];
    $Publication = $_POST['Publication'];
    $BookBarCode = $_POST['BookBarCode'];



    // mysql inserting a new row
    $query = "INSERT INTO book_mst(BookName,Author,Publication,BookBarCode) VALUES(?,?,?,?)";
    $statement = $mysqli->prepare($query);

    //bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
    $statement->bind_param('ssss', $BookName, $Author,$Publication,$BookBarCode);

    if($statement->execute())   {
        print 'Success! ID of last inserted record is : ' .$statement->insert_id .'<br />'; 
    } else {
        die('Error : ('. $mysqli->errno .') '. $mysqli->error);
    }

    $statement->close();
} else {
    // required field is missing
    $response["missing"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}

/* CLOSE THE CONNECTION */
mysqli_close($mysqli);
?>

我坚持将两者结合起来将值传递给PHP并形成如下语句:

SELECT * FROM book_mst WHERE BookBarCode ='0123456789';

我很感激你的帮助。

1 个答案:

答案 0 :(得分:0)

您的查询无效,因为您没有使用正确的列名。在您的插入查询中,您使用的是BookBarCode,如果有效,那么您的select语句应为:

SELECT * FROM book_mst WHERE BookBarCode = '0123456789'