1

私は MySQL にかなり慣れていないので、これがばかげた質問のように聞こえる場合は申し訳ありません。

SQL のセキュリティと SQL インジェクションの防止について学んでいたとき、次のような ID の結果をフェッチするときに bindparam を使用する方がよいことを学びました。

//Prepare Statement
$stmt = $mysqli->prepare("SELECT * FROM my_table WHERE id=?");

if ( false===$stmt ) {
  die('prepare() failed: ' . htmlspecialchars($mysqli->error));
}

$rc = $stmt->bind_param("i", $id);

if ( false===$rc ) {
  die('bind_param() failed: ' . htmlspecialchars($stmt->error));
}

$rc = $stmt->execute();

if ( false===$rc ) {
  die('execute() failed: ' . htmlspecialchars($stmt->error));
}


// Get the data result from the query. You need to bind results for each column that is called in the prepare statement above
$stmt->bind_result($col1, $col2, $col3, $col4);

/* fetch values and store them to each variables */
while ($stmt->fetch()) {
   $id = $col1;
   $abc = $col2; 
   $def = $col3; 
   $xyz = $col4;
}

$stmt->close();

$mysqli->close();

Atm、すべての結果を取得するときは、これを使用しています:

$query= "SELECT * FROM my_table";  
$result=mysqli_query($connect, $query);

if (!$result)  
{  
  die('Error fetching results: ' . mysqli_error()); 
  exit();  
}

echo '<table border="1">'; // start a table tag in the HTML
//Storing the results in an Array
while ($row = mysqli_fetch_array($result))  //Creates a loop to loop through results
{  
    echo "<tr><td>" . $row['abc'] . "</td><td>" . $row['def'] . "</td><td>" . $row['xyz'] . "</td></tr>";  
}  
echo '</table>'; //Close the table in HTML

私の質問は:

bind_result1) 2 番目のコードでは、最初の例と同様に、セキュリティ上の理由からすべての結果を取得するときに使用する必要がありますか?

bind_result2) はいの場合、すべての結果を取得して使用していないときに、どのように準備ステートメントを使用でき$idますか?

3) すべての結果を取得するために 2 番目の例を使用した場合、セキュリティ上の問題はありますか?

これを理解するのを手伝ってくれませんか...

4

1 に答える 1