Note that there are some explanatory texts on larger screens.

plurals
  1. POPorting a PHP script from mysql_... to PDO
    primarykey
    data
    text
    <p>I am trying to port the database backup script found on <a href="http://www.daniloaz.com/en/560/programming/backup-de-bases-de-datos-mysql-con-php/" rel="nofollow">this page</a> because it is throwing a warning that <code>mysql_...</code> is deprecated.</p> <p>My efforts so far are below, but the script is throwing couple errors at me that I just don't know how to solve. I'm only slightly familiar with PHP, so it is quite possible that I'm making a beginner's mistake here.</p> <p>The error thrown is:</p> <pre><code>Notice: Undefined variable: db in ......../index.php on line 109 Fatal error: Call to a member function query() on a non-object in ......../index.php on line 109 </code></pre> <p>Line 109 is (and marked with // <strong><em>*</em>**<em>*</em>**<em>*</em></strong>):</p> <pre><code>foreach( $db-&gt;query('SHOW TABLES') as $row ) </code></pre> <p>I've been playing around with <code>public</code>/<code>protected</code> (for <code>initializeDatabase</code>) and <code>global</code> (for <code>$db</code>) keywords, but I'm not sure if that is the cause or if I'm doing it wrong.</p> <p>Can anyone easily spot what I'm doing wrong here?</p> <hr> <pre><code>&lt;?php /** * This file contains the Backup_Database class wich performs * a partial or complete backup of any given MySQL database * @author Daniel López Azaña &lt;http://www.daniloaz.com&gt; * @version 1.0 */ /** * Changes by jippie: * ereg_replace("\n","\\n",$row[$j]); =&gt; preg_replace( '/\n/ , "\\n" , $row[$j] ); * mysql_... =&gt; PDO */ // Report all errors error_reporting(E_ALL); /** * Define database parameters here */ define("DB_USER", 'username'); define("DB_PASSWORD", 'password'); define("DB_NAME", 'database'); define("DB_HOST", 'localhost'); define("OUTPUT_DIR", 'cache'); define("TABLES", '*'); /** * Instantiate Backup_Database and perform backup */ $backupDatabase = new Backup_Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $status = $backupDatabase-&gt;backupTables(TABLES, OUTPUT_DIR) ? 'OK' : 'KO'; echo "&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Backup result: ".$status; /** * The Backup_Database class */ class Backup_Database { /** * Host where database is located */ var $host = ''; /** * Username used to connect to database */ var $username = ''; /** * Password used to connect to database */ var $passwd = ''; /** * Database to backup */ var $dbName = ''; /** * Database charset */ var $charset = ''; /** * Constructor initializes database */ function Backup_Database($host, $username, $passwd, $dbName, $charset = 'utf8') { $this-&gt;host = $host; $this-&gt;username = $username; $this-&gt;passwd = $passwd; $this-&gt;dbName = $dbName; $this-&gt;charset = $charset; $this-&gt;initializeDatabase(); } protected function initializeDatabase() { $db = new PDO("mysql:host=$this-&gt;host;dbname=$this-&gt;dbName;charset=$this-&gt;charset", $this-&gt;username, $this-&gt;passwd ); $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); /* JPH: mysql_... is deprecated * $conn = mysql_connect($this-&gt;host, $this-&gt;username, $this-&gt;passwd); * mysql_select_db($this-&gt;dbName, $conn); * if (! mysql_set_charset ($this-&gt;charset, $conn)) * { * mysql_query('SET NAMES '.$this-&gt;charset); * } */ } /** * Backup the whole database or just some tables * Use '*' for whole database or 'table1 table2 table3...' * @param string $tables */ public function backupTables($tables = '*', $outputDir = '.') { try { /** * Tables to export */ if($tables == '*') { $tables = array(); // $result = mysql_query('SHOW TABLES'); // while($row = mysql_fetch_row($result)) // ******* foreach( $db-&gt;query('SHOW TABLES') as $row ) { $tables[] = $row[0]; } } else { $tables = is_array($tables) ? $tables : explode(',',$tables); } $sql = 'CREATE DATABASE IF NOT EXISTS '.$this-&gt;dbName.";\n\n"; $sql .= 'USE '.$this-&gt;dbName.";\n\n"; /** * Iterate tables */ foreach($tables as $table) { echo "Backing up ".$table." table..."; // $result = mysql_query('SELECT * FROM '.$table); // $numFields = mysql_num_fields($result); $result = $db-&gt;query( 'SELECT * FROM ' . $table ); $numFields = $result-&gt;columnCount(); $sql .= 'DROP TABLE IF EXISTS '.$table.';'; // $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); $result2 = $db-&gt;query( 'SHOW CREATE TABLE ' . $table ); $row2 = $result2-&gt;fetch(); $sql.= "\n\n".$row2[1].";\n\n"; for ($i = 0; $i &lt; $numFields; $i++) { // while($row = mysql_fetch_row($result)) while( $row = $result2-&gt;fetch() ) { $sql .= 'INSERT INTO '.$table.' VALUES('; for($j=0; $j&lt;$numFields; $j++) { $row[$j] = addslashes($row[$j]); $row[$j] = preg_replace( '/\n/' , "\\n" , $row[$j] ); if (isset($row[$j])) { $sql .= '"'.$row[$j].'"' ; } else { $sql.= '""'; } if ($j &lt; ($numFields-1)) { $sql .= ','; } } $sql.= ");\n"; } } $sql.="\n\n\n"; echo " OK" . "&lt;br /&gt;"; } } catch (Exception $e) { var_dump($e-&gt;getMessage()); return false; } return $this-&gt;saveFile($sql, $outputDir); } /** * Save SQL to file * @param string $sql */ protected function saveFile(&amp;$sql, $outputDir = '.') { if (!$sql) return false; try { $handle = fopen($outputDir.'/db-backup-'.$this-&gt;dbName.'-'.date("Ymd-His", time()).'.sql','w+'); fwrite($handle, $sql); fclose($handle); } catch (Exception $e) { var_dump($e-&gt;getMessage()); return false; } return true; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload