本文目录一览:
- 1、PHP代码连接mysql数据库,我照着书上写为什么连接不上呢?
- 2、php代码操作mysql数据库
- 3、php中写mysql语句
- 4、PHP读取MySQL数据代码~呢?
- 5、php表单写入mysql数据库的代码
- 6、如何用PHP代码实现MySQL数据库的增删改查
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数据库成功";
?
如果还是运行错误,请贴出错误提示信息
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语句
不管怎样,在这里我总结了常用的PHP连接MySQL数据库以及读取写入数据库的方法,希望能够帮到你,当然也是作为我自己的一个回顾总结。
1.为了更好地设置数据连接,一般会将数据连接所涉及的值定义成变量.
?
1
2
3
4
5
6
7
$mysql_server_name='localhost'; //改成自己的mysql数据库服务器
$mysql_username='root'; //改成自己的mysql数据库用户名
$mysql_password='123456'; //改成自己的mysql数据库密码
$mysql_database='Mydb'; //改成自己的mysql数据库名
也可把以上变量放在一个文件里,可以随时让其他文件调用.
例如: 将以上内容放在:db_config.php 那么在其他需要用到数据库的页面直接调用.
调用代码:require("db_config.php");
2.连接数据库
?
1
2
3
4
5
6
7
8
9
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting") ; //连接数据库
mysql_query("set names 'utf8'"); //数据库输出编码 应该与你的数据库编码保持一致.南昌网站建设公司百恒网络PHP工程师建议用UTF-8 国际标准编码.
mysql_select_db($mysql_database); //打开数据库
$sql ="select * from news "; //SQL语句
$result = mysql_query($sql,$conn); //查询
3.读取表中的内容,这里我们用while,可以根据具体情况,用for 或其他的.
?
1
2
3
4
5
6
7
8
9
10
11
while($row = mysql_fetch_array($result))
{
echo "div style=\"height:24px; line-height:24px; font-weight:bold;\""; //排版代码
echo $row['Topic'] . "br/";
echo "/div"; //排版代码
}
4.php写入数据库,Mysql数据的写入
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数据库的代码
!--表单文件,拷入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数据库的增删改查
?php
$con = mysql_connect("localhost:3306","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM user");
echo "table border='1'
tr
thUsername/th
thPassword/th
/tr";
while($row = mysql_fetch_array($result)) {
echo "tr";
echo "td" . $row['username'] . "/td";
echo "td" . $row['password'] . "/td";
echo "/tr";
}
echo "/table";
mysql_close($con);
?
从服务器中获取用户所有信息(SQL SELECT语句)并以表格形式出现
?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
mysql_query("DELETE FROM user WHERE username = '$_POST[username]'");
mysql_close($con);
?
删除该用户所有信息delete.php
?php
$con = mysql_connect("localhost:3306","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql = "INSERT INTO user (username,password)
VALUES
('$_POST[username]','$_POST[password]')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?
注册一个新用户insert.php
?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
mysql_query("UPDATE user SET password = '$_POST[password]' WHERE username = '$_POST[username]'");
mysql_close($con);
?
修改一个用户密码update.php
html
head
titleFORM/title
/head
body
br /
h1Insert:/h1
form action="insert.php" method="post"
username:input type="name" name="username"/
br /
password:input type="password" name="password"/
input type="submit" value="submit"/
/form
br /hr /br /
h1Delete/h1
form action="delete.php" method="post"
username:input type="name" name="username" /
br /
Are you sure?input type="submit" value="sure" /
/form
br /hr /br /
h1Update/h1
form action="update.php" method="post"
username:input type="name" name="username"/
br /
You want to change your password into:input type="password" name="password"/
input type="submit" value="submit"/
/form
br /hr /br /
/body
/html
以上三个功能的提交源Operate.html