2016-04-29 71 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

相关问题