At the start of working with MySQLi on PHP we must create connection:
Example how to create table with PHP on MySQL database.
Example how to select row from table with PHP on MySQL database.
Example how to update row in table with PHP on MySQL database.
Example how to insert values to table with PHP on MySQL database.
Example how to delete row from table with PHP on MySQL database.
Example how to alter table with PHP on MySQL database for adding keys.
$mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die('Not connected');
$mysqli->set_charset(DB_CHARSET);
if (mysqli_connect_errno()) {
printf("Error connection this website: %s\n", mysqli_connect_error());
exit();
}
Example how to create table with PHP on MySQL database.
$sql_create_tbl="CREATE TABLE IF NOT EXISTS `mytable` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) UNSIGNED NOT NULL DEFAULT '0',
`name` char(25) NOT NULL,
`status` varchar(20) NOT NULL DEFAULT 'publish',
`pretext` text,
`content` longtext,
`post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`IP` varchar(255) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`uid` int(9) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
$mysqli->query($sql_create_tbl);
Example how to select row from table with PHP on MySQL database.
$var = 1;Or count rows in table
if ($stmt = $mysqli->prepare("SELECT title FROM `mytable` WHERE uid=?")) {
$stmt->bind_param("s", $var);
$stmt->execute();
$stmt->bind_result($district);
$stmt->fetch();
// printf("%s in %s\n", $var, $district);
echo $district;
$stmt->close();
}
$mysqli->close();
$result = $mysqli->query("SELECT count(*) as countees FROM `mytable`");
$row = $result->fetch_row();
echo '#: ', $row[0];
Example how to update row in table with PHP on MySQL database.
$mysqli->query("UPDATE `mytable` SET uid= '1' WHERE id = '".$id."'; ");
Example how to insert values to table with PHP on MySQL database.
$mysqli->query("INSERT INTO `mytable` (id, name, uid) VALUES (1, 'example', 3) ; ");
Or use the statement with insert/update:
$mysqli->query("INSERT INTO `mytable` (id, a, b) VALUES (1, 2, 3)
ON DUPLICATE KEY UPDATE a=a, b=b;");
Example how to delete row from table with PHP on MySQL database.
$stmt = $mysqli->prepare("DELETE FROM `mytable` WHERE id= ?");
$stmt->bind_param('i', $_POST['ID']);
$stmt->execute();
$stmt->close();
Example how to alter table with PHP on MySQL database for adding keys.
$mysqli->query("ALTER TABLE `mytable`
ADD KEY `key_post_id` (`name`,`post_id`,`id`),
ADD KEY `uid` (`uid`);");
Комментариев нет:
Отправить комментарий