繁体   English   中英

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

[英]SQL / PHP How to pull data from a table and place it in variable

假设我的表中有一个唯一键,并且该键是通过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'];
}

您应该准备好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.
}
// 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'];
}

暂无
暂无

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

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