2016-04-29 68 views
-1

我想在一個字段中回顯一次數據。然而,它正在循環,我不知道要改變什麼來使它回聲一次。有誰知道如何?這裏是代碼:我怎樣才能阻止這從循環?

<?php 

$email = $_POST['email']; 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbName = "users"; 

//Make Connection 
$conn = new mysqli($servername, $username, $password, $dbName); 
//Check Connection 
if(!$conn){ 
    die("Connection Failed. ". mysqli_connect_error()); 
} 

$sql = "SELECT `profilepicurl` FROM users WHERE `email`='".$email."'"; 
$result = mysqli_query($conn ,$sql); 


if(mysqli_num_rows($result) > 0){ 
    //show data for each row 
    while($row = mysqli_fetch_assoc($result)){ 
     echo $row['profilepicurl']; 
    } 
} 

?> 

謝謝!

+1

在'$ row ['profilepicurl'];' – Keeleon

+5

之後添加'break;'您正在使用** while循環**!爲什麼使用它,如果你不想循環? – AbraCadaver

+2

[Little Bobby](http://bobby-tables.com/)說[你的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can-i-prevent -sql -injection-in-php)瞭解[MySQLi]的[prepared](http://en.wikipedia.org/wiki/Prepared_statement)語句(http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –

回答

1

這很簡單!

變化while($row = mysqli_fetch_assoc($result)){$row = mysqli_fetch_assoc($result){

0

取出while()環和括號:

if (mysqli_num_rows($result) > 0) { 

    //show data for each row 
    $row = mysqli_fetch_assoc($result); 

    echo $row['profilepicurl']; 
} 
0

正確的方法停止循環使用break

if(mysqli_num_rows($result) > 0){ 
    //show data for each row 
    while($row = mysqli_fetch_assoc($result)){ 
     echo $row['profilepicurl']; 
     break; 
    } 
} 

儘管,如果你只是需要價值,你可以刪除while循環,只能使用:

$row = mysqli_fetch_assoc($result); 
echo $row['profilepicurl']; 

注:

你的腳本是不安全由於SQL注入的可能性,請務必閱讀how can i prevent sql injection in php

相關問題