网页中使用MySQL

来源:互联网 发布:学编程用不用面面俱到 编辑:程序博客网 时间:2024/06/08 19:20

1.在PHP集成环境WampServer下,添加“company”数据库,建立“employee”表,表结构和添加的数据如下:



2.将showdata.asp文件添加在所建立的站点下

showdata.asp文件代码如下:

<?php require_once('Connections/company.php'); ?><?phpif (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {  if (PHP_VERSION < 6) {    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;  }  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);  switch ($theType) {    case "text":      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";      break;        case "long":    case "int":      $theValue = ($theValue != "") ? intval($theValue) : "NULL";      break;    case "double":      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";      break;    case "date":      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";      break;    case "defined":      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;      break;  }  return $theValue;}}mysql_select_db($database_company, $company);$query_Reccompany = "SELECT * FROM employee";$Reccompany = mysql_query($query_Reccompany, $company) or die(mysql_error());$row_Reccompany = mysql_fetch_assoc($Reccompany);$totalRows_Reccompany = mysql_num_rows($Reccompany);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; " /><meta charset="gb2312" /><title>通讯簿</title> <style type="text/css"> * {margin:0;padding:0; font-size:12px;} #main {margin:80px auto; } #main ul {width:1000px;list-style:none;} #main li {border-left:1px solid #ccc;border-top:1px solid #ccc;float:left;width:120px;height:33px;text-align:center;line-height:33px; } #main li.b {border-bottom:1px solid #ccc; } #main li.r {border-right:1px solid #ccc; width:200px; }.ys{ background-color:#CC0000; color:#FFF;}  </style> </head> <body><div id="main" align="center"> <div align="center" style="margin:30px auto; ">  <h3 style="font-size:16px;">科创网络科技有限公司员工信息表</h3></div> <ul> <li class="ys">员工编号</li> <li class="ys">姓名</li> <li class="ys">性别</li> <li class="ys">生日</li><li class="ys">电子邮件</li> <li class="ys">电话</li>  <li class="r ys">地址</li></ul><?php do { ?>  <ul>    <li class="b"><?php echo $row_Reccompany['cmID']; ?></li>    <li class="b"><?php echo $row_Reccompany['cmName']; ?></li>    <li class="b"><?php echo $row_Reccompany['cmSex']; ?></li>    <li class="b"><?php echo $row_Reccompany['cmBirthday']; ?></li>    <li class="b"><?php echo $row_Reccompany['cmEmail']; ?></li>    <li class="b"><?php echo $row_Reccompany['cmPhone']; ?></li>    <li class="b r"><?php echo $row_Reccompany['cmAddress']; ?></li>  </ul>  <?php } while ($row_Reccompany = mysql_fetch_assoc($Reccompany)); ?></div> </body> </html><?phpmysql_free_result($Reccompany);?>
详细过程见视频:点击打开链接

3.然后发现网页预览时,出现中文字符都为?号,如下图所示:

此时,打开company.php,添加上下边两行代码就OK了

mysql_query("set character set 'gb2312'");//读库
mysql_query("set names 'gb2312'");//写库



*如果出现如下图所示的“Deprecated:mysql_pconnect(): The mysql extension is deprecated and will be removed in thefuture”警告信息,可以有三种解决办法。


(1)禁止PHP报错

在PHP设置文件php.ini中将报错取消,在文件中找到display_errors=On,修改为display_errors=Off。

(2)修改连接语句

常用的php语法连接MySQL如下
<?php
$link = mysql_connect('localhost', 'user', 'password');
mysql_select_db('dbname', $link); 
改成mysqi
<?php
$link = mysqli_connect('localhost', 'user', 'password', 'dbname');

常用mysql建表SQL如下
<?php
//  老的
mysql_query('CREATE TEMPORARY TABLE `table`', $link);
// 新的
mysqli_query($link, 'CREATE TEMPORARY TABLE `table`');

(3)设置报警级别

在PHP程序中添加如下代码:

<?phperror_reporting(E_ALL^E_DEPRECATED);


原创粉丝点击