dtewnsdf47253
2015-05-18 13:39
浏览 84
已采纳

在数据库中使用PDO预处理语句检查是否有任何结果

<?php

//I wanna make a login page

//This my logon.php which it gets data from my login.php and checks if (username,password)given exists in my Database, and if there are, it allows you to continue to view my index.php.

//I wanna do it with PDO prepared statements.

//Here is my connection to Database

$db_host="localhost";
$db_username="root";
$db_pass="";
$db_name="admin";

$db=new mysqli("$db_host","$db_username","$db_pass","$db_name");

echo $db->connect_errno;

if($db->connect_errno){
    die("sorry we have some  problems");

}

if($_SERVER ['REQUEST_METHOD']=='POST'){

        $username=$_POST['username'];
        $password=$_POST['password'];

        $username=htmlspecialchars($username);
        $password=htmlspecialchars($password);

//Here i try to check in my Database if the given username AND password exists and somehow check if i got any results.if i got 1,2,3...rows that matches the username and password

//this what i wanna do with PDO...please Help!

        $pdo->prepare$sql=('SELECT * FROM members WHERE username = :username and password = :password' );
        $pdo->execute(array(':username' => $username, ':password'=>$password));

//Here is what i wanna do if i got any results from my database

        if($result){
            $num_rows=mysqli_num_rows($result);
            if($num_rows>0){
                session_start();
                $_SESSION['check']="1";
                header ("Location:index.php");
            }
            else{
                session_start();
                $_SESSION['check']=""; 
                header ("Location:index.php");

            }
        }

    }





    ?>

图片转代码服务由CSDN问答提供 功能建议

 &lt;?php 
 </ code> </ pre> 
 
 

//我想创建一个登录页面</ p>

//这是我的logon.php,它从我的login.php获取数据并检查我的数据库中是否存在(用户名,密码),以及 如果有,它允许你继续查看我的index.php。</ p>

//我想用PDO预处理语句来做。</ p>

/ /这是我与数据库的连接</ p>

  $ db_host =“localhost”; 
 $ db_username =“root”; 
 $ db_pass =“”; 
 $ db_name =  “admin”; 
 
 $ db = new mysqli(“$ db_host”,“$ db_username”,“$ db_pass”,“$ db_name”); 
 
echo $ db-&gt; connect_errno; 
 
if  ($ db-&gt; connect_errno){
 die(“抱歉我们遇到了一些问题”); 
 
} 
 
if($ _ SERVER ['REQUEST_METHOD'] =='POST'){
 
  $ username = $ _ POST ['username']; 
 $ password = $ _ POST ['password']; 
 
 $ username = htmlspecialchars($ username); 
 $ password = htmlspecialchars($ password); 
  </ code> </ pre> 
 
 

//这里我尝试检查我的数据库是否给定用户名和密码e xists 以某种方式检查我是否有任何结果。如果我得到了1,2,3 ...与用户名和密码匹配的行</ p>

//这就是我想用PDO做的事情。 ..请帮助!</ p>

  $ pdo-&gt; prepare $ sql =('SELECT * FROM members WHERE username =:username and password =:password'); 
 $  pdo-&gt; execute(array(':username'=&gt;  $ username,':password'=&gt; $ password)); 
 </ code> </ pre> 
 
 

//如果我从我的数据库中得到任何结果,这就是我想做的事情</ p>

  if($ result){
 $ num_rows = mysqli_num_rows($ result); 
 if($ num_rows&gt; 0){
 session_start(); 
 $ _SESSION [  'check'] =“1”; 
 header(“Location:index.php”); 
} 
 else {
 session_start(); 
 $ _SESSION ['check'] =“”;  
标题(“位置:index.php”); 
 
} 
} 
 
} 
 
 
 
 
 
?&gt; 
 </ code> </ pre  > 
 </ div>

1条回答 默认 最新

相关推荐 更多相似问题