Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么不查询数据库_Php_Mysql_Json - Fatal编程技术网

Php 为什么不查询数据库

Php 为什么不查询数据库,php,mysql,json,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["courseI

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

<?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
文件没有从
mysql
数据库中获取数据。 我已经在当前
用户
数据库和
日期
时间
课程ID
大厅
课程名称
勒库图勒
为字符串数据类型,
无学生
课程FTELNO
为整数数据类型。
mysqli_prepare
或编码为JSON的方式是否有任何错误,因为我没有从这个(上面的)
php
文件返回到我在
android
应用程序中的
java
,我发现了问题,是通过
POST
发送的数据与数据库中的数据不匹配造成了问题。我以
08.00-10.00
的形式发送了时间,在数据库中,它们以5位数字的形式显示为
08-10
。因此,如果有人遇到这样的问题,首先检查
数据库中的数据以及通过
POST
方法传递的值在执行查询后检查错误,查询您使用的是PHP、LAMP还是WAMPDo
var_dump(mysqli_stmt_execute($statement))并查看是否打印
bool(true)
如果未打印,则执行
var\u转储(mysqli\u error($con))并给出结果。@Sathish Kumar D我正在使用LAMP执行php文件并更新路径var/log/apache2/error.log中的错误报告,错误显示为当前时间和日期
<?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); 







?>