致命错误:未捕获的异常'异常'堆栈跟踪:#0

时间:2017-03-26 17:01:37

标签: php mysqli server

我尝试在数据库和服务器之间建立简单的连接。 但我强迫这个错误请帮我解决。

  

连接!

     

致命错误:未捕获的异常'异常' in / Applications / XAMPP / xamppfiles / htdocs / social app / secure / access.php:45堆栈跟踪:#0 / Applications / XAMPP / xamppfiles / htdocs / social app / register.php(41):access-> registerUser( ' 1',' e05c96370d030ad ...',NULL,' \ x9F \ xFB \ x00 \ xF6p \ x84 \ xF9 \ x95 \ xB5 \ xBE \ x18Z \ xD2 \ xBDh ...')在第45行的/ Applications / XAMPP / xamppfiles / htdocs / social app / secure / access.php中抛出#1 {main}

这是我在access.php的代码

    <?php
class access{

var $host = null; 
var $user = null;
var $pass = null;
var $name = null;
var $conn = null;
var $result = null;

function __construct($dbhost,$dbuser,$dbname,$dbpass){
$this->host = $dbhost;
$this->user = $dbuser;
$this->pass = $dbpass;
$this->name = $dbname;

}

public function connect() {
$this->conn = new mysqli($this->host,  $this->user, $this->pass, $this->name); 

if (mysqli_connect_errno()){
echo " ";
}else{
echo "connected";
}//end if
$this->conn->set_charset("utf8");//for all languages
}//end func

public function disconnect() {
if ($this->conn != null){
    $this->conn->close();
    }//end if
}
// isert user details

public function registerUser($username, $password, $email, $salt){
//sql command
$sql = "INSERT INTO users SET username=?, password=?, email=?, salt=?";

$statment = $this->conn->prepare($sql);

//if error
if (!$statment){
 throw new Exception($statment->error);

 $statment->bind_param("ssss",$username, $password, $email, $salt);
 $returnValue = $statment->execute();

 return $returnValue;
}
}//end func

// select user
public function selectUser($username){
$sql = "SELECT * FROM users WHERE username = '".$username."'";

$result=$this->conn->query($sql);

if ($result !=null && (mysql_num_rows($result) >= 1)){
$row = $result->fetch_array(MYSQLI_ASSOC);

if(!empty($row)){
$returnArray = $row;
    }
    return $returnArray;

}

}

?>

这是register.php文件

<?php

// STEP 1: declare prametirs of user information

// Securing information and string variables
$username = htmlentities($_REQUEST["username"]);
 $password = htmlentities($_REQUEST["password"]);

// if get or post are empty
if (empty($username) || empty($password)){
$returnArray["status"] = "400";
$returnArray["message"] = "all fields are required";

echo json_encode($returnArray);
return;
}//endif

// secure password

$salt = openssl_random_pseudo_bytes(20);
//sha1 تستخدم لتشفير البيانات
$secured_password = sha1($password . $salt);

// STEP 2: build connection
// secure way to build conn

$file = parse_ini_file("../../../social app.ini");

// store php var information from ini var
$host = trim($file["dbhost"]);
$user = trim($file["dbuser"]);
$pass = trim($file["dbpass"]);
$name = trim($file["dbname"]);

// جملة استدعاء صفحة داخل صفحة
require("secure/access.php");
$access = new access($host, $user, $pass, $name);
 $access->connect();

//STEP 3. Insert user inforamtion
$result = $access->registerUser($username, $secured_password, $email, $salt);    
if ($result){
$user = $access->selectUser($username);

$returnArray["status"] = "200";
$returnArray["message"] = "seccessfully registerd.";

$returnArray["id"] = $user["id"];
$returnArray["username"] = $user["username"];
$returnArray["password"] = $user["password"];
$returnArray["email"] = $user["amail"];

}else {
$returnArray["status"] = "400";
$returnArray["message"] = "could not register with provided information";}

//STEP4. 
$access->disconnetct();

//STEP 5.
echo json_encode($returnArray);



  ?>

我该如何解决这个问题?!! ..

0 个答案:

没有答案