菜单

在RedHat上安装部署Apache+MySQL+PHP环境

2019年11月9日 - 电脑百科
在RedHat上安装部署Apache+MySQL+PHP环境
  1. 安装Apache2
    yum install httpd
    Apache安装到位后,手动运营Apache2
    /etc/init.d/httpd start
    那时候,直接在浏览器中输入服务器的IP地址,就能够以预知到Apache2的暗中同意页面。那时的Apache仅仅能提供HTTP服务,无法试行php、也不能够一而再MySQL数据库。

  2. 安装MySQL
    yum install mysql mysql-server
    姣好后,使用如下命令运行MySQL服务:
    /etc/init.d/mysqld start

  3. 安装PHP5
    yum install php
    设置完php5后,一定要重复开动Apache以使php生效:
    /etc/init.d/httpd restart
    这儿,Apache已经得以剖析推行php脚本了。由于Apache的暗许网址根目录位于:/var/www/html/,由此在这里目录建设构造二个info.php用来测量检验Apache+PHP的不利安装与否:
    echo “<?php phpinfo(); ?>” > /var/www/html/info.php
    然后在浏览器中做客 IP/info.php,现身了PHPINFO的输出消息则代表安装成功

  1.php  连接数据库的身体力行。
  2.php  关闭数据库连接。
  3.php  执行sql语句。
  4.php  获取sql语句实施错误的新闻。
  5.php  获取查询结果集。
  6.php  获取结果集中的有所记录。
  7.php  用php程序展现表中数据。
  8.php  分页突显数据。
  9.html  提交顾客音讯的html页面。
  10.php  数据插入数据库。
  11.php  彰显数据音信的php程序。
  12.php  加多删除链接的php程序。
  13.php  删除数据库数据。

安装php的mysql模块
yum install php-mysql
姣好后再一次重启Apache
/etc/init.d/httpd restart

*/
 // 1.php  连接数据库的亲自过问。

终极还亟需将LAMP组件设置为全自动运维:
chkconfig –levels 2345 httpd on
chkconfig –levels 2345 mysqld on

 代码如下

测试处境(Ali云虚机卡塔尔国:
签到本地的MySQL创设二个表,创制三个顾客,设定该顾客对表的操作权限
use test;
create table login(
uid char(20),
dt int,
text char(50)
);
grant select,insert,update,delete on *.* to alan@”%” identified by
‘password’;

$host = ‘localhost’;
$user_name = ‘root’;
$password = ‘admin’;

 test.php

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die(‘数据库连接失利:<br/>’.mysql_error());
}
echo ‘数据库连接成功!’;

<?php
$host = ‘xxx.xxx.xxx.xxx’;
$user = ‘alan’;
$passwd = ‘password’;
$db = ‘test’;

 

$conn = mysql_connect($host,$user,$passwd);
if (!$conn) {
 die(‘Could not connect: ‘ . mysql_error());
}
echo “select data from MySQL<br \>”;
mysql_select_db($db, $conn);

// 2.php  关闭数据库连接。

$select_sql = “select * from login”;
$result = mysql_query($select_sql);

 代码如下

echo “<table
border=’1′><tr><th>uid</th><th>time</th><th>text</th></tr>”;

$host = ‘localhost’;
$user_name = ‘root’;
$password = ‘admin’;

while ($row = mysql_fetch_array($result)) {
 echo “<tr>”;
 echo “<td>”.$row[‘uid’].”</td>”;
 echo “<td>”.$row[‘dt’].”</td>”;
 echo “<td>”.$row[‘text’].”</td>”;
 echo “</tr>”;
}
echo “</table><br \>”;

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die(‘数据库连接战败:’.mysql_error());
}
echo ‘数据库连接成功!’;

echo “<form action=’insert.php’ method=’post’>
 Name:<input type=’text’ name=’uid’ />
 text:<input type=’text’ name=’text’ />
 <input type=’submit’></form>”;

if(mysql_close($conn))
{
    echo ‘<br/>……..<br/>’;
    echo ‘到数据库的连接已经打响关闭’;
}

mysql_close($conn);
?>

 

insert.php

 

