用php变量执行查询

时间:2014-02-16 16:52:57

标签: php mysql

我必须查询数据库,其中我存储了登录凭据(用于作业)。这是我的PHP代码,但它不起作用。

<?php
$user = "cardatabase";
$password = "";
$host = "";
$database = "my_cardatabase";
$connession = mysql_connect($host, $user, $password);
mysql_select_db($database, $connession);


$id = $_REQUEST['id'];
$password = $_REQUEST['password'];

$query = "select Password
          from Client
          where IDClient = '".$id."'";
$result = mysql_query($query, $connession);

echo $result;

            if ($result == $password) {
                echo ok;
            } else {
                echo error;}
mysql_close();
?>

我试图打印来自我的html页面的值,这是正确的,所以这个代码应该有问题,但我无法理解。

4 个答案:

答案 0 :(得分:1)

替换代码

mysql_select_db($database, $connessione);

mysql_select_db($database, $connession);

答案 1 :(得分:0)

在第二行变量名的末尾有一个额外的e:

mysql_select_db($database, $connessione);

答案 2 :(得分:0)

第一个问题:

$connession = mysql_connect($host, $user, $password);
mysql_select_db($database, $connessione);

你有一个额外的e,所以应该是:

mysql_select_db($database, $connession)

第二个问题: mysql_query返回资源 - 您需要执行以下操作:

$result = mysql_query($query, $connession);
$rows = mysql_fetch_assoc($result);
echo $rows[0]['password'];

答案 3 :(得分:0)

更改以下代码::

<?php     
  $connession = mysql_connect($host, $user, $password) or die( mysql_error() . " <br/> could not connect to server");
  mysql_select_db($database, $connessione) or die( mysql_error() . " <br/> could not connect datbase");


  $id = $_REQUEST['id'];
  $password = $_REQUEST['password'];

   $query = "select Password
      from Client
      where IDClient='{$id}'";

   $result = mysql_query($query, $connession) or die(mysql_error());

   if( mysql_num_rows($result) > 0 )  {
       $row = mysql_fetch_array($result) or die(mysql_error());

       $dbPassword = $row['Password'];
       if( strcmp($password, $dbPassword) == 0 )  {
           echo "ok";
       }
       else {
           echo "Error";
       }
   }
   else {
       echo "error";
   }

   mysql_close();
?>