<!DOCTYPE html>
<html>
<head>
<style>
label{display:inline-blockwidth:100pxmargin-bottom:10px}
</style>
<title>Add students</title>
</head>
<body>
<!-- 数据库用mysqli 面向过程调用方法-->
<form method="post" action="write2db.php">
<!--数据库用mysqli 面向过程调用方法
<form method="post" action="write2db_sqlio.php">
-->
<!--数据库用PDO调用方法
<form method="post" action="write2db_pdo.php">
-->
<label>First Name</label>
<input type="text" name="first_name" />
<br />
<label>Last Name</label>
<input type="text" name="last_name" />
<br />
<label>department</label>
<input type="text" name="department" />
<br />
<label>Email</label>
<input type="text" name="email" />
<br />
<input type="submit" value="Add students">
</form>
</body>
</html>
------------------------------
<?php//拷贝命名为write2db.php,数据库用mysqli 面向过程调用方法
//print_r($_POST)
// create a variable
$first_name=$_POST['first_name']
$last_name=$_POST['last_name']
$department=$_POST['department']
$email=$_POST['email']
//调试用
echo "Your input: "
echo $first_name
echo '<br />'
echo $last_name
echo '<br />'
echo $department
echo '<br />'
echo $email
echo '<br />'
$servername = "localhost"
//Your database username and password
//$username = "username"
//$password = "password"
$username = "tester"
$password = "testerPassword"
//your database name
$dbname = "test"
$tablename ="student"
// Create connection
$connect = mysqli_connect($servername, $username, $password, $dbname)
if (!$connect) {
die("Connection failed: " . mysqli_connect_error())
}
//Execute the query
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')"
if (mysqli_query($connect, $sql)) {
echo "Hooray! New record is inserted to database successfully. Please check database."
} else {
echo "Error: " . $sql . "<br />" . mysqli_error($connect)
}
mysqli_close($connect)
?> <?php
//拷贝命名为write2db_sqlio.php,数据库用mysqli 面向对象调用方法
//print_r($_POST)
// create a variable
$first_name=$_POST['first_name']
$last_name=$_POST['last_name']
$department=$_POST['department']
$email=$_POST['email']
//调试用
echo "Your input: "
echo $first_name
echo '<br />'
echo $last_name
echo '<br />'
echo $department
echo '<br />'
echo $email
echo '<br />'
$servername = "localhost"
//Your database username and password
//$username = "username"
//$password = "password"
$username = "tester"
$password = "testerPassword"
//database name
$dbname = "test"
$tablename ="student"
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname)
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error)
}
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')"
if ($conn->query($sql) === TRUE) {
echo "New record created successfully"
} else {
echo "Error: " . $sql . "<br>" . $conn->error
}
$conn->close()
?> <?php
//拷贝为文件write2db_pdo.php,数据库用PDO调用方法
//print_r($_POST)
a variable
$first_name=$_POST['first_name']
$last_name=$_POST['last_name']
$department=$_POST['department']
$email=$_POST['email']
//调试用
echo "Your input: "
echo $first_name
echo '<br />'
echo $last_name
echo '<br />'
echo $department
echo '<br />'
echo $email
echo '<br />'
$servername = "localhost"
//Your database username and password
//$username = "username"
//$password = "password"
$username = "tester"
$password = "testerPassword"
//your database name
$dbname = "test"
$tablename ="student"
// Create connection
try {
$conn = new PDO("mysql:host=$servernamedbname=$dbname", $username, $password)
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')"
// use exec()
$conn->exec($sql)
echo "New record created successfully"
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage()
}
$conn = null
?> --创建数据库test, 将此文件存为test.sql 导入数据库,或者手动创建表结构
-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: Mar 12, 2018 at 04:04 AM
-- Server version: 5.7.19
-- PHP Version: 7.1.9
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"
SET AUTOCOMMIT = 0
START TRANSACTION
SET time_zone = "+00:00"
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */
/*!40101 SET NAMES utf8mb4 */
--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `student`
--
DROP TABLE IF EXISTS `student`
CREATE TABLE IF NOT EXISTS `student` (
`id` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` varchar(20) NOT NULL,
`last_name` varchar(20) NOT NULL,
`department` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
--
-- Dumping data for table `student`
--
INSERT INTO `student` (`id`, `first_name`, `last_name`, `department`, `email`) VALUES
(1, 'first1', 'last1', 'cs', '1985@qq.com')
COMMIT
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */
PHP连接数据库之PHP连接MYSQL数据库代码
< ?php $mysql_server_name= localhost //改成自己的mysql数据库服务器 $mysql_username= root //改成自己的mysql数据库用户名 $mysql_password= //改成自己的mysql数据库密码 $mysql_database= mycounter //改成自己的mysql数据库名 $conn=mysql_connect($mysql_server_name $mysql_username $mysql_password $mysql_database) $sql= CREATE DATABASE mycounter DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci mysql_query($sql) $sql= CREATE TABLE `counter` (`id` INT( ) UNSIGNED NOT NULL AUTO_INCREMENT `count` INT( ) UNSIGNED NOT NULL DEFAULT PRIMARY KEY ( `id` ) ) TYPE = innodb mysql_select_db($mysql_database $conn) $result=mysql_query($sql) //echo $sql mysql_close($conn) echo "Hello!数据库mycounter已经成功建立!" ?>PHP连接数据库之PHP连接ACCESS数据库代码方法
< ? $conn = new ("ADODB Connection") $connstr = "DRIVER={Microsoft Access Driver (* mdb)} DBQ=" realpath("data/db mdb") $conn >Open($connstr) $rs = new ("ADODB RecordSet") $rs >Open("select * from szd_t" $conn ) while(! $rs >eof) { $f = $rs >Fields( ) echo $f >value $rs >MoveNext() } ?>
PHP连接数据库之PHP连接MS SQL数据库代码方法
安装SQL服务器并添加PHP的MSSQL扩展
使用以下代码连接并测试
< ?php $myServer = localhost //主机 $myUser = sa //用户名 $myPass = password //密码 $myDB = Northwind //MSSQL库名 $s = @mssql_connect($myServer $myUser $myPass) or die(Couldnt connect to SQL Server on $myServer) $d = @mssql_select_db($myDB $s) or die(Couldnt open database $myDB) $query = SELECT TitleOfCourtesy+ +FirstName+ +LastName AS Employee $query = FROM Employees $query = WHERECountry=USA AND Left(HomePhone ) = ( ) $result = mssql_query($query) $numRows = mssql_num_rows($result) echo <h > $numRows Row ($numRows == ? : s) Returned </ h > while($row = mssql_fetch_array($result)) { echo <li> $row[Employee] </li> } ?>
PHP连接数据库之PHP连接Oracle数据库
PHP提供了两套函数与Oracle连接 分别是ORA_和OCI函数 其中ORA_函数略显陈旧 OCI函数更新据说更好一些 两者的使用语法几乎相差无几 你的PHP安装选项应该可以支持两者的使用
< ? if ($conn=Ora_Logon("user@TNSNAME" "password")) { echo "SUCCESS ! Connected to databasen" }else {echo "Failed : ( Could not connect to databasen"} Ora_Logoff($conn) phpinfo() ?> lishixinzhi/Article/program/PHP/201405/30761
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)