mysql -hlocalhost -u[username] -p[password]
SELECT DATABASE
use [database]
CREATING USERS/ GRANTING ACCESS TO DATABASE
Create new table:
CREATE table [table name](
id int NOT NULL PRIMARY KEY,
title varchar(255) UNIQUE
.
.
.
);
**NOT NULL
**PRIMARY KEY
**UNIQUE
Insert if not entry not existent or do nothing if entry is duplicate
INSERT INTO table(c1,c2)VALUES(v1,v2) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)
Search for duplicate:
SELECT address, count(id) as cnt FROM list
GROUP BY address HAVING cnt > 1
SELECT * FROM all_papers WHERE title IN(SELECT title FROM all_papers WHERE title NOT LIKE '%unavailable%' GROUP BY (title) HAVING COUNT( title ) >1)
PHP with SQL
$USER = [username];
$PASS = [password];
try {
$dsn = "mysql:dbname=$DBNAME;host=$HOST";
$dbh = new PDO($dsn, $USER, $PASS);
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage();
die();
}
$TABLE = [tablename];
$sql = "SELECT * FROM $TABLE";
foreach (self::$dbh->query($sql) as $row) {
.
.
.
}
use
addslashes
to process string before INSERT
沒有留言:
張貼留言