本文目录一览:
- 1、PHP读取MySQL数据代码~呢?
- 2、谁能帮我这一段关于mysql的php代码
- 3、php中用 mysql 查询整张表数据的代码?
- 4、php调用mysql存储过程,如何实现。 我的代码如下:
- 5、php关于mysql的代码??
- 6、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”为表名。大家可以根据自己的情况酌情修改。
接着,运行它~怎么样~数据是不是顺利被读取了呢?
谁能帮我这一段关于mysql的php代码
能不能说详细点啊?怎么帮,干什么。
?php
$lin = mysql_connect("","",""); //链接数据库
$shujuku1="create database music"; //创造一个数据库
mysql_query($shujuku1);
mysql_select_db('music',$lin) or die(mysql_error); //选择一个数据库
$biao1="create table exomusic(m_name varchar(50),m_zhuanji varchar(50),m_singer varchar(20))"; //创造一个数据表
mysql_query($biao1);
$tianjiashuju="insert into exomusic(m_name,m_zhuanji,m_singer) values('paoxiao','xoxo','exo')";//执行操作
mysql_query($tianjiashuju);
echo "数据库初始化成功!"
?
php中用 mysql 查询整张表数据的代码?
?php
$conn=mysql_connect('localhost','root','root') or die("连接失败").mysql_error();//连接数据库
$select=mysql_select_db('数据库名称',$conn) or die("选择失败").mysql_error();//选择数据库
$ss="set names gbk";
$aa=mysql_query($ss);//设置显示中文
$str="select * from 表名";
$recstr=mysql_query($str);//执行查询
$row=mysql_fetch_row($recstr);
while($row)
{ echo $row[0].$row[1].$row[2];
$row=mysql_fetch_row($recstr);
?
php调用mysql存储过程,如何实现。 我的代码如下:
mysql存储过程返回2个资源,第一个是执行信息,第二个是存储过程返回结果。
mysql_*系列函数无法获取超过1个资源,需使用mysqli或PDO代替。
PDO:
$stmt = $db-prepare("CALL pro_rb_save(?,?,@return_msg);");
$stmt-bindParam(1, $a);
$stmt-bindParam(2, $b);
$stmt-execute ();
$outputArray = $db-query("select @return_msg")-fetch(PDO::FETCH_ASSOC);
var_export($return_msg);
php关于mysql的代码??
$mysql_server_name = "localhost";//数据库服务器名称
$mysql_username = "root";// 连接数据库用户名
$mysql_password = "";// 连接数据库密码
$mysql_database = "";// 数据库的名字
$conn = mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("无法连接服务器!");
mysql_select_db($mysql_database,$conn) or die("无法连接数据库!");
mysql_query('set names gbk');//设定字符集
date_default_timezone_set('Asia/Shanghai');//设置中国时区
$result = mysql_query("*******",$conn);//连接数据库
$num = mysql_num_rows($result);//获取总数
while ($rs = mysql_fetch_array($result)){
循环输出
}
mysql_close($conn);//关闭数据库
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 */;