本文目录一览:
- 1、php代码操作mysql数据库
- 2、php表单写入mysql数据库的代码
- 3、怎么使用php代码建立mysql数据库
- 4、PHP读取MySQL数据代码~呢?
- 5、PHP代码连接mysql数据库,我照着书上写为什么连接不上呢?
php代码操作mysql数据库
form method="post" action="18.3_index.php" input type="hidden" name="savepost" value="1" / table width="490" height="180" border="0" cellpadding="0" cellspacing="0" tr td width="490" align="center" height="40" colspan="2"添加公告信息/td /tr tr td width="90" align="center"公告主题:/td td width="400" height="30" align="left" input name="txt_title" type="text" id="txt_title" size="40" / * /td /tr tr td width="90" align="center"公告内容:/td td width="400" height="80" align="left"textarea name="txt_content" id="txt_content" cols="50" rows="4"/textarea/td /tr tr td width="490" height="30" colspan="2" align="center" input name="submit1" type="submit" class="btn_grey" value="保存" onClick="return check(form1)" / input name="submit2" type="reset" value="重置" / /td /tr /table /form ?php if (isset($_POST['savepost']) $_SERVER['REQUEST_METHOD'] == 'POST') { $conn=mysql_connect('localhost','root','') or die
php表单写入mysql数据库的代码
!--表单文件,拷入index.php--
!DOCTYPE html
html
head
style
label{display:inline-block;width:100px;margin-bottom:10px;}
/style
titleAdd 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"
--
labelFirst Name/label
input type="text" name="first_name" /
br /
labelLast Name/label
input type="text" name="last_name" /
br /
labeldepartment/label
input type="text" name="department" /
br /
labelEmail/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=$servername;dbname=$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
--
--
-- 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代码建立mysql数据库
$rs = mysql_select_db($dbname,$conn);
if(!$rs){
$rs = mysql_query("CREATE DATABASE `$dbname`; ",$conn);
if(!$rs){
$errstr = GetBackAlert("数据库 {$dbname} 不存在,也没权限创建新的数据库!");
echo $errstr;
exit();
}else{
$rs = mysql_select_db($dbname,$conn);
if(!$rs){
$errstr = GetBackAlert("你对数据库 {$dbname} 没权限!");
echo $errstr;
exit();
}else{
$ctStr = 'CREATE TABLE `wish` (
`id` int(11) NOT NULL auto_increment,
`pname` varchar(30) default NULL,
`pqq` varchar(10) default NULL,
`plocal` varchar(50) default NULL,
`pemail` varchar(30) default NULL,
`context` tinytext,
`addDate` datetime default NULL,
`biz1` varchar(250) default NULL,
`biz2` varchar(250) default NULL,
`biz3` varchar(250) default NULL,
`biz4` varchar(250) default NULL,
`biz5` varchar(250) default NULL,
`biz6` int(11) default NULL,
`biz7` int(11) default NULL,
`biz8` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;';
mysql_query($ctStr,$conn);
}
}
PHP读取MySQL数据代码~呢?
新建view.php文件,其内容为:
复制代码 代码如下:
?php
$link=mysql_connect("localhost","root","之前的管理员密码");
if(!$link) echo "没有连接成功!";
else echo "连接成功!";
?
这样,我们便连接上了MySQL数据库,接下来,就是数据读取部分的代码,根据我的MySQL数据源表结构的介绍,我们试验性的来读取:
depart varchar(45) 部门名称
ename varchar(45) 员工姓名
pcname varchar(45) PC名称
这三段数据。
在view.php中紧接着前边的代码继续写:
复制代码 代码如下:
?php
mysql_select_db("infosystem", $link); //选择数据库
$q = "SELECT * FROM info"; //SQL查询语句
mysql_query("SET NAMES GB2312");
$rs = mysql_query($q, $link); //获取数据集
if(!$rs){die("Valid result!");}
echo "table";
echo "trtd部门名称/tdtd员工姓名/tdtdPC名称/td/tr";
while($row = mysql_fetch_row($rs)) echo "trtd$row[1]/tdtd$row[2]/tdtd$row[3]/td/tr"; //显示数据
echo "/table";
mysql_free_result($rs); //关闭数据集
?
其中,“infosystem”为数据库名,“info”为表名。大家可以根据自己的情况酌情修改。
接着,运行它~怎么样~数据是不是顺利被读取了呢?
PHP代码连接mysql数据库,我照着书上写为什么连接不上呢?
就你上面的代码来看,存在二方面错误:
1、or语句写法错误,你的代码中,所有的or语句都和它之前的代码写在一起,没有空格,这是格式错误;
2、mysql_select_db函数被你写成了
mysql_selcet_db
修改后的代码:
?php
$host="localhost";
$user="root";
$password="770880520";
$dbase_name="wei";
$conn=mysql_connect($host,$user,$password)
or
die("连接mysql服务器失败".mysql_error());
echo
"mysql服务器:$host
用户名:$userbr";
mysql_select_db($dbase_name,$conn)
or
die("连接mysql数据库失败".mysql_error());
echo"数据库:$dbase_namebr";
echo"连接mysql数据库成功";
?
如果还是运行错误,请贴出错误提示信息