PHP操作MySQL数据库一般可分为5个步骤:1.衔接MySQL数据库服务器;2.挑选数据库;3.履行SQL句子;4.封闭成果集;5断开与MySQL数据库服务器衔接。
1.用mysql_connect()函数衔接MySQL数据库服务器
用mysql_connect()函数建立与服务器的衔接。然后依据此函数的回来值定位不同的衔接。
$host=”localhost”;//MySQL服务器地址
$user=”root”;//用户名
$pwd=”***”;//密码
$connID=mysql_connect($host,$user,$pwd);//回来的衔接标识符
2.用mysql_select_db()函数挑选数据库文件
用mysql_select_db()函数依据第一步回来的衔接标识符来挑选数据库
mysql_select_db($dbName,$connID);//$dbName表示要挑选的数据库
3.用mysql_query()函数履行SQL句子
第一步:mysql_query(“select*fromtb_stu”,$connID);//履行查询回来成果集
第二步:从上述成果会集获取信息,这里有两种途径。1.用mysql_fetch_array()函数从数组成果会集获取信息;2.用mysql_fetch_object()函数从成果会集获取一行作为对象。它们的差异在于mysql_fetch_object()回来值是一个对象,而不是数组,也便是该函数只能经过字段名来访问数组。
$result=mysql_fetch_array($query);
OR
$result=mysql_fetch_object($query);
对数据的操作一般以下5种:
1.查询数据(select)
2.显现数据(select)
3.刺进数据(insert)
4.更新数据(update)
5.删去数据(delete)
4.封闭成果集
数据库操作完结之后,需求封闭成果集,开释资源
mysql_free_result($result);
5.断开服务器衔接
每使用一次mysql_connect()或mysql_query()函数,都会耗费系统资源,为避免资源糟蹋,用mysql_close()函数封闭与MySQL服务器的衔接,以节约系统资源。

php

docker部署php

PHP是当下最流行的Web服务器端开发言语,号称地球上最好的言语,没有之一。Docker部署PHP环境有两种方法:经过Dockerfile构建和从Docker库房拉取
咱们以当时最新的版别7.2.6装置为例
1.运用dockerpullphp
这是最简略的方法,开箱即用
运用dockersearchphp指令能够列出docker.io上一切的PHP有关的镜像
[root@localhost~]#dockersearchphp
NAMEDESCRIPTIONOFFICIAL
phpWhiledesignedforweb…[OK]

