用户行为分析:从PHP、Java到C++的探索与实践
PHP用户行为分析
1、使用PHP连接MySQL数据库,创建一个表格用于存储用户访问数据:
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } ?>
2、编写SQL语句,插入一条用户访问记录:
<?php $sql = "INSERT INTO user_behavior (user_id, visit_time, page_url) VALUES ('user1', NOW(), 'http://www.example.com/page1')"; if ($conn->query($sql) === TRUE) { echo "记录插入成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
3、查询数据库中的用户访问记录,进行数据分析:
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } $sql = "SELECT user_id, visit_time, page_url FROM user_behavior ORDER BY visit_time DESC"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 输出数据表内容 while($row = $result->fetch_assoc()) { echo "id: " . $row["user_id"]. " - Time: " . $row["visit_time"]. " - URL: " . $row["page_url"]. "<br>"; } } else { echo "0 结果"; } $conn->close(); ?>
还没有评论,来说两句吧...