php 把图片以二进制形式保存到mysql数据库

 更新时间:2016年11月25日 16:39  点击:2023

$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>

// 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>

当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不打开新连接。
 代码如下 复制代码

mysql_connect

 mysql_connect($this->root,$this->user,$this->pass)

/*

mysql_connect ,单个反问用户不会频繁的调用数据库教程,没必要保持连接,而且mysql的连接数也是有限制的, 使用 及时访问比较频繁,也最好使用mysql_connect,这样使用的过的资源可以立刻释放,否则,容易造成资源耗
*/
mysql_pconnect
/*
mysql_pconnect() 函数打开一个到 MySQL 服务器的持久连接
*/

 代码如下 复制代码
$con = mysql_pconnect("localhost","mysql_user","mysql_pwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


 

 代码如下 复制代码
class testLinkMysql{ 
  public $conn;
  public $root='localhost';
  public $user='root';//'loupan';
  public $pass='root';//'loupandsffds';
  public $db='dbname';
  public $charset='gbk';
  public $links='c'; //标题
  
  function __construct() {
    if( !$this->conn )
   {
    $this->connect();
   }   
  }
  
    
  function __destruct() {
   if( $this->conn )
   {
          $this->close();
   }
     }
  
  
  function MysqlConnect()
  {
   try{
    if( 'p' == $this->links )
    {
     $this->conn = mysql_pconnect($this->root,$this->user,$this->pass) or die(mysql_error());       
    }
    else
    {
     $this->conn = mysql_connect($this->root,$this->user,$this->pass) or die( mysql_error());
    }
    mysql_select_db($this->db,$this->conn); 
    mysql_query("set Names '$this->charset'");
   }catch (Exception $e){
    echo '数据库连接失败,请联系相关人员!';
    exit;
   } 
  }
  
  function close()
  {
   mysql_close($this->links);
  }
 }


 
 /*
 总结:
 mysql_pconnect() 和 mysql_connect() 非常相似,但有两个主要区别:


其次,当脚本执行完毕后到 SQL 服务器的连接不会被关闭,此连接将保持打开以备以后使用(mysql_close() 不会关闭由 mysql_pconnect() 建立的连接)。
本站原创教程转载注明来源http://www.111cn.net/phper/php.html

 代码如下 复制代码

$conn=mysql_connect("127.0.0.1","root","root");
 mysql_select_db("ip");
 $sql="select * from adminblog  ";
 $result=mysql_query($sql,$conn);
    
 while($rs=mysql_fetch_array($result))
        {
   echo $rs->username;
   echo $rs->password;
  }
//运行代码提示Notice: Trying to get property of non-object 好了,我们现在修改一下

while($rs=mysql_fetch_array($result))
        {
   echo $rs['username'];
   echo $rs['password'];
  }


 //输出结果: adsense 5498bef14143cd98627fb0560cb5ded6
//现在来看一个mysql_fetch_object的实例

 

 

 代码如下 复制代码
while($rs=mysql_fetch_object($result))
        {
   echo $rs['username'];
   
  }

//运行后出来 Fatal error: Cannot use object of type stdClass as array in 说这不是一个数组

 

 代码如下 复制代码
while($rs=mysql_fetch_object($result))
        {
   echo $rs->username;
   
  }

//输出结果为 adsense
/*
总结:
mysql_fetch_object 把记录作来一个对象来处理,像我们用类时就要用->访问
mysql_fetch_array 把记录保存到一个数据所以可以用$rs['下标名'] 或$rs[0]数组编号

本站原创教程转载注明来源http://www.111cn.net/phper/php.html

转义特殊字符在unescaped_string,考虑到当前字符的连接设置,以便它在的地方是安全的在mysql_query()它。如果二进制数据要插入,这个函数必须被使用。

mysql教程_real_escape_string - 转义特殊字符的SQL语句中使用字符串

string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier ] )

 

mysql_real_escape_string()调用MySQL的库函数mysql_real_escape_string,其中prepends反斜杠以下字符: x00 ñ ṛ,,',“和但X1a。

这个函数必须始终(除了少数例外)被用来制作发送之前查询到MySQL数据的安全。

unescaped_string
该字符串进行转义。

link_identifier
MySQL的连接。如果没有指定连接标识符,最后一个环节开幕mysql_connect()函数假设。如果没有这样的链接被发现,它会尝试创建一个犹如mysql_connect()是不带参数调用。如果没有找到或建立连接,一会生成一条E_WARNING级别的错误。

实例一

<?php教程
// Connect
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
    OR die(mysql_error());

// Query
$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
            mysql_real_escape_string($user),
            mysql_real_escape_string($password));
?>

实例二

<?php
// Query database to check if there are any matching users
$query = "SELECT * FROM users WHERE user='{$_POST['username']}' AND password='{$_POST['password']}'";
mysql_query($query);

// We didn't check $_POST['password'], it could be anything the user wanted! For example:
$_POST['username'] = 'aidan';
$_POST['password'] = "' OR ''='";

// This means the query sent to MySQL would be:
echo $query;
?>
查询发送到MySQL:

选择*从用户的WHERE用户='艾丹'和password = ''或''=''
这将允许任何人登录没有有效的密码。

[!--infotagslink--]

相关文章

  • PHP 数据库缓存Memcache操作类

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

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • Intellij IDEA连接Navicat数据库的方法

    这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下...2021-03-25
  • 在数据库里将毫秒转换成date格式的方法

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

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • 如何解决局域网内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
  • node.js如何操作MySQL数据库

    这篇文章主要介绍了node.js如何操作MySQL数据库,帮助大家更好的进行web开发,感兴趣的朋友可以了解下...2020-10-29
  • Mysql数据库错误代码中文详细说明

    1005:创建表失败1006:创建数据库失败1007:数据库已存在,创建数据库失败1008:数据库不存在,删除数据库失败1009:不能删除数据库文件导致删除数据库失败1010:不能删除数据目录导致删除数据库失败1011:删除数据库...2013-09-23
  • c#异步读取数据库与异步更新ui的代码实现

    这篇文章主要介绍了c#从数据库里取得数据并异步更新ui的方法,大家参考使用吧...2020-06-25
  • Yii2.0高级框架数据库增删改查的一些操作

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2使用中的一些基本的增删改查操作。 User::find()->all(); //返回所有用户数据; User::findOne($id); //返回 主键...2015-11-24
  • MYSQL数据库使用UTF-8中文编码乱码的解决办法

    1.用phpmyadmin创建数据库和数据表 创建数据库的时候,请将“整理”设置为:“utf8_general_ci” 或执行语句: 复制代码 代码如下:CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 创...2015-10-21
  • springBoot 项目排除数据库启动方式

    这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10
  • Linux 下使用shell脚本定时维护数据库的案例

    这篇文章主要介绍了Linux 下使用shell脚本定时维护数据库,本文通过案例分析给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-07-11
  • Java连接数据库oracle中文乱码解决方案

    这篇文章主要介绍了Java连接数据库oracle中文乱码解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-16
  • PHP连接公司内部服务器的MYSQL数据库的简单实例

    “主机,用户名,密码”得到连接、“数据库,sql,连接”得到结果,最后是结果的处理显示。当然,数据库连接是扩展库为我们完成的,我们能做的仅仅是处理结果而已。...2013-09-29
  • C#连接加密的Sqlite数据库的方法

    对数据加密分两种,一种是对数据库本身进行加密,另一种是对数据表中的数据进行加密,下面通过本文给大家介绍C#连接加密的Sqlite数据库的方法,感兴趣的朋友一起看看吧...2020-06-25