如何阻止循环?

时间:2016-04-29 20:41:27

标签: php mysqli

我试图在一个字段中回显一次数据。然而,它是循环,我不知道要改变什么,使它回应一次。有谁知道怎么样?这是代码:

<?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'];
    }
}

?>

谢谢!

3 个答案:

答案 0 :(得分:1)

这很简单!

$row = mysqli_fetch_assoc($result){更改为<telerik:RadGrid ID="gvPIOList" runat="server" Class="display table table-bordered table-hover table-responsive text-center" CellPadding="2" HorizontalAlign="Center" AllowPaging="True" AllowSorting="True" GroupPanelPosition="Top" Skin="Outlook" ShowFooter="True" OnNeedDataSource="gvPIOList_NeedDataSource" MasterTableView-RowIndicatorColumn-Visible="false" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" AlternatingItemStyle-HorizontalAlign="Center" AllowFilteringByColumn="true" MasterTableView-EditFormSettings-EditColumn-FilterListOptions="VaryByDataTypeAllowCustom" MasterTableView-EditFormSettings-EditColumn-ShowFilterIcon="false" MasterTableView-EditFormSettings-EditColumn-AndCurrentFilterFunction="Contains" OnColumnCreated="gvPIOList_ColumnCreated" OnInit="gvPIOList_Init" OnItemCreated="gvPIOList_ItemCreated"> <MasterTableView GroupLoadMode="Server" EditFormSettings-EditColumn-AutoPostBackOnFilter="true" EditFormSettings-EditColumn-FilterDelay="1000" AllowFilteringByColumn="true" EditFormSettings-EditColumn-FilterListOptions="VaryByDataTypeAllowCustom" EditFormSettings-EditColumn-ShowFilterIcon="false"> </MasterTableView> </telerik:RadGrid>

答案 1 :(得分:0)

删除while()循环和大括号:

if ( mysqli_num_rows($result) > 0 ) {

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

    echo $row['profilepicurl'];
}

答案 2 :(得分:0)

停止循环的正确方法是使用break

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

但是,如果您只需要 1 值,则可以删除 while循环并仅使用:

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

注意:

由于 sql注入的可能性,您的脚本不安全,请务必阅读how can i prevent sql injection in php