Php
PHP 腳本顯示錯誤
我正在嘗試為 php-mysql 伺服器和 android 設備之間的數據交換編寫 Web 服務。但是服務顯示一些錯誤並且無法正常工作。
這是我的程式碼
<?php include 'config.inc.php'; // Check whether username or password is set from android if(isset($_POST['username']) && isset($_POST['password'])) { // Innitialize Variable $result=''; $username = $_POST['username']; $password = $_POST['password']; // Query database for row exist or not $sql = 'SELECT * FROM tbl_login WHERE email = :username AND password = :password'; $stmt = $conn->prepare($sql); $stmt->bindParam(':username', $username, PDO::PARAM_STR); $stmt->bindParam(':password', $password, PDO::PARAM_STR); $stmt->execute(); if($stmt->rowCount()) { $result="true"; } elseif(!$stmt->rowCount()) { $result="false"; } // send result back to android echo $result; } ?>
現在我的同事是一名 android 開發人員,正在從 android 端製作登錄會話模組,他需要 Web 服務進行註冊和登錄。
你能解決錯誤嗎?或者
您能否為這些服務提供一些程式碼或連結到合適的資源?
您是否正確包含配置文件?是否獲得正確的數據庫連接?仔細檢查這些東西一次。
您的來源 coed 看起來不錯,但如果您在瀏覽器中得到任何錯誤,請提供您的錯誤。
你也可以試試下面的程式碼
<?php if($_SERVER['REQUEST_METHOD']=='POST'){ // echo $_SERVER["DOCUMENT_ROOT"]; // /home1/demonuts/public_html //including the database connection file include_once("config.php"); $username = $_POST['username']; $password = $_POST['password']; if( $username == '' || $password == '' ){ echo json_encode(array( "status" => "false","message" => "Parameter missing!") ); }else{ $query= "SELECT * FROM registerDemo WHERE username='$username' AND password='$password'"; $result= mysqli_query($con, $query); if(mysqli_num_rows($result) > 0){ $query= "SELECT * FROM registerDemo WHERE username='$username' AND password='$password'"; $result= mysqli_query($con, $query); $emparray = array(); if(mysqli_num_rows($result) > 0){ while ($row = mysqli_fetch_assoc($result)) { $emparray[] = $row; } } echo json_encode(array( "status" => "true","message" => "Login successfully!", "data" => $emparray) ); }else{ echo json_encode(array( "status" => "false","message" => "Invalid username or password!") ); } mysqli_close($con); } } else{ echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") ); } ?>
如果您想獲得工作服務,請訪問php login 並在 android教程中註冊以獲取更多詳細資訊。
登記服務
<?php if($_SERVER['REQUEST_METHOD']=='POST'){ // echo $_SERVER["DOCUMENT_ROOT"]; // /home1/demonuts/public_html //including the database connection file include_once("config.php"); $name = $_POST['name']; $username = $_POST['username']; $password = $_POST['password']; $hobby= $_POST['hobby']; if($name == '' || $username == '' || $password == '' || $hobby == ''){ echo json_encode(array( "status" => "false","message" => "Parameter missing!") ); }else{ $query= "SELECT * FROM registerDemo WHERE username='$username'"; $result= mysqli_query($con, $query); if(mysqli_num_rows($result) > 0){ echo json_encode(array( "status" => "false","message" => "Username already exist!") ); }else{ $query = "INSERT INTO registerDemo (name,hobby,username,password) VALUES ('$name','$hobby','$username','$password')"; if(mysqli_query($con,$query)){ $query= "SELECT * FROM registerDemo WHERE username='$username'"; $result= mysqli_query($con, $query); $emparray = array(); if(mysqli_num_rows($result) > 0){ while ($row = mysqli_fetch_assoc($result)) { $emparray[] = $row; } } echo json_encode(array( "status" => "true","message" => "Successfully registered!" , "data" => $emparray) ); }else{ echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") ); } } mysqli_close($con); } } else{ echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") ); } ?>
配置如下
<?php $host="localhost"; $user="your username"; $password="your password"; $db = "your db name"; $con = mysqli_connect($host,$user,$password,$db); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }else{ //echo "Connect"; } ?>