<?php
$host = ‘xxx.xxx.xxx.xxx’;
$user = ‘alan’;
$passwd = ‘password’;
$db = ‘test’;

 
  //3.php  执行sql语句。
 

$conn = mysql_connect($host,$user,$passwd);
if (!$conn) {
 die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db($db, $conn);
$dt = time();
$insert_sql = “insert into login(uid,dt,text) values(‘$_POST[uid]’,
$dt, ‘$_POST[text]’)”;
echo “SQL: “.$insert_sql.”<br \>”;
if (!mysql_query($insert_sql, $conn)) {
 die(‘Error: ‘ . mysql_error());
}
echo “1 record added.<br \>”;

 

mysql_close($conn);

 代码如下

?>

$host = ‘localhost’;
$user_name = ‘root’;
$password = ‘admin’;

  1. 拜望页面

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die(‘数据库连接退步:’.mysql_error());
}
mysql_select_db(‘test’);

图片 1

$sql = ‘select id,name,city from users’;
$result = mysql_query($sql);

  1. 付给表单,推行一条sql插入语句

if($result)
{
    echo ‘sql语句:’ . $sql . ‘<br/>已经打响实施!’;
    $num = mysql_num_rows($result);   
//调用函数mysql_num_row()拿到select语句询问结果的行数
    echo
‘<br/>该sql语句询问到<b>’.$num.'</b>行数据’;
}

图片 2

mysql_close($conn);
 

 

//4.php  获取sql语句施行错误的音讯。

  1. 回去页面刷新查看结果

 代码如下

图片 3

$host = ‘localhost’;
$user_name = ‘root’;
$password = ‘admin’;

 

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die(‘数据库连接退步:’.mysql_error());
}
mysql_select_db(‘test’);

连带陈设与意况
Apache主配置文件:/etc/httpd/conf/httpd.conf
Apache主配置目录,能够将差异门类的安插分类一下归入这么些目录中:/etc/httpd/conf.d/
Apache网址根目录:/var/www/html/
Apache日志文件目录:/var/log/httpd
MySQL的my.cnf配置文件:/etc/my.cnf
MySQL数据库文件地点:/usr/lib/mysql

$sql = ‘select id,name,city,gender from users’;

推荐介绍阅读:

//这里运用mysql_error()获取sql语句实行出错开上下班时间的相干音讯
$result = mysql_query($sql) or die(“<br/>error:
<b>”.mysql_error().”</b><br/><br/><br/>发生难题的sql<br/>”.$sql);

LAMP平台铺排及采取 
http://www.linuxidc.com/Linux/2013-07/87644.htm

if($result)
{
    echo ‘sql语句:’ . $sql . ‘<br/>已经打响执行!’;
}

更多RedHat有关音信见RedHat 专项论题页面 http://www.linuxidc.com/topicnews.aspx?tid=10

mysql_close($conn);

图片 4

 

 

  //5.php  获取查询结果集。

 

 代码如下

$host = ‘localhost’;
$user_name = ‘root’;
$password = ‘admin’;

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die(‘数据库连接失利:’.mysql_error());
}
mysql_select_db(‘test’);

$sql = ‘select id,name,city from users’;

$result = mysql_query($sql) or die(“<br/>error:
<b>”.mysql_error().”</b><br/>发生难点的sql:”.$sql);

if($num = mysql_num_rows($result))
{
    $row = mysql_fetch_array($result);
    echo ‘<pre>’;
    print_r($row);
}

mysql_close($conn);
  //6.php  获取结果集中的持有记录。
$host = ‘localhost’;
$user_name = ‘root’;
$password = ‘admin’;

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die(‘数据库连接失利:’.mysql_error());
}
mysql_select_db(‘test’);

$sql = ‘select id,name,city from users’;

$result = mysql_query($sql) or die(“<br/>error:
<b>”.mysql_error().”</b><br/>发生难题的sql:”.$sql);

if($num = mysql_num_rows($result))
{
    echo ‘<pre>’;
    while($row = mysql_fetch_array($result,mysql_assoc))
    {
        print_r($row);
    }
}

mysql_close($conn);
 

 

 

//7.php  用php程序展现表中数据。

 

 代码如下

