为什么不查询数据库

时间:2016-02-24 09:39:35

标签: php mysql json

我编写了以下代码来从数据库中检索数据

<?php
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'users';

$con = mysqli_connect($host, $username, $password, $database);

// catch the values that are passed by the POST method
$courseId=$_POST["courseId"];
$hall=$_POST["hall"];
$day=$_POST["day"];
$year=$_POST["year"];
$time=$_POST["time"];

$statement=mysqli_prepare($con,"SELECT * FROM Lectures WHERE day=? AND timeInterval=? AND courseId=? AND hall=? AND year=? ");

mysqli_stmt_bind_param($statement,"sssss",$courseId,$hall,$day,$year,$time);
mysqli_stmt_execute($statement);
// have to collect the results that are coming after the query is being executed
mysqli_stmt_store_result($statement);   //storing the results in a buffer for temporary
// now we need to bind the results
mysqli_stmt_bind_result($statement,$day,$time,$courseId,$hall,$year,$noStudents,$courseRefName,$courseRefTelNo,$lecturer);

// to send the data via JSON string
$lectureDetails=array();
mysqli_stmt_fetch($statement);

$lectureDetails["day"]=$day;
$lectureDetails["time"]=$time;
$lectureDetails["courseId"]=$courseId;
$lectureDetails["hall"]=$hall;
$lectureDetails["year"]=$year;
$lectureDetails["noStudents"]=$noStudents;
$lectureDetails["courseRefName"]=$courseRefName;
$lectureDetails["courseRefTelNo"]=$courseRefTelNo;
$lectureDetails["lecturer"]=$lecturer;

// data are stored in the array. now we need to send them via a JSON string
echo json_encode($lectureDetails);
// the java file that calls this method will receive echo
//The PHP json_encode function returns a string, containing the JSON equivalent of the values passed to it
//so in here $lectureDetails array is passed throught throughe JSON String.
mysqli_stmt_close($statement);  //closing the connection
mysqli_close($con);     //closing the sql connection

?>

根据下面的工作提取文件(这会返回预期的值)

<?php
//database connection
$host = 'localhost';
$user1 = 'root';
$password1 = '';
$database = 'users';

$con = mysqli_connect($host, $user1, $password1, $database);


//checking the validity of the database
   // if(!$con){
    //die("connection Failed" . mysqli_connect_error());}
    //echo "connected Successfully";

$userName=$_POST["userName"];
$password=$_POST["password"];


$statement=mysqli_prepare($con,"SELECT * FROM usersLogged WHERE userName=? AND password=?");
//to prevent from sql injection
mysqli_stmt_bind_param($statement,"ss",$userName,$password);
mysqli_stmt_execute($statement);

//after executing the command we will get all the results that were selected
mysqli_stmt_store_result($statement);   //storing the results in a buffer for temporary
//we need to bind the results
mysqli_stmt_bind_result($statement, $userId, $userName, $firstName, $lastName, $password, $position,$birthDate,$qualification,$email);


//now we need to store them into an array inoder to send them via a JSON

$user=array();


mysqli_stmt_fetch($statement);
//fetch the result from a prepared statement into the variables bound by mysqli_stmt_bind_result. 
//Data are transferred unbuffered without calling mysqli_stmt_store_result() which can decrease performance (but reduces memory cost). 

//storing the values which are fetched from the database are kept in the array(#user)
$user["userName"]=$userName;
$user["firstName"]=$firstName; 
$user["lastName"]=$lastName;
$user["password"]=$password;
$user["position"]=$position;
$user["birthDate"]=$birthDate;
$user["qualification"]=$qualification;
$user["email"]=$email;

//now we need to pass the content to the phone,we send the array in a json

echo json_encode($user); // the java file that calls this method will receive echo
//The PHP json_encode function returns a string, containing the JSON equivalent of the values passed to it
//so in here $user array is passed throught the JSON String.
mysqli_stmt_close($statement);
mysqli_close($con); 







?>

但上述php文件未从mysql数据库中获取数据。 我在当前Lectures数据库和users day time courseId hall year创建了courseRefName表格lecuturer是字符串数据类型,noStudentscourseRefTelNo是整数数据类型。 mysqli_prepare或者编码为JSON的方式是否有任何错误,因为我没有从{(1}}文件到php java的任何回复应用程序

1 个答案:

答案 0 :(得分:-1)

我发现了问题,通过POST和数据库发送的数据之间的不匹配导致了问题。我已经以08.00-10.00的形式发送了时间,在数据库中,它们以5位数的形式显示为08-10。因此,如果有人遇到这类问题,请首先检查database中的数据以及通过POST方法传递的值