2015-10-20 55 views
1

我正在使用下面的代碼來檢查密碼。它的工作正常,而我進入表單的用戶名是在數據庫中。但如果用戶名不可用,則foreach拋出錯誤。當空結果返回時,php mysql錯誤

<?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 Warning: Invalid argument supplied for foreach() in E:\noname\name\logged.php on line 19 PHP Notice: Undefined variable: value in E:\noname\name\logged.php on line 22.

再加上我想知道是否有任何的方式來定義數據庫憑據ONY一次compelete網站。在範圍變量

回答

1

我同意Luthando戰利品。另外,如果您想爲競技網站定義一次數據庫憑證,您可以使用憑據創建一個常量文件,並將其包含到您連接數據庫的函數文件中。之後,在腳本中包含連接功能。這樣你就不必每次編寫腳本來連接你需要的數據庫。只是包括它和它的好去。

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的。這只是一個如何處理常量的例子,因此可以用於整個網站。

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

儲值,foreach循環中的$值範圍

+0

此錯誤仍然存​​在::: PHP注意:未定義的變量:第23行E:\ noname \ name \ logged.php中的值 –

+0

檢查此更新的代碼 –

+0

我這樣做了,它解決了我的問題。其他方式來做同樣的事情。 ($ empty($ row)){if(!empty($ row)){foreach($ row as $ value){ $ value;}} else {echo「enter valid id」;} if if($ value == $ pas){ echo「password matched」; \t } else {echo「not matched」;} } –

1

的問題是你的$value屬性只是躺在那兒,回聲或貯存的地方:

打印

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

或存儲它

foreach($row as $value){ 
    $valueStore = $value; 
} 
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"; 
    } 
?> 
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";  
}  
?>