$host = ‘localhost’;
$user_name = ‘root’;
$password = ‘admin’;

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die(‘数据库连接战败:’.mysql_error());
}
mysql_select_db(‘test’);

$sql = ‘select id,name,city,created_time from users’;

$result = mysql_query($sql) or die(“<br/>error:
<b>”.mysql_error().”</b><br/>爆发难点的sql:”.$sql);
?>
<html>
<head>
<title>13-7.php</title>
</head>
<center>

<body>
<table width=”75%” border=”0″ cellpadding=”0″ cellspacing=”1″
bgcolor=”#7b7b84″>
    <tr bgcolor=”#8bbcc7″>
        <td height=”33″><div
align=”center”><strong>用户id</strong></div></td>
        <td><div
align=”center”><strong>顾客名称</strong></div></td>
        <td><div
align=”center”><strong>来自城市</strong></div></td>
        <td><div
align=”center”><strong>注册时间</strong></div></td>
    </tr>

<?php
if($num = mysql_num_rows($result))
{
    while($row = mysql_fetch_array($result,mysql_assoc))
    {
?>
    <tr bgcolor=”#ffffff”>
        <td height=”22″ align=”right”><?php echo $row[‘id’];
?> </td>
        <td height=”22″> <?php echo $row[‘name’];
?> </td>
        <td height=”22″> <?php echo $row[‘city’];
?> </td>
        <td height=”22″> <?php echo
$row[‘created_time’]; ?> </td>
    </tr>
<?php
    }
}
mysql_close($conn);
?>

</table>
</body>
</center>
</html>
 

 

 

//8.php  分页呈现数据。

 

 代码如下

$host = ‘localhost’;
$user_name = ‘root’;
$password = ‘admin’;

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die(‘数据库连接失败:’.mysql_error());
}
mysql_select_db(‘test’);

if(isset($_get[‘page’]))     //由get方法获得页面传入当前页数的参数
{
    $page = $_get[‘page’];
}
else
{
    $page = 1;
}
$page_size = 2;              //每页显示两条数据

//获取数据总数
$sql = ‘select * from users’;
$result = mysql_query($sql);
$total = mysql_num_rows($result);

//开第二思忖总页数
if($total)
{
    //假若总量据量小于$page_size,那么唯有朝气蓬勃页
    if($total < $page_size)
        $page_count = 1;
    //若是有余数,则总页数等于总记录数除以页数的结果取整再加1
    if($total % $page_size)
    {
        $page_count = (int)($total/$page_size) + 1;
    }
    //若无余数,则页数等于总的数量据量除以每页数的结果
    else
    {
        $page_count = $total/$page_size;
    }
}
else
{
    $page_count = 0;
}
//翻页链接
$turn_page = ”;
if($page == 1)
{
    $turn_page .= ‘首页 | 上一页 |’;
}
else
{
    $turn_page .= ‘<a href=13-8.php?page=1> 首页</a> |
<a href=13-8.php?page=’.($page-1).’> 上一页 </a> |’;
}
if($page == $page_count || $page_count == 0)
{
    $turn_page .= ‘ 下一页 | 尾页’;
}
else
{
    $turn_page .= ‘<a href=13-8.php?page=’.($page+1).’> 下一页
</a>|<a href=13-8.php?page=’.$page_count.’> 尾页
</a>’;
}

$sql = ‘select id,name,city,created_time from users limit ‘.
($page-1)*$page_size .’, ‘.$page_size;
$result = mysql_query($sql) or die(“<br/>error:
<b>”.mysql_error().”</b><br/>产生难点的sql:”.$sql);
?>
<html>
<head>
<title>13-8.php</title>
</head>
<center>

<body>
<table width=”75%” border=”0″ cellpadding=”0″ cellspacing=”1″
bgcolor=”#7b7b84″>
    <tr bgcolor=”#8bbcc7″>
        <td height=”33″><div
align=”center”><strong>用户id</strong></div></td>
        <td><div
align=”center”><strong>客户名称</strong></div></td>
        <td><div
align=”center”><strong>来自城市</strong></div></td>
        <td><div
align=”center”><strong>注册时间</strong></div></td>
    </tr>

