在php中创建 数据表代码

 更新时间:2016年11月25日 16:39  点击:1587
 代码如下 复制代码
  $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>";

这是一篇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>";

  }?>

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

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

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

[!--infotagslink--]

相关文章

  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
  • js识别uc浏览器的代码

    其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
  • JavaScript动态创建div属性和样式示例代码

    1.创建div元素: Javascript代码 复制代码 代码如下: <scripttypescripttype="text/javascript"> functioncreateElement(){ varcreateDiv=document.createElement("div"); createDiv.innerHTML="Testcreateadiveleme...2013-10-13
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • JS创建Tag标签的方法详解

    这篇文章主要介绍了JS创建Tag标签的方法,结合具体实例形式分析了javascript动态操作页面HTML元素实现tag标签功能的步骤与相关操作技巧,需要的朋友可以参考下...2017-06-15
  • php怎么用拼音 简单的php中文转拼音的实现代码

    小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
  • php导出csv格式数据并将数字转换成文本的思路以及代码分享

    php导出csv格式数据实现:先定义一个字符串 存储内容,例如 $exportdata = '规则111,规则222,审222,规222,服2222,规则1,规则2,规则3,匹配字符,设置时间,有效期'."/n";然后对需要保存csv的数组进行foreach循环,例如复制代...2014-06-07
  • ecshop商品无限级分类代码

    ecshop商品无限级分类代码 function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset($cat_options[$spec_cat_id]))...2016-11-25
  • 几种延迟加载JS代码的方法加快网页的访问速度

    本文介绍了如何延迟javascript代码的加载,加快网页的访问速度。 当一个网站有很多js代码要加载,js代码放置的位置在一定程度上将会影像网页的加载速度,为了让我们的网页加载速度更快,本文总结了一下几个注意点...2013-10-13
  • vue项目,代码提交至码云,iconfont的用法说明

    这篇文章主要介绍了vue项目,代码提交至码云,iconfont的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-30