简体   繁体   English

如何通过php从mysql数据库中获取单个参数?

[英]how to get a individual single parameter from mysql database via php?

i'm not pro in php. 我不是专业的PHP。 i have a simple table like this: 我有一个简单的表,像这样:

game_id      name      score

and i used this query to get the rank of a player in mysql: 我使用此查询来获取mysql中播放器的等级:

SELECT FIND_IN_SET( score, ( SELECT GROUP_CONCAT( score ORDER BY score ASC ) FROM highscores ) ) AS 'rank' FROM highscores where game_id = $game_id

it works and shows the rank in mysql but i cant get the rank number as a parameter in my php code. 它的工作原理,并显示在mysql中的等级,但我无法在我的php代码中获得等级号作为参数。

I have tried with the following methods: 我尝试了以下方法:

mysql_fetch_assoc();
mysql_free_result();
mysql_fetch_row();

But I didn't succeed to display (get) the actual value. 但是我没有成功显示(获取)实际值。

As has been notes in the comments you shouldn't use the mysql_* functions. 如注释中所指出的,您不应使用mysql_ *函数。 I tend to use mysqli. 我倾向于使用mysqli。

You can create a connection 您可以建立连接

$con = mysqli_connect(serverip,"myname","mypassword","mydatabase");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

And execute a query 并执行查询

$result = $con->query($myquery) or die($con->error.__LINE__);

Once you have successfully executed the query 成功执行查询后

you can get a row with 你可以和

$row =  $result->fetch_assoc()

and get the individual fields with 并获得单个字段

$row['score']
include ('dbconnection.php');


$sql = "SELECT game_id, score, ... from YOURTABLE where game_id = '$game_id'";

now you have to query it like 现在您必须像查询

$query = mssql_query($sql, $connection);

and now u can fetch it with : 现在你可以用:

 while($row = mssql_fetch_assoc($query))
{
 echo $row["game_id"];
 echo $row["score"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM