sqlite - sqlite2 to sqlite3 convert php code -


i'm trying run script on server. script uses sqlite2 database. php file written in sqlite2, on server have installed sqlite3.

this code:

if (!$db = sqlite_open ('chatdb')) {     die('database problem. please try again later.'); }  function sqlite_table_exists ($db, $mytable) {     $result = sqlite_query($db, "select count(*) sqlite_master type='table' , name='$mytable'");     $count = intval(sqlite_fetch_single($result));     return $count > 0; }  if (sqlite_table_exists($db, 'messages') == false) {     $sql = 'create table messages (username varchar(50), message text, date date)';     sqlite_query ($db, $sql); }  if (sqlite_table_exists($db, 'users') == false) {     $sql = 'create table users (username varchar(50) unique, last_activity date, is_kicked integer default 0, is_banned integer default 0, kick_ban_message varchar(100))';     sqlite_query ($db, $sql); }  if (sqlite_table_exists($db, 'bans') == false) {     $sql = 'create table bans (ip varchar(15), time date)';     sqlite_query ($db, $sql); } 

when run code, errors, such as;

fatal error: call undefined function sqlite_open() 

i think code not ready sqlite3. how can convert code sqlite3?

thanks.

you need use sqlite3 class. significant difference current code you'll using object , methods instead of procedural functions.

for connecting (or creating) database need instantiate new sqlite3 object. if there problem exception thrown:

try {     $db = new sqlite3('chatdb'); } catch (exception $e) {     die($e->getmessage()); } 

you shouldn't concatenate variables queries security risk (see: how can prevent sql-injection in php?) i've modified function use binding:

function sqlite_table_exists($db, $mytable) {     $stmt = $db->prepare("select count(*) sqlite_master type='table' , name=:name");     $stmt->bindvalue('name', $mytable);     $result = $stmt->execute();      $row = $result->fetcharray(sqlite3_num);      return $row[0] > 0; } 

then can create tables so:

if (sqlite_table_exists($db, 'table_name') == false) {     $db->exec('create table ...'); } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -