从mysql中检索数据

时间:2018-02-20 07:22:35

标签: php mysql

我有一个带有播放器信息的mysql数据库。 enter image description here

在那里,有一种名为" rank"和等级5等于管理员。所以我可以检索排名信息,以便我可以使用它:

if(rank == 5) {
// do stuff
} else {
// error message
}

我试图搜索这个,但我似乎找不到任何东西。所以任何帮助都会很好。为我糟糕的英语而烦恼

5 个答案:

答案 0 :(得分:1)

<?php

$DB_Server = 'localhost';
$DB_User = 'root';
$DB_pass = '';
$database = 'SAMPLE';

$conn = mysqli_connect($DB_Server, $DB_User, $DB_pass, $database);


$rank = "";

$result = $conn->query("SELECT * from TableName where rank = '5'");

while($row = $result->fetch_assoc())
{
    $rank = $row['rank'];
}

if($rank == 5)
{
    #do something
}else{
    #do something
}
?>

答案 1 :(得分:0)

试试这个

  select * from table name  where rank =5

答案 2 :(得分:0)

将其用于实际的SQL查询:

SELECT * FROM TABLE_NAME WHERE rank = 5;

这将返回具有该排名的所有用户。如果您只想让一个用户为用户名或ID添加where语句,则必须使用任何唯一字段。

SELECT * FROM TABLE_NAME WHERE rank = 5 AND username = USER_NAME;

用于连接数据库(如果您需要知道如何操作)google谁可以选择PHP。

将结果作为数组后,您将访问返回行的正确字段:

if($row['rank'] == 5) {

关于PHP和Mysql的任何好的教程都会更深入地解释。

答案 3 :(得分:0)

语言新手?

try {
    // Try to connect to the database server
    $conn = new mysqli("localhost", "user", "password", "database");
} catch (Exception $e) {
    // If throws an error, show the error message
    echo $e->getMessage();
}

// Query the table players and select all keys where rank equals to 5
$result = $conn->query("SELECT * FROM players where `rank` = 5");
// For each result (row) in the query as $row (iterating over the multiple rows)
foreach ($result as $row) {
    if($row['rank'] == 5) {
       echo '<span>'.$row['username'].'<\/span>';
       echo '<span>'.$row['rank'].'<\/span>';
       echo '<span>'.$row['email'].'<\/span>';
   }
}

答案 4 :(得分:0)

使用更快速且受SQL注入保护的PHP PDO类 试试

    $db = new PDO('mysql:host=YourServerName;dbname=YourDatabaseName','YourUserName','YourDatabaseUser');   

$RankNum = 5;
$query = $db->prepare("SELECT * FROM YourTableName Where Rank=:RankNum");
$query->bindParam("RankNum", $RankNum);

$query->execute();

if($rows = $query->fetchAll(PDO::FETCH_ASSOC))
{
    //Write your logics here
    //right now just echoing all data returned by query
    echo json_encode($rows);
    $db = null;
}
else
{
    echo "No Record Found"; 
}

不要忘记更改占位符YourServerName等