I am redesigning a PHP/MySql site that was about 7 years old and had lots of MySql statements. I had updated them all to use MySqli, but have recently heard about "prepared statements", so now I'm wondering if I should take the time to change all the MySqli statements to use prepared statements.
For example, the majority of mysql statements in the current code follow this general format...
$sql = "SELECT * FROM users WHERE userid = '$userid'"; $getnotifyoption = mysqli_query($connection, $sql); if (!$getnotifyoption ) { die("Database query failed: " . mysqli_error()); } else { while ($row = mysqli_fetch_array($getnotifyoption )) { $firstname=$row['firstname']; //etc } }
That's a SELECT obviously, but I also have lots of INSERT statements that follow the same general format. And if I was inserting a first name into the database, I would first do this to it...
$firstname=mysqli_real_escape_string($connection,$_POST['firstname']);
From my recent reading, it seems like that a good thing to do, but definitely not as safe from SQL injection as it could/should be.
Now, I'm reading about prepared statements and struggling to fully grasp them (if anyone has any good tutorials for somewhat newbies, please share a link). And if anyone's feeling generous, let me know how the example statement I gave earlier would be re-written using a prepared statement. But other than that...
Do I need to install something to use prepared statements, or would they just work within my mysqli/php7 environment?
More importantly, the feedback I'm looking for is whether you think I should definitely add what I think would be significant time to the redesign and go back and change ALL of my MySqli statements to use prepared statements?
How much more efficient/safe are prepared statements than the format I use above?
Is my way TERRIBLE, or is it OK to use for now?
Will my way eventually not work? If so, are we talking a year or like 10 years? (i know probably can't answer that :) )