【问题标题】:Selecting a row based on multiple column values?根据多列值选择一行?
【发布时间】:2012-12-05 18:32:07
【问题描述】:

我正在为 SecondLife 开发一个小系统,其中涉及到大量使用数据库,我遇到了一个我一辈子都无法解决的问题。

我正在尝试运行一个查询,该查询根据两个不同的列值选择整行。我正在使用 $_GET[''] 调用来确定我需要哪些详细信息,然后我需要从整行中提取所有数据并将其回显到空白页上。

现在,我一开始可以使用它,但是当我只需要一个特定的时,它会拉取由 UUID 创建的所有行并回显它们。然后我试图让它与第二个标识符一起工作(这是一个称为“字段”的列,用于测试目的),而这几乎就是我丢失它的地方。

我只需要选择一个具有两个精确值的行,并且我需要它是严格的,所以它只选择一个具有两个而不是两个之一的行。

有什么想法吗?

附带的脚本(不要介意我保留供参考的混乱或注释掉的代码;一旦我开始工作,我将清理脚本!)

     <?php

// Get key from the parameter.
$avatar_key = $_GET['key'];

// Quest params.
$questname = $_GET['questname'];
$questid = $_GET['id'];


// Connect to the database. $con holds connection info.

$con = mysql_connect("localhost","user","pass");

// Error checking.
if(!$con)
{
    die('Could not connect: '.mysql_error());
}


// Select the DB we want.
mysql_select_db("yyyyy_slcom",$con);

// Build the query.
$query = "SELECT * FROM sldb_data WHERE sldb_data.uuid = '$avatar_key' AND sldb_data.field = '$questname'";
/*$query = "select * from (
                            select uuid, field, value, progress, ROW_NUMBER() OVER (PARTITION BY uuid ORDER BY progress DESC)
            ";*/

// Run the query, store the result in variable $result.
$result = mysql_query($query);

if(!result)
{
die('Error: ' . mysql_error());
}

// Check how many rows we got back with our query.
$rows_returned = mysql_num_rows($result);

echo $row['field'];

// If we get anything back,
if($rows_returned > 0)
{




    //echo $row['field'] . ' was found.' . $questname;
    /*if(!$row['field'])
    {
        echo 'You are not on this quest yet.';
    }
    elseif(($row['field']) && ($row['value'] == "notstarted"))
    {
        echo 'This quest hasn\'t started yet.';
    }
    elseif(($row['field']) && ($row['value'] == "started"))
    {
        echo 'You are on quest: "' .$row['field']. '"';
    }*/

    /* $fields = mysql_list_fields("tenaar_slcom","sldb_data");
    $columns = mysql_num_fields($fields);
    for ($i = 0; $i < $columns; $i++) {$field_array[] = mysql_field_name($fields, $i);}

    if(in_array($quest, $field_array))
    {
        echo 'Your quest is ' . $row['field'];
    }
    elseif(!in_array($questname, $field_array))
    {
        echo 'You are not on this quest yet.';
    }
    elseif((in_array($questname, $field_array)) && ($row['value'] == "notstarted"))
    {
        echo 'You have not started quest "' .$row['field']. '" yet.';
    } */    

    // cycle through and print what we got.
    //while($row = mysql_fetch_array($result))
    //{

        //echo 'Quest name: ' . $row['field'] . ' | Progress: ' .$row['value'] . '<br />';

    //}
}
/*else
{
echo 'You are not on this quest yet.';
}*/

// Close the connection.
mysql_close($con);

?>

【问题讨论】:

  • 不看表很难说,但是您是否尝试过基于$questid而不是$questname拉?
  • +1 @nullrevolution。您本可以向我们展示您的表格和预期输出:$
  • 有什么问题?您的数据库查询确实需要这两个值(作为and 条件)。
  • 显然问题是我太穷了,我什至无法注意 x3 我不知何故错过了一个非常基本的步骤。谢谢!

标签: php sql linden-scripting-language


【解决方案1】:

在获取行数和打印字段之间似乎缺少一步。您需要将查询的结果实际存储在 $row 数组中。

$rows_returned = mysql_num_rows($result);
$row = mysql_fetch_array($result);
echo $row['field'];

【讨论】:

  • 不敢相信我错过了这样一个基本步骤 n.n 谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-01-20
  • 1970-01-01
  • 2016-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
相关资源
最近更新 更多