有很多版别,咱们挑选官方的php
拉取最新的PHP标签:7.2.6-fpm-stretch
[root@localhost~]#dockerpullphp:7.2.6-fpm-stretch
稍等片刻,下载完成后,就能够在本地镜像列表里找到REPOSITORY为php,标签为7.2.6-fpm-stretch的镜像
[root@localhost~]#dockerimagesphp
REPOSITORYTAGIMAGEID…SIZE
php7.2.6-fpm-stretch0a757334c1f6…367.7MB
2.经过Dockerfile文件构建
运用Dockerfile文件构建,咱们能够完成定制,而且熟悉PHP的装置进程
创立目录php,用于寄存后边的相关东西
[root@localhost~]#mkdir-pphp/logsphp/conf
文件阐明
logs该目录将映射为phpops容器的日志目录
conf该目录里的配置文件将映射为phpops容器的配置文件
进入php目录,并创立以下几个文件
[root@localhost~]#cdphp
如果你嫌复制太费事,能够直接去GitHub上下载
docker-php-entrypoint
#!/bin/sh#移除榜首行的#!之间的空格set-e#firstargis`-f`or`–some-option`if[“${1#-}”!=”$1″];thenset–php-fpm”$@”fiexec”$@”
docker-php-ext-configure
#!/bin/sh#移除榜首行的#!之间的空格set-e#preferusersuppliedCFLAGS,butdefaulttoourPHP_CFLAGS:${CFLAGS:=$PHP_CFLAGS}:${CPPFLAGS:=$PHP_CPPFLAGS}:${LDFLAGS:=$PHP_LDFLAGS}exportCFLAGSCPPFLAGSLDFLAGSsrcExists=if[-d/usr/src/php];thensrcExists=1fidocker-php-sourceextractif[-z”$srcExists”];thentouch/usr/src/php/.docker-delete-meficd/usr/src/php/extusage(){echo”usage:$0ext-name[configureflags]”echo”ie:$0gd–with-jpeg-dir=/usr/local/something”echoecho’Possiblevaluesforext-name:’find.\-mindepth2\-maxdepth2\-typef\-name’config.m4’\|xargs-n1dirname\|xargs-n1basename\|sort\|xargsechoecho’SomeoftheabovemodulesarealreadycompiledintoPHP;pleasecheck’echo’theoutputof”php-i”toseewhichmodulesarealreadyloaded.’}ext=”$1″if[-z”$ext”]||[!-d”$ext”];thenusage>&2exit1fishiftpm=’unknown’if[-e/lib/apk/db/installed];thenpm=’apk’fiif[“$pm”=’apk’];thenif\[-n”$PHPIZE_DEPS”]\&&!apkinfo–installed.phpize-deps>/dev/null\&&!apkinfo–installed.phpize-deps-configure>/dev/null\;thenapkadd–no-cache–virtual.phpize-deps-configure$PHPIZE_DEPSfifiifcommand-vdpkg-architecture>/dev/null;thengnuArch=”$(dpkg-architecture–queryDEB_BUILD_GNU_TYPE)”set—-build=”$gnuArch””$@”ficd”$ext”phpize./configure”$@”
docker-php-ext-enable
#!/bin/sh#移除榜首行的#!之间的空格set-eextDir=”$(php-r’echoini_get(“extension_dir”);’)”cd”$extDir”usage(){echo”usage:$0[options]module-name[module-name…]”echo”ie:$0gdmysqli”echo”$0pdopdo_mysql”echo”$0–ini-name0-apc.iniapcuapc”echoecho’Possiblevaluesformodule-name:’find-maxdepth1\-typef\-name’*.so’\-execbasename'{}”;’\|sort\|xargsechoecho’SomeoftheabovemodulesarealreadycompiledintoPHP;pleasecheck’echo’theoutputof”php-i”toseewhichmodulesarealreadyloaded.’}opts=”$(getopt-o’h?’–long’help,ini-name:’–“$@”||{usage>&2&&false;})”evalset–“$opts”iniName=whiletrue;doflag=”$1″shiftcase”$flag”in–help|-h|’-?’)usage&&exit0;;–ini-name)iniName=”$1″&&shift;;–)break;;*){echo”error:unknownflag:$flag”usage}>&2exit1;;esacdonemodules=formodule;doif[-z”$module”];thencontinuefiif[-f”$module.so”]&&![-f”$module”];then#allow”.so”tobeoptionalmodule=”$module.so”fiif![-f”$module”];thenecho>&2″error:’$module’doesnotexist”echo>&2usage>&2exit1fimodules=”$modules$module”doneif[-z”$modules”];thenusage>&2exit1fipm=’unknown’if[-e/lib/apk/db/installed];thenpm=’apk’fiapkDel=if[“$pm”=’apk’];thenif\[-n”$PHPIZE_DEPS”]\&&!apkinfo–installed.phpize-deps>/dev/null\&&!apkinfo–installed.phpize-deps-configure>/dev/null\;thenapkadd–no-cache–virtual’.docker-php-ext-enable-deps’binutilsapkDel=’.docker-php-ext-enable-deps’fififormodulein$modules;doifreadelf–wide–syms”$module”|grep-q’zend_extension_entry$’;then#https://wiki.php.net/internals/extensions#loading_zend_extensionsabsModule=”$(readlink-f”$module”)”line=”zend_extension=$absModule”elseline=”extension=$module”fiext=”$(basename”$module”)”ext=”${ext%.*}”ifphp-r’exit(extension_loaded(“‘”$ext”‘”)?0:1);’;then#thisisn’tperfect,butit’sbetterthannothing#(forexample,’opcache.so’presentsinsidePHPas’ZendOPcache’,not’opcache’)echo>&2echo>&2″warning:$ext($module)isalreadyloaded!”echo>&2continuefiini=”/usr/local/etc/php/conf.d/${iniName:-“docker-php-ext-$ext.ini”}”if!grep-q”$line””$ini”2>/dev/null;thenecho”$line”>>”$ini”fidoneif[“$pm”=’apk’]&&[-n”$apkDel”];thenapkdel$apkDelfi
docker-php-ext-install
#!/bin/sh#移除榜首行的#!之间的空格set-e#preferusersuppliedCFLAGS,butdefaulttoourPHP_CFLAGS:${CFLAGS:=$PHP_CFLAGS}:${CPPFLAGS:=$PHP_CPPFLAGS}:${LDFLAGS:=$PHP_LDFLAGS}exportCFLAGSCPPFLAGSLDFLAGSsrcExists=if[-d/usr/src/php];thensrcExists=1fidocker-php-sourceextractif[-z”$srcExists”];thentouch/usr/src/php/.docker-delete-meficd/usr/src/php/extusage(){echo”usage:$0[-jN]ext-name[ext-name…]”echo”ie:$0gdmysqli”echo”$0pdopdo_mysql”echo”$0-j5gdmbstringmysqlipdopdo_mysqlshmop”echoecho’ifcustom./configureargumentsarenecessary,seedocker-php-ext-configure’echoecho’Possiblevaluesforext-name:’find.\-mindepth2\-maxdepth2\-typef\-name’config.m4’\|xargs-n1dirname\|xargs-n1basename\|sort\|xargsechoecho’SomeoftheabovemodulesarealreadycompiledintoPHP;pleasecheck’echo’theoutputof”php-i”toseewhichmodulesarealreadyloaded.’}opts=”$(getopt-o’h?j:’–long’help,jobs:’–“$@”||{usage>&2&&false;})”evalset–“$opts”j=1whiletrue;doflag=”$1″shiftcase”$flag”in–help|-h|’-?’)usage&&exit0;;–jobs|-j)j=”$1″&&shift;;–)break;;*){echo”error:unknownflag:$flag”usage}>&2exit1;;esacdoneexts=forext;doif[-z”$ext”];thencontinuefiif[!-d”$ext”];thenecho>&2″error:$PWD/$extdoesnotexist”echo>&2usage>&2exit1fiexts=”$exts$ext”doneif[-z”$exts”];thenusage>&2exit1fipm=’unknown’if[-e/lib/apk/db/installed];thenpm=’apk’fiapkDel=if[“$pm”=’apk’];thenif[-n”$PHPIZE_DEPS”];thenifapkinfo–installed.phpize-deps-configure>/dev/null;thenapkDel=’.phpize-deps-configure’elif!apkinfo–installed.phpize-deps>/dev/null;thenapkadd–no-cache–virtual.phpize-deps$PHPIZE_DEPSapkDel=’.phpize-deps’fififipopDir=”$PWD”forextin$exts;docd”$ext”[-eMakefile]||docker-php-ext-configure”$ext”make-j”$j”make-j”$j”installfindmodules\-maxdepth1\-name’*.so’\-execbasename'{}”;’\|xargs-rdocker-php-ext-enablemake-j”$j”cleancd”$popDir”doneif[“$pm”=’apk’]&&[-n”$apkDel”];thenapkdel$apkDelfiif[-e/usr/src/php/.docker-delete-me];thendocker-php-sourcedeletefi
docker-php-source
#!/bin/sh
#移除榜首行的#!之间的空格
set-e
dir=/usr/src/php
usage(){
echo”usage:$0COMMAND”
echo
echo”Managephpsourcetarballlifecycle.”
echo
echo”Commands:”
echo”extractextractphpsourcetarballintodirectory$dirifnotalreadydone.”
echo”deletedeleteextractedphpsourcelocatedinto$dirifnotalreadydone.”
echo
}
case”$1″in
extract)
mkdir-p”$dir”
if[!-f”$dir/.docker-extracted”];then
tar-Jxf/usr/src/php.tar.xz-C”$dir”–strip-components=1
touch”$dir/.docker-extracted”
fi
;;
delete)
rm-rf”$dir”
;;
*)
usage
exit1
;;
esac
创立Dockerfile文件
[root@localhostphp]#viDockerfile
然后复制以下内容
FROMdebian:jessie
#编译和运转时依靠的东西
ENVPHPIZE_DEPSautoconffileg++gcclibc-devmakepkg-configre2c
#指定php.ini目录
ENVPHP_INI_DIR/usr/local/etc/php
##指定PHP版别和编译的选项
ENVPHP_EXTRA_CONFIGURE_ARGS–enable-fpm–with-fpm-user=www-data–with-fpm-group=www-data
ENVPHP_VERSION7.2.6
ENVPHP_FILENAMEphp-7.2.6.tar.xz
#装置依靠的库和环境
RUNapt-getupdate\
&&apt-getinstall-y$PHPIZE_DEPSca-certificatescurllibedit2\
libsqlite3-0libxml2–no-install-recommends\
&&rm-r/var/lib/apt/lists/*
RUNmkdir-p$PHP_INI_DIR/conf.d
RUNset-xe&&buildDeps=”$PHP_EXTRA_BUILD_DEPSlibcurl4-openssl-devlibedit-devlibsqlite3-devlibssl-devlibxml2-devxz-utils”\
&&apt-getupdate&&apt-getinstall-y$buildDeps–no-install-recommends&&rm-rf/var/lib/apt/lists/*\
&&curl-fSL”http://php.net/get/$PHP_FILENAME/from/this/mirror”-o”$PHP_FILENAME”\
&&mkdir-p/usr/src/php\
&&tar-xf”$PHP_FILENAME”-C/usr/src/php–strip-components=1\
&&rm”$PHP_FILENAME”\
&&cd/usr/src/php\
&&./configure–with-config-file-path=”$PHP_INI_DIR”\
–with-config-file-scan-dir=”$PHP_INI_DIR/conf.d”\
$PHP_EXTRA_CONFIGURE_ARGS\
–disable-cgi–enable-mysqlnd–enable-mbstring\
–with-curl–with-libedit–with-openssl–with-zlib\
&&make-j”$(nproc)”\
&&makeinstall\
&&{find/usr/local/bin/usr/local/sbin-typef-executable-execstrip–strip-all'{}’+||true;}\
&&makeclean\
&&apt-getpurge-y–auto-remove-oAPT::AutoRemove::RecommendsImportant=false-oAPT::AutoRemove::SuggestsImportant=false$buildDeps
COPYdocker-php-ext-*/usr/local/bin/
####
WORKDIR/var/www/html
RUNset-ex\
&&cd/usr/local/etc\
&&if[-dphp-fpm.d];then\
sed’s!=NONE/!=!g’php-fpm.conf.default|teephp-fpm.conf>/dev/null;\
cpphp-fpm.d/www.conf.defaultphp-fpm.d/www.conf;\
else\
mkdirphp-fpm.d;\
cpphp-fpm.conf.defaultphp-fpm.d/www.conf;\
{\
echo'[global]’;\
echo’include=etc/php-fpm.d/*.conf’;\
}|teephp-fpm.conf;\
fi\
&&{\
echo'[global]’;\
echo’error_log=/proc/self/fd/2′;\
echo;\
echo'[www]’;\
echo’;ifwesendthisto/proc/self/fd/1,itneverappears’;\
echo’access.log=/proc/self/fd/2′;\
echo;\
echo’clear_env=no’;\
echo;\
echo’;Ensureworkerstdoutandstderraresenttothemainerrorlog.’;\
echo’catch_workers_output=yes’;\
}|teephp-fpm.d/docker.conf\
&&{\
echo'[global]’;\
echo’daemonize=no’;\
echo;\
echo'[www]’;\
echo’listen=[::]:9000′;\
}|teephp-fpm.d/zz-docker.conf
EXPOSE9000
CMD[“php-fpm”]
经过dockerbuild指令创立镜像my-php:7.2.6-fpm
[root@localhostphp]#dockerbuild-tmy-php:7.2.6-fpm.
稍等片刻,指令履行完成后,能够运用dockerimages指令显现刚刚创立的镜像
[root@localhostphp]#dockerimagesmy-php
REPOSITORYTAGIMAGEID…SIZE
运转my-php容器
先在当时目录下创立目录www,然后在目录www中新建文件i.php内容如下
phpinfo();
最终运用下面的指令运转容器
[root@localhostphp]#dockerrun-it-p9000:9000–namephp-fpm-v$pwd/www:/www-v$PWD/conf:/usr/local/etc/php-v$PWD/logs:/phplogsmy-php:7.2.6-fpmbash
这会直接进入容器
参数阐明
-p9000:9000
将容器的9000端口映射到主机的9000端口
–namephp-fpm
将容器命名为php-fpm
-v$pwd/www:/www
将主机中项目的目录www挂载到容器的/www
-v$PWD/conf:/usr/local/etc/php
将主机中当时目录下的conf目录挂载到容器的/usr/local/etc/php
-v$PWD/logs:/phplogs
将主机中当时目录下的logs目录挂载到容器的/phplogs
然后运用下面的指令CD到/www目录,运转php-s0.0.0.0:9000指令
root@bd90d7c7bbf9:/www#cd/www
root@bd90d7c7bbf9:/www#php-S0.0.0.0:9000

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。