Showing posts with label Mysql. Show all posts
Showing posts with label Mysql. Show all posts

MySQL import export command

Import database:

mysql -u root -p database_name <database.sql

C:\wamp\bin\mysql\mysql5.7.11\bin>mysql -u root -p database_name <database.sql

Mysqli connet and queries

http://stackoverflow.com/questions/15251095/display-data-from-sql-database-into-php-html-table 
 Mysqi connect
===================
$db_host = 'localhost';
$db_user = 'root';
$db_password = '';
$db_name = 'dbname';
$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);

// Insert and Update
=====================
if(!empty($_POST['id'])){
    $query = "UPDATE siteinfo SET site_name = '".$_POST['site_name']."', site_url = '".$_POST['site_url']."' WHERE id=".$_POST['id'];
}else{
    $query = "INSERT INTO siteinfo SET site_name = '" . $_POST['site_name'] . "', site_url = '" . $_POST['site_url'] . "' ";
}
$mysqli->query($query);

// fetch result
==============================
if ($result = $mysqli->query("SELECT * FROM siteinfo"))
{
    while($row = $result->fetch_array(MYSQLI_NUM))
    {
        echo '<pre>';
        print_r($row);
        echo '</pre>';
    }
}

Mysql connect and fetch data
==========================
<?php
$dbname 
'mysql_dbname';

if (!
mysql_connect('mysql_host''mysql_user''mysql_password')) {
    echo 
'Could not connect to mysql';
    exit;
}
$sql "SHOW TABLES FROM $dbname";$result mysql_query($sql);

if (!
$result) {
    echo 
"DB Error, could not list tables\n";
    echo 
'MySQL Error: ' mysql_error();
    exit;
}

while (
$row mysql_fetch_row($result)) {
    echo 
"Table: {$row[0]}\n";
}
mysql_free_result($result);?>

MySQL Query


MyISAM to InnoDB
ALTER TABLE wp_posts ENGINE=InnoDB;
 
Add primary key / Alter table for primary key
 
ALTER TABLE Customer ADD PRIMARY KEY (id); 

What is difference between MYISAM and InnoDB?

MYISAM:
1. MYISAM supports Table-level Locking
2. MyISAM designed for need of speed
3. MyISAM does not support foreign keys hence we call MySQL with MYISAM is DBMS
4. MyISAM stores its tables, data and indexes in diskspace using separate three different files. (tablename.FRM, tablename.MYD, tablename.MYI)
5. MYISAM not supports transaction. You cannot commit and rollback with MYISAM. Once you issue a command it’s done.

INNODB:
1. InnoDB supports Row-level Locking
2. InnoDB designed for maximum performance when processing high volume of data
3. InnoDB support foreign keys hence we call MySQL with InnoDB is RDBMS
4. InnoDB stores its tables and indexes in a tablespace
5. InnoDB supports transaction. You can commit and rollback with InnoDB

Difference between Mysql and Mysqli

“mysqli” extension is an improvement over the old “mysql” extension. In fact, the “i” in mysqli stands for “improved”.
mysqli was developed to take advantage of the new features available in MySQL since version 4.1.3. Also, you can use it since PHP 5. So, if you code with PHP5 with a newer version of MySQL, it’s strongly recommended for you to use the extension “mysqli” over “mysql”.

Key benefits of mysqli over mysql extension

  • Object-oriented interface. You can still use the “old procedural” way of calling the mysql extension but the OO version groups the functions by their purpose.
  • Prepared Statements. Those are useful to prevent SQL injections and are executed faster.
  • Multiple Statements. With this “feature”, you can execute multiple SQL queries inside only one “mysqli” call. This reduces the round trips between the database server and the PHP server.
  • Support for Transactions. This is really useful to write robust applications. It gives you the ability to write a group of SQL statements that will either be executed or all rolled back (usually if there is an error somewhere in the process).
  • Enhanced debugging capabilities. As an example, you can use “mysqli_debug(…)” to save debugging information into a file.
  • Embedded server support. Since MySQL 4.0, there is a library available that can be used to run a complete MySQL server embedded inside a program, usually a desktop application.