通过PHP实现MySQL表中数据插入的SQL操作

版权申诉
0 下载量 58 浏览量 更新于2024-10-24 收藏 1KB RAR 举报
资源摘要信息:"本资源主要介绍了如何通过PHP函数mysql_query执行SQL INSERT语句,将数据输入MySQL数据库表格中。本资源包含四个PHP示例文件:insert.php、phphtml.php、forminput.php和postdemo.php,详细演示了数据录入过程。" 一、PHP与MySQL数据库交互概述 PHP(超文本预处理器)是一种广泛使用的开源服务器端脚本语言,它用于创建动态网页内容、生成网站和移动应用。MySQL是一个流行的开源关系型数据库管理系统(RDBMS),它用于存储、检索和管理大量数据。PHP与MySQL的结合可以实现动态网站的数据交互功能。 在PHP中,向MySQL数据库插入数据通常通过SQL的INSERT语句来完成。PHP提供了一系列的数据库访问函数,其中mysql_query是用于执行SQL语句的一个函数。通过这个函数,可以向数据库提交数据并执行数据插入操作。 二、使用mysql_query函数执行SQL INSERT语句 mysql_query函数是PHP中用于发送一条SQL语句到MySQL数据库的函数。它既可以用于查询操作,也可以用于数据的插入、更新和删除等操作。当使用mysql_query执行SQL INSERT语句时,PHP脚本向MySQL服务器提交新的数据记录。 一个典型的SQL INSERT语句的基本格式如下: ```sql INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); ``` 在这个语句中,`table_name` 表示要插入数据的表名,`column1, column2, column3, ...` 表示表中的列名,`value1, value2, value3, ...` 表示对应列的数据值。 在PHP中,可以这样使用mysql_query函数: ```php <?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语句 $sql = "INSERT INTO MyTable (column1, column2) VALUES ('value1', 'value2')"; // 执行SQL语句并检查是否成功 if (mysql_query($sql, $conn)) { echo "新记录插入成功"; } else { echo "Error: " . mysql_error(); } $conn->close(); ?> ``` 注意:在上面的代码中,我们使用了`mysqli`,而不是已废弃的`mysql_*`函数。`mysqli`是`mysql_*`的改进版本,提供了面向对象和过程两种API,并支持预处理语句等高级功能。 三、PHP文件说明 1. insert.php 此文件可能展示了如何创建一个表单,用户可以在这个表单中输入数据,并通过PHP脚本将这些数据插入到MySQL数据库中。该文件可能还包含了HTML表单的创建代码以及处理表单提交的PHP代码。 2. phphtml.php 此文件可能包含了将PHP代码嵌入到HTML页面中的示例。通过使用PHP和HTML混合的方式,可以在网页上直接展示数据库查询结果或执行数据库操作。 3. forminput.php 此文件很可能演示了如何使用HTML表单来获取用户输入,并利用PHP脚本处理这些输入数据。这个文件中可能会包含表单创建以及通过POST方法接收数据的相关代码。 4. postdemo.php 此文件可能演示了使用POST方法接收数据并处理这些数据的示例。它可能展示了如何通过PHP处理表单提交的数据,并且可能还包含了如何对这些数据进行验证以及将验证通过的数据插入到MySQL数据库中的操作。 总结,本资源通过一系列PHP文件实例展示了如何使用PHP和MySQL实现动态网页数据交互的基本方法。通过这些文件,可以学习到数据提交、验证和存储到数据库的整个过程。开发者可以通过这些示例进一步深入学习PHP编程和MySQL数据库管理。

