Is there a better, more “Standard” way to perform SQL queries in PHP without using a framework?

前端 未结 8 1079
借酒劲吻你
借酒劲吻你 2021-01-27 09:02

For the longest time, I\'ve been using the following basic formatting for SQL queries within my PHP:

$sql = \"SELECT * FROM `user-data` WHERE `id` = \'\".$id.\"\         


        
8条回答
  •  梦如初夏
    2021-01-27 09:30

    You can use mysqli to write little place holders in you SQL and then fill them in. It should be less susceptible to SQL injection attacks than string concatenation.

    $conn = new mysqli($server, $username, $password, $database);
    $stmt = $conn->prepare('SELECT * FROM people WHERE age = ? AND name != ?');
    $stmt->bind_param('is', 20, "Austin");
    

提交回复
热议问题