本文概述:
1、在MySQL中建立一个新的数据库。
2、一个怎样储存文件的例子程序。
3、一个怎样访问文件的例子程序。
在MySQL中建立一个新的database。
首先,你必须在你的MySQL中建立一个新的数据库,我们将会把那些二进制文件储存在这个数据库里。在例子中我会使用下列结构,为了建立数据库,你必须做下列步骤:
进入MySQL控制器;
输入命令“create database binary_data;”
输入命令“use binary_data;”
输入命令:
<ccid_code></ccid_code>CREATE TABLE binary_data ( id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, description CHAR(50), bin_data LONGBLOB, filename CHAR(50), filesize CHAR(50), filetype CHAR(50));" (注:不能断行) |
如果没有意外,数据库和表应该建立好了。
一个怎样储存文件的例子程序 用这个例子你可以通过Html表单将文件传输到数据库中。
Store.php3 以下为引用的内容:
<ccid_code></ccid_code><?php // store.php3 - by Florian Dittmer <dittmer@gmx.net> ?> <title>Store binary data into SQL Database</title> <?php // 如果提交了表单,代码将被执行: if ($submit) { // 连接到数据库 // (你可能需要调整主机名,用户名和密码) MYSQL_CONNECT( "localhost", "root", "password"); mysql_select_db( "binary_data"); $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); $result=MYSQL_QUERY( "INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ". "VALUES (’$form_description’,’ $data’,’$form_data_name’,’$form_data_size’,’$form_data_type’)"); $id= mysql_insert_id(); print "<p>This file has the following Database ID: <b>$id</b>"; MYSQL_CLOSE(); } else { // 否则显示储存新数据的表单 ?> |
如果你执行了这个程序,你将会看见一个简单的Html表单,单击“浏览”选择一个文件,然后单击提交。
当文件上传至web服务器之后,程序将会告诉你刚刚上传的文件的ID,记住这个ID,一会儿可能会用到。
一个怎样访问文件的例子程序
<ccid_code></ccid_code><?php // getdata.php3 - by Florian Dittmer <dittmer@gmx.net> // 调用方法: getdata.php3?id=<id> if($id) { // 你可能需要调整主机名,用户名和密码: @MYSQL_CONNECT( "localhost", "root", "password"); @mysql_select_db( "binary_data"); $query = "select bin_data,filetype from binary_data where id=$id"; $result = @MYSQL_QUERY($query); $data = @MYSQL_RESULT($result,0, "bin_data"); $type = @MYSQL_RESULT($result,0, "filetype"); Header( "Content-type: $type"); echo $data; }; ?></id> |
程序必须知道要访问那个文件, 你必须将ID作为一个参数。
例如: 一个文件在数据库中的ID为2
你可以这样调用它: getdata.php3?id=2
如果你将图片储存在数据库里, 你可以向调用图片一样调用它。
例如: 一个图片文件在数据库中的ID为3. 你可以这样调用它:
怎样储存大于1MB的文件:
如果你想储存大于1MB的文件,你必须对你的程序、PHP设置、SQL设置进行许多修改,下面几条也许可以帮助你储存小于24MB的文件:
修改:store.php3 ,将 MAX_FILE_SIZE 的值改成 24000000。
修改:你的PHP设置,在一般情况下,PHP只允许小于2MB的文件,你必须将max_filesize(在php.ini中)的值改成24000000。 去掉MYSQL的数据包大小限制,在一般情况下 MYSQL小于1MB的数据包。

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools