php在哪写数据库代码 php实现数据库查询
PHP连接mysql数据库代码该怎么写啊
1,mysql_connect('数据库服务器','数据库用户名','数据库密码');
创新互联公司企业建站,10多年网站建设经验,专注于网站建设技术,精于网页设计,有多年建站和网站代运营经验,设计师为客户打造网络企业风格,提供周到的建站售前咨询和贴心的售后服务。对于成都网站建设、网站设计中不同领域进行深入了解和探索,创新互联在网站建设中充分了解客户行业的需求,以灵动的思维在网页中充分展现,通过对客户行业精准市场调研,为客户提供的解决方案。
//连接数据库服务器。一般数据库服务器是127.0.0.1:3306,用户名是root,密码是dba。具体要看你安装mysql的设置。
2,mysql_select_db('数据库名');//选择你需要的数据库。
3,mysql_query(执行的sql增删改查语句);
4,mysql_close();//关闭数据库
以上几个函数,具体您可以去百度。代码就是这样的了。
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点抗 ');
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数据库中写入数据有三个步骤:
1,PHP和MySQL建立连接关系
2,打开MySQL数据库
3,接受页面数据,PHP录入到指定的表中
1、2两步可直接使用一个数据库链接文件即可:conn.php
代码如下
?php
mysql_connect("localhost","root","");//连接MySQL
mysql_select_db("hello");//选择数据库
?
当然,前提是已经安装WEB服务器、PHP和MySQL,并且建立MySQL表“cnbruce”
mysql_connect()中三个参数分别为MySQL地址、MySQL用户名和MySQL密码
然后就是通过WEB页面传递数据,让PHP通过SQL语句将数据写入MySQL数据库指定的表中,比如新建文件 post.php
代码如下
?php
require_once("conn.php");//引用数据库链接文件
$uname = $_GET['n'];//GET方法为URL参数传递
$psw = $_GET['p'];
$psw=md5($psw);//直接使用MD5加密
$sql = "insert into members(username,password) values ('$uname','$psw')";
mysql_query($sql);//借SQL语句插入数据
mysql_close();//关闭MySQL连接
echo "成功录入数据";
?
测试页面: ;p=i0514
即可向MySQL数据库hello的members表中插入新的数据“cnbruce”到username字段、“i0514”到password字段
补充:读取表
读取表中的内容,这里我们用while,可以根据具体情况,用for 或其他的.
代码如下
while($row = mysql_fetch_array($result))
{
echo "div style="height:24px; line-height:24px; font-weight:bold;""; //排版代码
echo $row['Topic'] . "br/";
echo "/div"; //排版代码
php用pdo初始化数据库代码怎么写啊,就是如果所指定的数据库已经存在,用户就可以自己输入数据库名
没听懂你的意思。
如果是:确认名为xx的数据库存在,用户可以通过输入新数据库名,来取代xx数据库。
那么你可以用PDO先来个exec查询xx数据库是否存在,然后如果存在,就让用户输入新数据库名,跳转到控制器之后,将旧数据库删除,接着使用prepare、bindValue和execute,创建数据库并新建你所需要的表。
网站栏目:php在哪写数据库代码 php实现数据库查询
URL地址:http://cdiso.cn/article/ddiocgo.html