[root@zhaosai conf]# sqoop import --connect jdbc:mysql://zhaosai:3306/mydb --username root --password jqe6b6 --table news --target-dir /user/news --fields-terminated-by “;” --hive-import --hive-table news -m 1 Warning: /opt/programs/sqoop-1.4.7.bin__hadoop-2.6.0/../hbase does not exist! HBase imports will fail. Please set $HBASE_HOME to the root of your HBase installation. Warning: /opt/programs/sqoop-1.4.7.bin__hadoop-2.6.0/../hcatalog does not exist! HCatalog jobs will fail. Please set $HCAT_HOME to the root of your HCatalog installation. Warning: /opt/programs/sqoop-1.4.7.bin__hadoop-2.6.0/../accumulo does not exist! Accumulo imports will fail. Please set $ACCUMULO_HOME to the root of your Accumulo installation. Warning: /opt/programs/sqoop-1.4.7.bin__hadoop-2.6.0/../zookeeper does not exist! Accumulo imports will fail. Please set $ZOOKEEPER_HOME to the root of your Zookeeper installation. 23/06/10 16:07:14 INFO sqoop.Sqoop: Running Sqoop version: 1.4.7 23/06/10 16:07:15 WARN tool.BaseSqoopTool: Setting your password on the command-line is insecure. Consider using -P instead. 23/06/10 16:07:15 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset. 23/06/10 16:07:15 INFO tool.CodeGenTool: Beginning code generation 23/06/10 16:07:15 ERROR sqoop.Sqoop: Got exception running Sqoop: java.lang.RuntimeException: Could not load db driver class: com.mysql.jdbc.Driver java.lang.RuntimeException: Could not load db driver class: com.mysql.jdbc.Driver at org.apache.sqoop.manager.SqlManager.makeConnection(SqlManager.java:875) at org.apache.sqoop.manager.GenericJdbcManager.getConnection(GenericJdbcManager.java:59) at org.apache.sqoop.manager.SqlManager.execute(SqlManager.java:763) at org.apache.sqoop.manager.SqlManager.execute(SqlManager.java:786) at org.apache.sqoop.manager.SqlManager.getColumnInfoForRawQuery(SqlManager.java:289) at org.apache.sqoop.manager.SqlManager.getColumnTypesForRawQuery(SqlManager.java:260) at org.apache.sqoop.manager.SqlManager.getColumnTypes(SqlManager.java:246) at org.apache.sqoop.manager.ConnManager.getColumnTypes(ConnManager.java:327) at org.apache.sqoop.orm.ClassWriter.getColumnTypes(ClassWriter.java:1872) at org.apache.sqoop.orm.ClassWriter.generate(ClassWriter.java:1671) at org.apache.sqoop.tool.CodeGenTool.generateORM(CodeGenTool.java:106) at org.apache.sqoop.tool.ImportTool.importTable(ImportTool.java:501) at org.apache.sqoop.tool.ImportTool.run(ImportTool.java:628) at org.apache.sqoop.Sqoop.run(Sqoop.java:147) at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70) at org.apache.sqoop.Sqoop.runSqoop(Sqoop.java:183) at org.apache.sqoop.Sqoop.runTool(Sqoop.java:234) at org.apache.sqoop.Sqoop.runTool(Sqoop.java:243) at org.apache.sqoop.Sqoop.main(Sqoop.java:252)

2023-06-11 上传

sqoop import --connect jdbc:mysql://zhaosai:3306/mydb --username root --password jqe6b6 --table news --target-dir /user/news --fields-terminated-by “;” --hive-import --hive-table news -m 1出现错误Warning: /opt/programs/sqoop-1.4.7.bin__hadoop-2.6.0/../hbase does not exist! HBase imports will fail. Please set $HBASE_HOME to the root of your HBase installation. Warning: /opt/programs/sqoop-1.4.7.bin__hadoop-2.6.0/../hcatalog does not exist! HCatalog jobs will fail. Please set $HCAT_HOME to the root of your HCatalog installation. Warning: /opt/programs/sqoop-1.4.7.bin__hadoop-2.6.0/../accumulo does not exist! Accumulo imports will fail. Please set $ACCUMULO_HOME to the root of your Accumulo installation. Warning: /opt/programs/sqoop-1.4.7.bin__hadoop-2.6.0/../zookeeper does not exist! Accumulo imports will fail. Please set $ZOOKEEPER_HOME to the root of your Zookeeper installation. 23/06/10 16:18:23 INFO sqoop.Sqoop: Running Sqoop version: 1.4.7 23/06/10 16:18:23 WARN tool.BaseSqoopTool: Setting your password on the command-line is insecure. Consider using -P instead. 23/06/10 16:18:23 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset. 23/06/10 16:18:23 INFO tool.CodeGenTool: Beginning code generation Sat Jun 10 16:18:23 CST 2023 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 23/06/10 16:18:24 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM news AS t LIMIT 1 23/06/10 16:18:24 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM news AS t LIMIT 1 23/06/10 16:18:24 INFO orm.CompilationManager: HADOOP_MAPRED_HOME is /opt/programs/hadoop-2.7.6 注: /tmp/sqoop-root/compile/84ba419f00fa83cb5d16dba722729d01/news.java使用或覆盖了已过时的 API。 注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。 23/06/10 16:18:25 INFO orm.CompilationManager: Writing jar file: /tmp/sqoop-root/compile/84ba419f00fa83cb5d16dba722729d01/news.jar 23/06/10 16:18:25 WARN manager.MySQLManager: It looks like you are importing from mysql. 23/06/10 16:18:25 WARN manager.MySQLManager: This transfer can be faster! Use the --direct 23/06/10 16:18:25 WARN manager.MySQLManager: option to exercise a MySQL-specific fast path. 23/06/10 16:18:25 INFO manager.MySQLManager: Setting zero DATETIME behavior to convertToNull (mysql) 23/06/10 16:18:25 ERROR tool.ImportTool: Import failed: No primary key could be found for table news. Please specify one with --split-by or perform a sequential import with '-m 1'.

2023-06-11 上传