2013-02-11 35 views
-1

我11岁,我正在为我和我的朋友建立一个聊天网站。我使用MYSQLi来处理数据库的事情,而且我还挺新的。我一直使用普通的mysql。如何检查电子邮件是否存在MYSQLi

哦!如果你可以分享一个链接到一个mysqli的教程,这将是巨大的:)

那么这里是我的配置文件

<?PHP      


define("HOST", "localhost"); define("USER", "root"); define("PASSWORD", "****"); define("DATABASE", "secure_login"); 

$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE); 

?> 

的数据库secure_login,然后我有一个表称为成员,然后该表中名为电子邮件的列。

我把这个包含在一个文件(register.php)中,我必须检查邮件是否存在。如果存在,重定向到家。

如果你们可以帮助我,这将是很酷!我希望尽快完成:)

+0

不要使用mysqli的,使用PDO来代替 – 2013-02-12 10:47:37

回答

1

我会从MySQLi文档http://php.net/manual/en/book.mysqli.php开始阅读,具体涉及该类的方法。如果您需要教程,我建议您在Google上进行搜索,网上有大量教程。

至于你的问题,这样的事情应该工作:

$query = "SELECT email FROM members WHERE USER = ? AND PASS = ?"; 

if ($stmt = $mysqli->prepare($query)){ 
     // Bind the parameters, these are the ?'s in the query. 
     $stmt->bind_param("ss", $username, sha1($password)); 
     // Execute the statement 
     if($stmt->execute()){ 
      // get the results from the executed query 
      $stmt->store_result(); 

      $email_res= "";   
      // This stores the value of the emailaddres inside $email_res 
      $stmt->bind_result($email_res); 


      $stmt->fetch(); 

      // There is a result 
      if ($stmt->num_rows == 1){ 

          // Validate the emailadres here, check the PHP function filter_var() 

      } 
      else { 
        // Not a valid email 
      } 
     } 
     else { 
      printf("Execute error: %s", $stmt->error); 
     } 

    } 
    else { 
     printf("Prepared Statement Error: %s\n", $mysqli->error); 
    } 
} 

我希望这有助于