php中的数据库怎么调用 php怎么调用数据库数据

php调用数据库字段我说一下几个步骤:
1、首先你得有一个存储这些数据的数据库表,比如数据库表的结构是这样的 。
数据库表名为:user
字段:编号(id),姓名(name),手机(mobile),产品名称(productName) 主键为id
2、实现你需要的功能:
第一步:你需要连接数据库,有一个连接数据库的文件:conn.php 。内容如下:
// 我假设你的数据库是mysql的,假设你的数据库用户名为root,密码为123456,根据你数据库的实际情况改写成你的 。数据库名称假设为db_889888658
?php
$conn=mysql_connect("localhost","root","123456") or die("数据库连接失败,请检查用户名或密码");
mysql_select_db("db_889888658",$conn);
mysql_query("SET NAMES 'gb2312'");
?
第二步:你需要一个添加数据的表单 , 就相当于一个注册或添加数据的页面 。如文件为:add.html内容如下:
form action="reg.php" method="post"
input type="text" name="name"br/
input type="text" name="mobile"br/
input type="text" name="productName"/br
input type="submit" name="submit" value="https://www.04ip.com/post/添加数据"
/form
第三步:写一个处理你表单提交的数据的文件reg.php 。内容如下:
?php
include "conn.php";
if(isset($_POST["submit"])){
$name=$_POST["name"];
$mobile=$_POST["mobile"];
$productName=$_POST["productName"];
$sql="INSERT INTO 'user'(id,name,mobile,productName) VALUES (NULL,$name,$mobile,$productName)";
$query=mysql_query($sql);
$num=mysql_affected_rows($conn);
if($num=1){
echo "scriptalert('数据添加成功');location.href='https://www.04ip.com/post/add.html';/script";
}else{
echo "scriptalert('数据添加失败');history.back();/script";
}
}
?
第四步,第三步已经实现你说的第一个功能 。下面说一下你的第二个功能 。写一个表单 , 输入你要查询的手机号,点击“查询”按钮查询你想要的字段 。
?php
if($_POST["submit"]){
$mobile=$_POST["mobile"];
if(!empty($mobile)){
include "conn.php";
$sql="SELECT * FROM 'user' WHERE 'mobile'='$mobile'";
$query=mysql_query($sql);
while($rs=mysql_fetch_array($query)){
$str="查询结果:br/";
$str.="用户名:".$rs["name"]." ";
$str.="产品名:".$rs["name"]." ";
}
echo "您查询的手机号为".$mobile."的数据信息如下:br/";
echo $str;
}else{
echo "请输入手机号";
}
}
?
form action="" method="post"
请输入您要查询的手机号:input type="text" name="mobile" input type="submit" name="submit" value="https://www.04ip.com/post/查询"
/form
PHP操作mysql数据库的步骤PHP访问MySQL数据库php中的数据库怎么调用:
因为连接数据库需要较长php中的数据库怎么调用的时间和较大的资源开销php中的数据库怎么调用,所以如果在多个网页中都要频繁地访问数据库php中的数据库怎么调用 , 则可以建立与数据库的持续连接 。即调用mysql_pconnect()代替mysql_connect() 。
基本步骤:
1.连接服务器:mysql_connect();
2.选择数据库:mysql_select_db();
3.执行SQL语句:mysql_query();
查询:select
显示:show
插入:insert
into
更新:update
删除:delete
4.关闭结果集:mysql_free_result($result);
5.关闭数据库:mysql_close($link);
PHP调用三种数据库的方法(3)Oracle(甲骨文)是世界上最为流行的关系数据库 。它是大公司推崇的工业化的强有力的引擎 。我们先看看其相关的函数:
(1)integer
ora_logon(string
user
,
string
password)
开始对一个Oracle数据库服务器的连接 。
(2)integer
ora_open(integer
connection)
打开给出的连接的游标 。
(3)integer
ora_do(integer
connection,
string
query)
在给出的连接上执行查询 。PHP生成一个指示器 , 解析查询 , 并执行之 。
(4)integer
ora_parse(integer
cursor,
string
query)
【php中的数据库怎么调用 php怎么调用数据库数据】解析一个查询并准备好执行 。
(5)boolean
ora_exec(integer
cursor)
执行一个先前由ora_parse函数解析过的查询 。
(6)boolean
ora_fetch(integer
cursor)
此函数会使得一个执行过的查询中的行被取到指示器中 。这使得您可以调用ora_getcolumn函数 。
(7)string
ora_getcolumn(integer
cursor,
integer
column)
返回当前的值 。列由零开始的数字索引 。
(8)boolean
ora_logoff(integer
connection)
断开对数据库服务器的链接 。
以下是向ORACLE数据库插入数据的示例程序:
html
headtitle向ORACLE数据库中插入数据/title/head
body
form
action="?echo
$PHP_SELF;?"
method="post"
table
border="1"
cellspacing="0"
cellpadding="0"
tr
thID/th
thname/th
thDescription/th
/tr
tr
tdinput
type="text"
name="name"
maxlength="50"
size="10"/td
tdinput
type="text"
name="email"
maxlength="255"
size="30"/td
tdinput
type="text"
name="Description"
maxlength="255"
size="50"/td
/tr
tr
align="center"
td
colspan="3"input
type="submit"
value="https://www.04ip.com/post/提交"input
type="reset"
value="https://www.04ip.com/post/重写"/td
/tr
/table
/form
?
//先设置两个环境变量ORACLE_HOME,ORACLE_SID
putenv("ORACLE_HOME=/oracle/app/oracle/product/8.0.4");
putenv("ORACLE_SID=ora8");
//设置网页显示中文
putenv("NLS_LANG=Simplified_Chinese.zhs16cgb231280");
if($connection=ora_logon("scott","tiger"))
{
//库表test有ID,name,Description三项
$sql
=
'insert
into
test(ID,name,Description)
values
';
$sql
.=
'(''
.
$ID
.
'',''
.
$name
.
'',''.
$Description
.
'')';
if($cursor=ora_do($connect,$sql))
{
print("insert
finished!");
}
$query
=
'select
*
from
test';
if($cursor=ora_do($connect,$query))
{
ora_fetch($cursor);
$content0=ora_getcolumn($cursor,0);
$content1=ora_getcolumn($cursor,1);
$content2=ora_getcolumn($cursor,2);
print("$content0");
print("$content1");
print("$content2");
ora_close($cursor);
}
ora_logoff($connection);
}
?
/body
/html
php中选择打开数据库的方法是在mysql数据库中php中的数据库怎么调用,创建一个test数据库php中的数据库怎么调用,用于测试 。
请点击输入图片描述
新建一个php文件 , 命名为test.php,用于讲解php如何选择要操作的数据库 。
请点击输入图片描述
在test.php文件中 , 使用header()方法将页面的编码格式设置为utf-8,避免输出中文乱码 。
请点击输入图片描述
在test.php文件中,使用mysql_connect()函数,通过账号和密码创建一个数据库的连接 。
请点击输入图片描述
在test.php文件中,再使用mysql_select_db()函数选择要操作的数据库test,选择数据库成功,则返回true,否则 , 返回false 。最后,通过if语句判断结果 。
请点击输入图片描述
在浏览器打开test.php文件,查看结果 。
请点击输入图片描述
END
总结php中的数据库怎么调用:
1、创建一个test数据库 。
2、使用mysql_connect()函数创建一个数据库的连接 。
3、再使用mysql_select_db()函数选择要操作的数据库test,并通过if语句判断结果 。
php 调用数据库怎么调用?php
mysql_connect("localhost","root","123456") //填写mysql用户名和密码
or die("Could not connect to MySQL server!");
mysql_select_db("phpcms") //数据库名
or die("Could not select database!");
mysql_query('set names "gbk"'); //数据库内数据的编码
?
关于php中的数据库怎么调用和php怎么调用数据库数据的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

    推荐阅读