2013-02-18 84 views
0

我對MYSQL和PHP是全新的,所以我只需要做一些非常基本的事情。 我需要從帳戶中選擇一個密碼username = $_POST['username'] ...我無法弄清楚這一個,我不斷得到resource id(2)而不是輸入的帳戶所需的密碼。我需要通過mysql查詢函數傳遞該mysql,並將返回值保存在變量$ realpassword中。謝謝!在mysql中選擇某一行

編輯: 這段代碼返回的資源ID(2),而不是真正的密碼 CODE:

<?php 
$con = mysql_connect('server', 'user', 'pass'); 
if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 
echo '<br/> '; 

// Create table 
mysql_select_db("dbname", $con); 

//Variables 

//save the entered values 

$enteredusername = $_POST['username']; 
$hashedpassword = sha1($_POST['password']); 

$sql = "SELECT password from accounts where username = '$enteredusername'"; 

$new = mysql_query($sql,$con); 

echo "$new"; 


if (!mysql_query($sql,$con)) 
{ 
    die('Error: ' . mysql_error()); 
} 



mysql_close($con); 

?> 
+4

對於初學者來說可能有點多,但你應該考慮SQL注入和密碼哈希 – 2013-02-18 18:35:08

+0

任何代碼?任何示例? – vikingmaster 2013-02-18 18:35:12

+1

你是什麼意思資源ID(2)? – mariosk89 2013-02-18 18:35:25

回答

0

我覺得你的代碼看起來是這樣的

$realpassword = mysql_query("SELECT password 
    from accounts where username = '$_POST[username]'"); 
echo $realpassword; 

這將返回它是用來指向在數據庫中記錄的Resource。然後你需要做的是獲取資源指向的行。所以,你這樣做(請注意,我將使用結構的MySQLi而不是MySQL的,因爲MySQL現在已經過時了。)

$connection = mysqli_connect("localhost", "your_mysql_username", 
    "your_mysql_password", "your_mysql_database") 
    or die("There was an error"); 
foreach($_POST as $key=>$val) //this code will sanitize your inputs. 
    $_POST[$key] = mysqli_real_escape_string($connection, $val); 
$result = mysqli_query($connection, "what_ever_my_query_is") 
    or die("There was an error"); 
//since you should only get one row here, I'm not going to loop over the result. 
//However, if you are getting more than one rows, you might have to loop. 
$dataRow = mysqli_fetch_array($result); 
$realpassword = $dataRow['password']; 
echo $realpassword; 

所以,這需要獲取密碼的照顧。但是,你有更多的固有問題。您不會對輸入進行清理,甚至可能不會將散列的密碼存儲在數據庫中。如果你開始使用PHP和MySQL,你應該仔細研究這些東西。

編輯:如果你只是想創建一個登錄系統,那麼你不需要從數據庫中檢索密碼。在這種情況下,查詢非常簡單。

$pass = sha1($_POST['Password']); 
$selQ = "select * from accounts 
    where username = '$_POST[Username]' 
    and password = '$pass'"; 
$result = mysqli_query($connection, $selQ); 
if(mysqli_num_rows($result) == 1) { 
    //log the user in 
} 
else { 
    //authentication failed 
} 

從邏輯上講,用戶可以登錄的唯一方式是如果用戶名和密碼都匹配。所以,用戶名和密碼只有一行。這正是我們在這裏查看的內容。

+0

好的好消息,我得到所需的密碼打印到屏幕上,但立即收到密碼後錯誤:查詢是空的使得結果correctpassError:查詢是空的。您發佈的這段代碼正是我所需要的,現在我該如何解決它以擺脫錯誤。這是Mysql中變量$ realpassword的錯誤部分還是回顯到屏幕的內容。謝謝! – Shadowpat 2013-02-18 19:05:22

+0

我會如何讓這個更安全的程序?謝謝! – Shadowpat 2013-02-18 19:06:36

+0

@Shadowpat像上面那樣使用mysqli或PDO,使用像[password_hash()](http://php.net/password_hash)這樣的安全工具來散列密碼(如果PHP <5.5,請參閱註釋)。 – Mike 2013-02-18 19:08:49

-1
<?php 
$query = "SELECT password_field_name FROM UsersTableName WHERE username_field_name =".$_POST['username']; 
$result = mysql_query($query); 
$row = mysql_fetch_array($result); 
echo $row['password_field_name']; 
?> 
+1

Downvoted for SQL注入漏洞。 – Mike 2013-02-18 19:10:46

-1
$username = $_POST['username'];          
$login_query = "SELECT password FROM users_info WHERE users_info.username ='$username'";   
$password = mysql_result($result,0,'password');  
+1

SQL注入任何人? – Mike 2013-02-18 19:01:07

+0

pffff,是的,你說得對。我的錯。但無論如何,這是主要的(也是最簡單的)想法! – mariosk89 2013-03-01 07:44:22

0

通過觀察這個問題,我們能理解你是非常非常新的編程。所以我請你通過這個鏈接http://php.net/manual/en/function.mysql-fetch-assoc.php

我在下面的每一行添加註釋

$sql = "SELECT id as userid, fullname, userstatus 
     FROM sometable 
     WHERE userstatus = 1"; // This is query 

$result = mysql_query($sql); // This is how to execute query 

if (!$result) { //if the query is not successfully executed 
    echo "Could not successfully run query ($sql) from DB: " . mysql_error(); 
    exit; 
} 

if (mysql_num_rows($result) == 0) { // if the query is successfully executed, check how many rows it returned 
    echo "No rows found, nothing to print so am exiting"; 
    exit; 
} 

while ($row = mysql_fetch_assoc($result)) { //fetch the data from table as rows 
    echo $row["userid"]; //echoing each column 
    echo $row["fullname"]; 
    echo $row["userstatus"]; 
} 

希望它有助於

0

試試這個

<?php 
    $con = mysql_connect('server', 'user', 'pass'); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 
echo '<br/> '; 

// Create table 
mysql_select_db("dbname", $con); 

//Variables 

//save the entered values 

$enteredusername = mysql_real_escape_string($_POST['username']); 
$hashedpassword = sha1($_POST['password']); 

$sql = "SELECT password from accounts where username = '$enteredusername'"; 

$new = mysql_query($sql,$con); 

$row = mysql_fetch_array($new) ; 
echo $row['password']; 

if (!$new) 
{ 
die('Error: ' . mysql_error()); 
} 



mysql_close($con); 

?> 
1

這將是一個好很多,如果你有prepared statements一起使用PDO

這是你如何連接到MySQL服務器:

$db = new PDO('mysql:host=example.com;port=3306;dbname=your_database', $mysql_user, $mysql_pass); 

這就是你如何選擇正確的行(使用bindParam):

$stmt = $db->prepare('SELECT password FROM accounts WHERE username = ?;'); 
$stmt->bindParam(1, $enteredusername); 
$stmt->execute(); 
$result = $stmt->fetch(PDO::FETCH_ASSOC); 
$password = $result['password']; 

此外,綁定參數,而不是把他們立即進入查詢字符串,保護您免受SQL注入(在您的情況下,很可能因爲您不以任何方式過濾輸入)。