返回空结果时出现php mysql错误

时间:2015-10-20 05:41:07

标签: php mysql

我使用下面的代码检查密码。我输入表单的用户名在数据库中工作正常。但如果用户名不可用,那么foreach会抛出错误。

   <?php 
    $u=$_REQUEST['loginname'];
    $P=$_REQUEST['loginpass'];
    $pas=md5($P);
    $server="localhost:3306";
    $user="root";
    $pass="prabhs226";
    $database="sugar";
    $conn=mysqli_connect($server,$user,$pass);
    mysqli_select_db($conn,$database);
    $sql=("select b_pass from ub_per where b_id='$u' or b_email= '$u';");
    $res = mysqli_query($conn,$sql);

    $row = mysqli_fetch_array($res,MYSQLI_ASSOC);

    foreach($row as $value){
    $value;}

    if($value==$pas){
    echo "password matched";    
    }
    else{echo"not matched";}
    ?>

我收到此错误:

  

PHP警告:在第19行的E:\ noname \ name \ logged.php中为foreach()提供的参数无效       PHP注意:未定义的变量:第22行的E:\ noname \ name \ logged.php中的值。

加上我想知道是否有任何方法可以为complete网站定义一次DB凭据。

5 个答案:

答案 0 :(得分:1)

$storeVal = '';
    if(!empty($row))
       foreach($row as $value){
        $storeVal = $value;
}
if($storeVal==$pas){
    echo "password matched";    
    }
    else{echo"not matched";}

将值存储在范围变量,foreach循环中的$ value范围

答案 1 :(得分:1)

问题是你的$value属性只是放置在那里,回应或存储在某个地方:

打印

foreach($row as $value){
    echo $value;  
}  

或存储

foreach($row as $value){
    $valueStore = $value;  
} 

答案 2 :(得分:1)

我同意Luthando Loot。此外,如果您想为complete网站定义一次DB凭据。您可以使用凭据创建一个常量文件,并将其包含在与数据库建立连接的函数文件中。之后包括脚本中的连接功能。这样你就不必在每次需要数据库时编写脚本来连接。只需加入它就可以了。

实施例

constants.php

/**
 * Database Constants - these constants are required
 * in order for there to be a successful connection
 * to the MySQL database. Make sure the information is
 * correct.
 */
define("DB_SERVER", "localhost");
define("DB_USER", "User_db");//enter your database username
define("DB_PASS", 'Pass_db');//databse password
define("DB_NAME", "Name_db");//database name

database.php中

/**
 * Database.php
 *
 * The Database class is meant to simplify the task of accessing
 * information from the website's database.
 */
include("constants.php");

try {
    $db = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME, DB_USER, DB_PASS); //Initiates connection
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Sets error mode
}
catch (PDOException $e) {
    file_put_contents("log/dberror.log", "Date: " . date('M j Y - G:i:s') . " ---- Error: " . $e->getMessage() . PHP_EOL, FILE_APPEND);
    die($e->getMessage()); // Log and display error in the event that there is an issue connecting
}

请记住,这是一个PDO连接而不是mysqli。这只是如何使用常量的示例,因此它可以用于整个站点。

答案 3 :(得分:0)

试试这个。

<?php 
    $u=$_REQUEST['loginname'];
    $P=$_REQUEST['loginpass'];
    $pas=md5($P);
    $server="localhost:3306";
    $user="root";
    $pass="prabhs226";
    $database="sugar";
    $conn=mysqli_connect($server,$user,$pass);
    mysqli_select_db($conn,$database);
    $sql=("select b_pass from ub_per where b_id='$u' or b_email= '$u';");
    $res = mysqli_query($conn,$sql);

    if($row = mysqli_fetch_array($res,MYSQLI_ASSOC)) {
        if($row['b_pass'] == $pas){
            echo "password matched";    
        }
        else{
            echo"not matched";
        }
    }
    else{
        echo"not matched";
    }
?>

答案 4 :(得分:0)

我正在使用PDO

<?php    $u=$_REQUEST['loginname'];    
 $P=$_REQUEST['loginpass'];    
 $pas=md5($P);    
 $db = new PDO('mysql:host=localhost;dbname=sugar', 'root' , 'prabhs226');  
 $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);    
 $stmt=$db->prepare("select b_pass from ub_per where b_id= :id or b_email= :email;");    
 $stmt->bindParam(':id', $id, PDO::PARAM_INT);    
 $stmt->bindParam(':email', $email, PDO::PARAM_STR);    
 $stmt->execute();    
 $row = $stmt->fetch();    
 if($row){    
   $row['b_pass']=$value;    
 }    
 else{     
   echo "invalid user name ";    
 }    
 if($value==$pas){    
   echo "password matched";    
 }    
 else{    
   echo "not matched";    
 }    
?>