Creating MySQL Prepared Statement

336
July 30, 2017, at 1:45 PM

I have absolutely zero experience protecting my SQL data. I am trying to prevent injection attacks on my web service by using prepared statements. I've followed several tutorials, but each one I've implemented has killed my PHP script. How could I protect this query?

$value = (integer)$_GET["name"];
$sql = "SELECT `coordinates`, `center` , `content_string` FROM Regions WHERE `id` = {$value}";
$result = $conn->query($sql);
$rows = array();
if ($result->num_rows > 0) {
        // output data of each row
        while($r = mysqli_fetch_assoc($result)) {
        $rows[] = $r;
        }
    } 

Here is my attempt:

$value = (integer)$_GET["name"];
$sql = $dbConnection->prepare('SELECT `coordinates`, `center` , `content_string` FROM Regions WHERE `id` = ?');
$sql->bind_param('i', $value);
$sql->execute();
$result = $sql->get_result();
$rows = array();
if ($result->num_rows > 0) {
        // output data of each row
        while($r = mysqli_fetch_assoc($result)) {
        $rows[] = $r;
        }
}

I'm not really sure why this code doesn't work.

Answer 1

Prepared statement with MySQLi

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt = $conn->prepare("INSERT INTO users (username, email) VALUES (?, ?)");
$stmt->bind_param("ss", $username, $email);
// set parameters and execute
$username= "John";
$email = "john@example.com";
$stmt->execute();
$username= "Mary";
$email = "mary@example.com";
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();

Note:

  1. stack overflow is not the place to ask for people to code for you.
  2. Need PDO prepared statement put a comment below.
  3. Have any issue put a comment below.

Best and latest php tips and tricks. http://www.phptherightway.com/

If you are concerned about security this is the topic that I love very much. What are the best PHP input sanitizing functions?

I am using @Charles it works.

Answer 2

You will have to bind the result and below is the code - it is going to work please try it. please check if there any syntax issues in my code. otherwise it will work.

$value = (integer)$_GET["name"];
$sql = $dbConnection->prepare('SELECT 'coordinates', 'center' , 'content_string' FROM Regions WHERE `id` = ?');
$sql->bind_param('i', $value);
$sql->execute();
$sql->bind_result($coordinates, $center, $content_string)
while($sql->fetch())
{
   echo $coordinates;
   echo $center;
   echo $content_string; 
}
Rent Charter Buses Company
READ ALSO
how to put URL inside of laravel blade

how to put URL inside of laravel blade

I have database structure like Book & download link

393
Php image not showing

Php image not showing

I have this program where a puts in his/her name, the image's name, and selects the image from a fileEverything works perfectly but the image

256
How to assign data from Observable Subscribe to object Angularjs 2

How to assign data from Observable Subscribe to object Angularjs 2

I am retrieving JSON data from PHPThe data only returns one record

480
MySQL query fails to execute

MySQL query fails to execute

I am trying to query large amounts of data to a server, here is my code for that:

292