SQL / PHP如何从表中提取数据并将其放在变量中

时间:2012-03-04 02:37:06

标签: php mysql sql

假设我的表中有一个uniqid密钥,并且在get方法中将相同的密钥发送到我的站点,我该如何提取该特定密钥并将表中的所有数据分配给变量。这是我到目前为止所得到的,但似乎无法弄明白。

$query1 = "SELECT * 
       FROM todo_item2 as ti INNER JOIN todo_category2 as tc ON ti.todo_id = tc.todo_id'
       WHERE todo_id = :todo_id";

$statement1 = $db->prepare($query1);
$statement1 -> execute(array(
    'todo_id' =>$id
));

while ($row = $statement1->fetch()) 
{
    $text = $row['todo'];
    $cat = $row['category'];
    $percent = $row['precent'];
    $date = $row['due_date'];
}

2 个答案:

答案 0 :(得分:0)

你应该准备好execute实际做什么.. execute的参数(我假设你在这里使用PDO或类似的东西)是查询的标记。你想要的是:

$query = " ... WHERE todo_id = ?"
$stmt = $db->prepare($query);
$stmt->execute(array($id));
while ($row = $stmt->fetch()) {
   //$row is now an associative array of row values.
}

答案 1 :(得分:0)

// Start the Load
$query1 = "SELECT * 
           FROM todo_item2 as ti INNER JOIN todo_category2 as tc ON ti.todo_id = tc.todo_id
           WHERE ti.todo_id = :todo_id";

$statement1 = $db->prepare($query1);
$statement1 -> execute(array(
    'todo_id' =>$id
));

// Make Sure the Data Exists
if( $statement1->rowCount() == 0 )
{
    die('Please Enter a Valid ID Tag - (id)');
}


while($row = $statement1->fetch())
{
    $text = $row['todo'];
    $cat = $row['category'];
    $percent = $row['percent'];
    $date = $row['due_date'];
}