php - Can I blindly replace all mysql_ functions with mysqli_? -
php - Can I blindly replace all mysql_ functions with mysqli_? -
i have used mysql_query()
throughout project; i've learned mysql_
deprecated of php 5.5, has been removed in php 7.
so, know if can replace mysql_
functions mysqli_
in project blindly? example, replacing mysql_query()
mysqli_query()
. there adverse effect?
the short reply no, functions not equivalent.
the news there converter tool help if you've got lot of calls/projects change. allow scripts work right away.
https://github.com/philip/mysqlconvertertool
it's forked version of oracle version, it's kosher.
that said, it's not hard update code, , might want migrate object orientated methodology anyway ...
1) connection
for intents , purposes, need new connection function saves connection php variable, example;
$mysqli = new mysqli($host,$username,$password,$database);
notice i've saved connection $mysqli
. can save $db
or whatever like, should utilize throughout code reference connection.
remember check connection error though;
if ($mysqli->connect_errno) echo "error - failed connect mysql: " . $mysqli->connect_error;
2) query
note: should protect against sql injection. take @ how can prevent sql-injection in php?, i'm going cover basics here.
you have include connection argument in query, , other mysqli_
functions. in procedural code it's first argument, in oo write class method;
procedural:
$result = mysqli_query($mysqli,$sql);
oo:
$result = $mysqli->query($sql);
3) fetch result
the fetching of result similar old mysql_
function in procedural;
while($row = mysqli_fetch_assoc($result))
but $result
object in mysqli, can utilize object function call;
while($row = $result->fetch_assoc())
4) close connection
so before, need include connection in close function; argument in procedural;
mysqli_close($mysqli);
and object run function on in oo;
$mysqli->close();
i here forever if went through them all, idea. take @ the documentation more information. don't forget convert connection close, result release, or error , row counting functions have.
the basic rule of thumb functions utilize database connection, need include in function (either first argument in procedural, or object utilize phone call function in oo), or result set can alter function mysqli_
or utilize result set object.
php mysql mysqli
Comments
Post a Comment