2017-02-21 54 views
-1

捕获的致命错误:转换成字符串错误

Object of class mysqli_result could not be converted to string in line:99

如何解决这个问题?

<?php 

class DB_con 
{ 

    private $servername; 
    private $user; 
    private $password; 
    private $dbname; 
    public $connection; 

    public function __construct() 
    { 
     $this->db_connect();  
    } 

    public function db_connect() 
    { 
     $this->servername = "localhost:3307"; 
     $this->user = "root"; 
     $this->password = "1234"; 
     $this->dbname = "library"; 

     $this->connection = new mysqli($this->servername,$this->user,$this->password,$this->dbname); 

     if($this->connection->connect_error) 
     { 
      die("connection failed :".$this->connection->connect_error); 
     } 
     else 
     { 
      echo "Success"; 
     } 
     return $this->connection;  
    } 

    public function insert($user,$hide,$phone,$address,$posting,$date,$secu_ques,$secu_ans) 
    { 
       $this->db_connect(); 

       /*if($pass != $rpass) 
       { 
        echo "<script> 
        alert('The Passwords do not match'); 
        </script>"; 
       }*/ 


       $phone = mysqli_query($this->connection,"SELECT Phone from Users WHERE Phone = '$phone'"); 

       $get_phone = mysqli_affected_rows($this->connection); 

       if($get_phone >1) 
       { 
        echo "<script> 
        alert('Number already Exists!!!'); 
        </script>"; 
       } 

       else if($address == null) 
       { 
        echo "<script> 
        alert('Fill out all the Fields to Continue'); 
        </script>"; 
       } 

       else if($posting == 'Designation') 
       { 
        echo "<script> 
        alert('Choose a Designation!!!'); 
        </script>"; 
       } 

       else if ($secu_ques == null || $secu_ans == null) 
       { 
        echo "<script> 
        alert('Enter A Security Question and Answer to Continue'); 
        </script>"; 
       } 

       else 
       { 

       $select_user = "SELECT Username from Users WHERE Username = '$user'"; 
       mysqli_query($this->connection,$select_user); 

       $get_user = mysqli_affected_rows($this->connection); 

       if($get_user >=1) 
       { 
        echo "<script> 
        alert('This Username Already Exists!!!'); 
        </script>"; 
       } 

       else 
       {    

    $record_add = "INSERT INTO Users(Username,Password,Phone,Address,Designation,DOJ,Question,Answer)VALUES('$user','$hide','$phone','$address','$posting','$date','$secu_ques','$secu_ans')"; 
       $res = mysqli_query($this->connection,$record_add); 

       if($res == TRUE) 
       { 
        echo "<script type = 'text/javascript'> 
        alert('Congrats you have Successfully Registered!!!'); 
        </script>"; 
       } 

       else 
       { 
        echo "<script type = 'text/javascript'> 
        alert('Error in Registering!!!'); 

        </script>"; 
       } 

       } 

      }   
    } 


} 

?> 

回答

0

您的$ phone变量覆盖原始变量。试试这个:

变化

$phone = mysqli_query($this->connection,"SELECT Phone from Users WHERE Phone = '$phone'"); 

$phone_exist = mysqli_query($this->connection,"SELECT Phone from Users WHERE Phone = '$phone'"); 
+0

谢谢老兄......这对我帮助很大。我已经找到了解决的办法浪费了一天.. – Shyam