簡體   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