php 连接数据库并显示所要的数据

 更新时间:2016年11月25日 16:39  点击:1462
这是一篇php入门教程,讲得就是关于php 连接数据库并显示所要的数据哦,也就是简单的php+mysql数据库在web网页上的应用实例了。
 代码如下 复制代码

$Host="localhost";

  $MySQL_UserName="root";

  $MySQL_UserPass="password";

  $MySQL_Database="db";

  $Query="SELECT * FROM domain";

  if (!mysql_connect ($Host, $MySQL_UserName, $MySQL_UserPass)){

  print "Cannot connect to MySQL: ".mysql_error();

  exit;

  }

  if (!mysql_select_db ($MySQL_Database)){

  print "Cannot select db<BR>";

  exit;

  }

  if (!$Result_ID=mysql_query ($Query)){

  print "Query Error: ".mysql_error();

  exit;

  }

  while ($Result=mysql_fetch_row($Result_ID)){

  print "------------------<BR>";

  print "$Result[0]<BR>";

  print "$Result[1]<BR>";

  print "$Result[2]<BR>";

  print "$Result[3]<BR>";

  print "-------------------<BR>";

  }?>

本款程序是一款简单的实例的php mysql 数据库的页面计数器实例代码,这样你就可以查询你网页访问次数了。

<HTML>
<HEAD>
<TITLE>基于数据库的页面计数器范例</TITLE>
</HEAD>

<BODY>

 代码如下 复制代码

<?PHP

 $db=mysql_connect("localhost","root","qwaszx");

 $query="select * from num";

 $result=mysql_db_query("test",$query);

 if($result){

   $r=mysql_fetch_array($result);
   $counter=$r["NumValue"];
   $counter=$counter+1; 

   $query='update num set NumValue='.$counter.' where id=1';

   $result=mysql_db_query("test",$query);

   mysql_close();
   echo '您好,您是第'. $counter.'位访客';
 }
 
?>

