从MySQL字段中获取单个值并将其存储在变量中

时间:2014-05-31 10:26:18

标签: php mysql

我是PHP和MySql的初学者。我想要做的是从MySql表中获取单个字段值并将其存储到php变量中。我尝试了这段代码,但它似乎不起作用:

//Get Role ID

$con=     mysqli_connect("localhost","root","","darrenvellaedp2");
$result = mysqli_query($con,"SELECT userRoleID FROM tbl_users");

while($row = mysqli_fetch_array($result)) {
    echo $row['userRoleID'];
    echo "<br>";
}                   

1 个答案:

答案 0 :(得分:2)

//make the connection
$con = mysqli_connect("localhost","root","","darrenvellaedp2") or die("Error: " . mysqli_error($con));
//create the query
$result = "SELECT userRoleID FROM tbl_users" or die("Error: " . mysqli_error($con));
//execute the query
$res = $con->query($result);

while($row = mysqli_fetch_array($res)) {
    //this will print the userroleid out to the screen
    echo $row['userRoleID'];
    //this will store it in a variable
    $UserRoleID = $row['userRoleID'];
}

您可以更轻松地进行谷歌搜索,因为PHP.NET

上有关于此内容的整个部分