简体   繁体   English

如何从表 WHERE id=variable 中选择字段?

[英]How can I SELECT field FROM table WHERE id=variable?

I have a variable of the logged in user ( $steamid ) that I need to use to select and echo specific fields from the database.我有一个登录用户 ( $steamid ) 的变量,我需要用它来从数据库中选择和回显特定字段。 I am using the following code, but it is working incorrectly.我正在使用以下代码,但它工作不正常。 All database info is correct, the tables, columns, and variables are not misspelled.所有数据库信息都是正确的,表、列和变量没有拼写错误。 Not sure what I'm doing wrong.不知道我做错了什么。

<?php
$con=mysqli_connect("private","private","private","private");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql = "SELECT `bananas` FROM `es_player` WHERE `steamid` = '$steamID'";

if ($result=mysqli_query($con,$sql))
  {
  // Get field information for all fields
  while ($fieldinfo=mysqli_fetch_field($result))
    {
    printf("bananas: %n",$fieldinfo->bananas);
    }
  // Free result set
  mysqli_free_result($result);
}

mysqli_close($con);
?>

No errors are shown, it simply returns "bananas:" with nothing after it.没有显示错误,它只是返回“bananas:”,后面没有任何内容。 I feel like I didn't to it correctly, does anyone know what I might've done wrong?我觉得我没有正确对待它,有谁知道我可能做错了什么? Here is a screenshot of my database table so you know what it looks like http://puu.sh/gCY3d/983b738458.png .这是我的数据库表的屏幕截图,因此您知道它的样子http://puu.sh/gCY3d/983b738458.png

Try this:尝试这个:

$query = Mysqli_Query($con, "SELECT * FROM `es_player` WHERE `steamid`='$steamID'") or die(mysql_error());

  if( ! mysqli_num_rows($query) ) 
  {
  echo 'No results found.';
  }
  else
  {   
   $bananas_array = mysqli_fetch_assoc($query);
   $bananas = $bananas_array['bananas'];
   echo 'Number of bananas: '. $bananas;      
  }

If this doesn't work, there is a problem with STEAM_ID format.如果这不起作用,则 STEAM_ID 格式有问题。 You could try triming the IDs to be JUST a number, and add the STEAM_x:x: to it later.您可以尝试将 ID 修剪为一个数字,然后再添加 STEAM_x:x: 到它。

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

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