<?php
if($num = mysql_num_rows($result))
{
    while($row = mysql_fetch_array($result,mysql_assoc))
    {
?>
    <tr bgcolor=”#ffffff”>
        <td height=”22″ align=”right”><?php echo $row[‘id’];
?> </td>
        <td height=”22″> <?php echo $row[‘name’];
?> </td>
        <td height=”22″> <?php echo $row[‘city’];
?> </td>
        <td height=”22″> <?php echo
$row[‘created_time’]; ?> </td>
    </tr>
<?php
    }
}
echo $turn_page.'<br/><br/>’;
mysql_close($conn);
?>

</table>
</body>
</center>
</html>
 

 

 

//9.html  提交客商新闻的html页面。
 

 

 代码如下

<html>
<head>
<title>13-9.html</title>
</head>

<body>
<b>提交顾客音信</b>
<form name=”form” method=”post” action=”13-10.php”>
    <table width=”75%” border=”0″ cellpadding=”0″
cellspacing=”2″>
        <tr>
            <td width=”24%” height=”29″>用户名:</td>
            <td width=”76%”><input name=”user_name”
type=”text” id=”user_name” size=”20″></td>
        </tr>
        <tr>
        <td height=”25″>来自城市:</td>
        <td>
     <select name=”city”>
     <option value=”beijing”>北京</option>
     <option value=”london”>伦敦</option>
     <option value=”newyork”>纽约</option>
     <option value=”paris”>巴黎</option>
     <option value=”rome”>罗马</option>
        </select>
  </td>
        </tr>
        <tr>
            <td height=”31″>
            <input type=”submit” name=”submit”
value=”提交”></td>
            <td> </td>
        </tr>
    </table>
</form>
</body>
</html>

  //10.php  数据插入数据库。
  <?php
$host = ‘localhost’;
$user_name = ‘root’;
$password = ‘admin’;

$name = $_post[‘user_name’];
$city = $_post[‘city’];

if(empty($name) || trim($name)==”)
{
    echo ‘请填写客户名!<a href=”13-9.html”>再次回到</a>’;
    exit;
}

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die(‘数据库连接退步:’.mysql_error());
}
mysql_select_db(‘test’);

$sql = “insert into users set id=7,name='” . $name . “‘,city='” . $city
. “‘,created_time=now()”;
mysql_query($sql) or die(“<br/>error:
<b>”.mysql_error().”</b><br/>sql:”.$sql);
mysql_close($conn);

echo ‘数据插入成功,张开<a
href=”13-7.php”>13-7.php</a>查看数据’;
?>

 

 

  //11.php  展现数据音信的php程序。
 

 

 代码如下

<?php
$host = ‘localhost’;
$user_name = ‘root’;
$password = ‘admin’;
$arr_city =
array(‘beijing’=>’北京’,’newyork’=>’纽约’,’paris’=>’巴黎’,’london’=>’伦敦’,’rome’=>’罗马’);

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die(‘数据库连接失利:’.mysql_error());
}
mysql_select_db(‘test’);

if(!isset($_get[‘uid’]))
{
    echo ‘参数错误!’;
    exit;
}
$id = $_get[‘uid’];

$sql = “select * from users where id=$id”;
$result = mysql_query($sql) or die(“<br/>error:
<b>”.mysql_error().”</b><br/>sql:”.$sql);
if(!mysql_num_rows($result))
{
    echo ‘用户id错误!’;
    exit;
}

$row = mysql_fetch_array($result);

$name = $_post[‘user_name’];
$city = $_post[‘city’];
if(!empty($name) || trim($name)!=”)
{
    $sql = “update users set name='” . $name . “‘,city='” . $city . “‘
where id=$id”;
    mysql_query($sql) or die(“<br/>error:
<b>”.mysql_error().”</b><br/>sql:”.$sql);
    mysql_close($conn);

    echo ‘数据改革成功,展开<a
href=”13-7.php”>13-7.php</a>查看数据’;
    exit;
}
?>

<html>
<head>
<title>13-11.php</title>
</head>

<body>
<b>修正客户音信</b>
<form name=”form” method=”post” action=”13-11.php?uid=<?php echo
$id; ?>”>
    <table width=”75%” border=”0″ cellpadding=”0″