</BODY>
</HTML>

 代码如下 复制代码
  $link=mysql教程_connect("localhost","root","3435945")or die("***".mysql_error());
   echo "成功接连到服务器";
  if(mysql_select_db(base2,$link)) echo"选择到base2数据库教程";
 
 
  $sql="create table t1(id int(5) not null primary key,name varchar(12) not null)";
  if(mysql_query($sql,$link))
  echo "表T1创建成功";
  echo "<p>";
 
  echo "当前base2数据库上的表有:";
  echo "<p>";
  $table_list=mysql_query("show tables",$link);
  while($row=mysql_fetch_row($table_list)){
     echo $row[0];
     echo "<p>";

// Connect to database 
 

 代码如下 复制代码
$errmsg = "";
if (! @mysql教程_connect("localhost","root","")) {
   $errmsg = "Cannot connect to database";
   }
@mysql_select_db("db1");
 
$q = <<<CREATE
create table pix (
    pid int primary key not null auto_increment,
    title text,
    imgdata longblob)
CREATE;
@mysql_query($q);


 
// Insert any new image into database 
 

 代码如下 复制代码
if ($_REQUEST[completed] == 1) {
   move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
   $instr = fopen("latest.img","rb");
   $image = addslashes(fread($instr,filesize("latest.img")));
   if (strlen($instr) < 149000) {
      mysql_query ("insert into pix (title, imgdata) values ("".
      $_REQUEST[whatsit].
      "", "".
      $image.
      "")");
   } else {
      $errmsg = "Too large!";
   }
}


 
// Find out about latest image 
 

 代码如下 复制代码
$gotten = @mysql_query("select * from pix order by pid desc limit 1");
if ($row = @mysql_fetch_assoc($gotten)) {
   $title = htmlspecialchars($row[title]);
   $bytes = $row[imgdata];
} else {
   $errmsg = "There is no image in the database yet";
   $title = "no database image available";
   // Put up a picture of our training centre
   $instr = fopen("../wellimg/ctco.jpg","rb");
   $bytes = fread($instr,filesize("../wellimg/ctco.jpg"));
}
 


// If this is the image request, send out the image
 
if ($_REQUEST[gim] == 1) {
   header("Content-type: image/jpeg");
   print $bytes;
   exit ();
   }
?>

 代码如下 复制代码
 
<html><head>
<title>Upload an image to a database</title>
<body bgcolor=white><h2>Here's the latest picture</h2>
<font color=red><?= $errmsg ?></font>
<center><img src= width=144><br>
<b><?= $title ?></center>
<hr>
<h2>Please upload a new picture and title</h2>
<form enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value=150000>
<input type="hidden" name="completed" value=1>
Please choose an image to upload: <input type="file" name="imagefile"><br>
Please enter the title of that picture: <input name="whatsit"><br>
then: <input type="submit"></form><br>
 


</body>
</html>

$errmsg = "" ;
if (! @ mysql_connect ( "localhost" , "trainee" , "abc123" )) {
$errmsg = "Cannot connect to database" ;
}
@ mysql_select_db ( "wellho" );

// First run ONLY - need to create table by un commenting this
// Or with silent @ we can let it fail every subsequent time ;-)

$q = <<<CREATE
create table pix (
pid int primary key not null auto_increment,
title text,
imgdata longblob)
CREATE;
@ mysql_query ( $q );

// Insert any new image into database

if ( $_REQUEST [ completed ] == 1 ) {
// Need to add - check for large upload. Otherwise the code
// will just duplicate old file ;-)
// ALSO - note that latest.img must be public write and in a
// live application should be in another (safe!) directory.
move_uploaded_file ( $_FILES [ 'imagefile' ][ 'tmp_name' ], "latest.img" );
$instr = fopen ( "latest.img" , "rb" );
$image = mysql_real_escape_string ( fread ( $instr , filesize ( "latest.img" )));
if ( strlen ( $instr ) < 149000 ) {
mysql_query ( "insert into pix (title, imgdata) values ("" .
$_REQUEST [ whatsit ].
"", "" .
$image .
"")" );
} else {
$errmsg = "Too large!" ;
}
}

// Find out about latest image

$gotten = @ mysql_query ( "select * from pix order by pid desc limit 1" );
if ( $row = @ mysql_fetch_assoc ( $gotten )) {
$title = htmlspecialchars ( $row [ title ]);
$bytes = $row [ imgdata ];
} else {
$errmsg = "There is no image in the database yet" ;
$title = "no database image available" ;
// Put up a picture of our training centre
$instr = fopen ( "../wellimg/ctco.jpg" , "rb" );
$bytes = fread ( $instr , filesize ( "../wellimg/ctco.jpg" ));
}

// If this is the image request, send out the image

if ( $_REQUEST [ gim ] == 1 ) {
header ( "Content-type: image/jpeg" );
print $bytes ;
exit ();
}
?>

<html><head>
<title>Upload an image to a database</title>
<body bgcolor=white><h2>Here's the latest picture</h2>
<font color=red> <?= $errmsg ?> </font>
<center><img src= width=144><br>
<b> <?= $title ?> </center>
<hr>
<h2>Please upload a new picture and title</h2>
<form enctype=multipart/form-data method=post>
<input type=hidden name=MAX_FILE_SIZE value=150000>
<input type=hidden name=completed value=1>
Please choose an image to upload: <input type=file name=imagefile><br>
Please enter the title of that picture: <input name=whatsit><br>
then: <input type=submit></form><br><br>
<a href=pic_alog.php4>Link - view images already uploaded</a><br>
<a href=/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html>Link - technical article</a> including source code<br><br>
Note</b> - by uploading an image to this site, you agree that you are the
copyright owner, that the picture is legally acceptable and that you take
full responsibility for this, and that the owners of this site are
henceforth able to make free limitless use of the picture if they so
wish. Your IP address and other details may be logged as you upload.
Sorry about this note - have to protect ourselves.<b>
<hr>
By Graham Ellis - graham@wellho.net</b>
</body>
</html>

[!--infotagslink--]

相关文章

  • PHP 数据库缓存Memcache操作类

    操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • php简单数据操作的实例

    最基础的对数据的增加删除修改操作实例,菜鸟们收了吧...2013-09-26
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • 解决Mybatis 大数据量的批量insert问题

    这篇文章主要介绍了解决Mybatis 大数据量的批量insert问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-09
  • Antd-vue Table组件添加Click事件,实现点击某行数据教程

    这篇文章主要介绍了Antd-vue Table组件添加Click事件,实现点击某行数据教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-17
  • Intellij IDEA连接Navicat数据库的方法

    这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下...2021-03-25
  • 详解如何清理redis集群的所有数据

    这篇文章主要介绍了详解如何清理redis集群的所有数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-18
  • 在数据库里将毫秒转换成date格式的方法

    在开发过程中,我们经常会将日期时间的毫秒数存放到数据库,但是它对应的时间看起来就十分不方便,我们可以使用一些函数将毫秒转换成date格式。 一、 在MySQL中,有内置的函数from_unixtime()来做相应的转换,使用如下: 复制...2014-05-31
  • C#操作本地文件及保存文件到数据库的基本方法总结

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • vue 获取到数据但却渲染不到页面上的解决方法

    这篇文章主要介绍了vue 获取到数据但却渲染不到页面上的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-19
  • php把读取xml 文档并转换成json数据代码

    在php中解析xml文档用专门的函数domdocument来处理,把json在php中也有相关的处理函数,我们要把数据xml 数据存到一个数据再用json_encode直接换成json数据就OK了。...2016-11-25
  • mybatis-plus 处理大数据插入太慢的解决

    这篇文章主要介绍了mybatis-plus 处理大数据插入太慢的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-18
  • 如何解决局域网内mysql数据库连接慢

    通过内网连另外一台机器的mysql服务, 确发现速度N慢! 等了大约几十秒才等到提示输入密码。 但是ping mysql所在服务器却很快! 想到很久之前有过类似的经验, telnet等一些服务在连接请求的时候,会做一些反向域名解析(如果...2015-10-21
  • MySQL快速复制数据库数据表的方法

    某些时候,例如为了搭建一个测试环境,或者克隆一个网站,需要复制一个已存在的mysql数据库。使用以下方法,可以非常简单地实现。假设已经存在的数据库名字叫db1,想要复制一份,命名为newdb。步骤如下:1. 首先创建新的数据库newd...2015-10-21
  • mysqldump命令导入导出数据库方法与实例汇总

    mysqldump命令的用法1、导出所有库系统命令行mysqldump -uusername -ppassword --all-databases > all.sql 2、导入所有库mysql命令行mysql>source all.sql; 3、导出某些库系统命令行mysqldump -uusername -ppassword...2015-10-21
  • Mysql数据库错误代码中文详细说明

    1005:创建表失败1006:创建数据库失败1007:数据库已存在,创建数据库失败1008:数据库不存在,删除数据库失败1009:不能删除数据库文件导致删除数据库失败1010:不能删除数据目录导致删除数据库失败1011:删除数据库...2013-09-23
  • postgresql数据添加两个字段联合唯一的操作

    这篇文章主要介绍了postgresql数据添加两个字段联合唯一的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-04
  • c#异步读取数据库与异步更新ui的代码实现

    这篇文章主要介绍了c#从数据库里取得数据并异步更新ui的方法,大家参考使用吧...2020-06-25
  • Vue生命周期activated之返回上一页不重新请求数据操作

    这篇文章主要介绍了Vue生命周期activated之返回上一页不重新请求数据操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-26