php用mysql怎么写,phpmysql是什么
PHP操作MySQL,删除一行数据库,怎么写?
删除一条记录简单方法,应该说是最简单的入门级的了.
创新互联主要从事网站设计制作、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务聊城,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
mysqldelete from 表名 where id=1;
query ok,
这样就可以删除了,
如果你和php教程 mysql配置只要利用php连接到数据库教程再用php mysql_query("delete from 表名 where id=1");就可以删除了。
?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("delete from person where lastname='griffin'");
mysql_close($con);
?
下面看一下关于mysql delete语法.
删除数据库中的数据
delete from 语句用于从数据库表中删除记录。
语法
delete from table_name
where column_name = some_value
注释:sql 对大小写不敏感。delete from 与 delete from 等效。
为了让 php 执行上面的语句,我们必须使用 mysql_query( 函数。该函数用于向 sql 连接发送查询和命令
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的表查询出来,该怎么写,用来考二级mysql的
?php
$link=mysql_connect('localhost','mysql_user','mysql_password');//mysql的服务器地址,数据库账号,数据库密码 该方法用于链接数据库
if(!$link){
die('Could not connect: ' . mysql_error()); //链接不上返回错误
}
mysql_select_db('XK',$link) or die ('Can\'t use foo : ' . mysql_error()); //链接数据库中其中的一个库‘XK’,连接不上报错。
$sql = mysql_query(”select name,xuehao from girltable where sex=‘girl’ “); //发送一条 MySQL 查询
$result = 'table'; //将查询结果做成表格
while($row=mysql_fetch_assoc($result)){ //从结果中取得一行作为关联数组
$result .= 'tr';
$result .= 'td'$row['name']'/td';
$result .= 'td'$row['xuehao']'/td';
$result .= '/tr';
}
$result .= '/table';
echo $result; //打印表格
?
大神!!新手求教:php多条件查询MYsql怎么写啊
$sql="select * from table where (sex="'".$_POST['sex']."'") and (aihao="'".$_POST['aihao']."'") and (dizhi="'".$_POST['dizhi']."') order by addtime desc"
php mysql 事务怎么写
建一张test表
set autocommit=0;//设置mysql不自动提交就是不自动执行sql语句
begin; //开启一个事务
insert into test value(test);
insert into test value(test2);
commit;//提交 ,会执行上面2句sql语句 此时test 表里面有 test 和 test2 2条数据
begin; //开启一个事务
insert into test values(test3);
rollback; //回滚 , test表里面并没有test3 这条数据
通常 在php里面会判断 if(!mysql_query($sql)){mysql_query('rollback');} 就是如果没有成功执行sql就进行回滚,比如转账我给你10块钱 先从我账号里面减去10元 再从你账户加10元 这就是2条sql 要保证都能执行完成
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怎么写,phpmysql是什么
地址分享:http://myzitong.com/article/hojscg.html