cellspacing=”2″>
        <tr>
            <td width=”24%” height=”29″>用户名:</td>
            <td width=”76%”><input name=”user_name”
type=”text” id=”user_name” size=”20″ value=”<?php echo
$row[‘name’]; ?>”></td>
        </tr>
        <tr>
        <td height=”25″>来自城市:</td>
        <td>
        <select name=”city”>
        <?php
        foreach($arr_city as $k=>$v)
        {
            $option = ($row[‘city’] == $k) ? ‘<option
value=”‘.$k.'” selected>’.$v.'</option>’ : ‘<option
value=”‘.$k.'”>’.$v.'</option>’;
            echo $option.’ ‘;
        }   
        ?>
        </select>
        </td>
        </tr>
        <tr>
        <td>注册时间:</td>
        <td><?php echo $row[‘created_time’];
?></td>
        </tr>
        <tr>
            <td height=”31″>
            <input type=”submit” name=”submit”
value=”修改”></td>
            <td> </td>
        </tr>
    </table>
</form>
</body>
</html>
 

 

 

// 12.php  加多删除链接的php程序。
 

 

 代码如下

<?php
$host = ‘localhost’;
$user_name = ‘root’;
$password = ‘admin’;

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die(‘数据库连接战败:’.mysql_error());
}
mysql_select_db(‘test’);

$sql = ‘select id,name,city,created_time from users’;

$result = mysql_query($sql) or die(“<br/>error:
<b>”.mysql_error().”</b><br/>发生难点的sql:”.$sql);
?>
<html>
<head>
<title>13-12.php</title>
<script language=”网页特效”>

</script>
</head>
<center>

<body>
<table width=”75%” border=”0″ cellpadding=”0″ cellspacing=”1″
bgcolor=”#7b7b84″>
    <tr bgcolor=”#8bbcc7″>
        <td height=”33″><div
align=”center”><strong>用户id</strong></div></td>
        <td><div
align=”center”><strong>客户名称</strong></div></td>
        <td><div
align=”center”><strong>来自城市</strong></div></td>
        <td><div
align=”center”><strong>注册时间</strong></div></td>
        <td><div
align=”center”><strong>操作</strong></div></td>
    </tr>

<?php
if($num = mysql_num_rows($result))
{
    while($row = mysql_fetch_array($result,mysql_assoc))
    {
?>
    <tr bgcolor=”#ffffff”>
        <td height=”22″ align=”right”><?php echo $row[‘id’];
?> </td>
        <td height=”22″> <?php echo $row[‘name’];
?> </td>
        <td height=”22″> <?php echo $row[‘city’];
?> </td>
        <td height=”22″> <?php echo
$row[‘created_time’]; ?> </td>
        <td height=”22″> <a
onclick=”javascript:if(confirm(‘分明要删减顾客消息吗?’)) return true;
else return false;” href=”13-13.php?id=<?php echo $row[‘id’];
?>”>删除</a> </td>
    </tr>
<?php
    }
}
mysql_close($conn);
?>

</table>
</body>
</center>
</html>
 

/

 

/13.php  删除数据库数据。
 

 

 代码如下

<?php
if(!isset($_get[‘id’]))
{
    echo ‘参数错误!’;
    exit;
}

$id = $_get[‘id’];
if(empty($id))
{
    echo ‘客户id无法为空!’;
    exit;
}

$host = ‘localhost’;
$user_name = ‘root’;
$password = ‘admin’;

$conn = mysql_connect($host,$user_name,$password);
if(!$conn)
{
    die(‘数据库连接战败:’.mysql_error());
}
mysql_select_db(‘test’);

//先判定是还是不是存在该id的客商
$sql = “select * from users where id=$id”;
$result = mysql_query($sql) or die(“<br/>error:
<b>”.mysql_error().”</b><br/>sql:”.$sql);
if(!mysql_num_rows($result))
{
    echo ‘用户id错误!’;
    exit;
}

//删除客户数据
$sql = “delete from users where id=$id”;
mysql_query($sql) or die(“<br/>error:
<b>”.mysql_error().”</b><br/>sql:”.$sql);
mysql_close($conn);

echo ‘数据删除成功,再次来到<a
href=”13-12.php”>13-12.php</a>查看数据’;
?>

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图