如何连接,插入和检索数据库中的数据?

时间:2022-09-25 21:15:39
$con = mysql_connect("localhost","music123_sri","password");
 if (!$con) 
  { die('Could not connect: ' . mysql_error()); } 
mysql_select_db("music123_telugu", $con);
 $sql="INSERT INTO xml (File) VALUES (" . mysql_escape_string($xmlString) . ")"; 

$data = mysql_query("SELECT File FROM xml")

 $info = mysql_fetch_array( $data );

can u help me in coding for connecting to database and inserting data and retrieving data

你可以帮我编码连接数据库和插入数据和检索数据

2 个解决方案

#1


As I wrote in some other answer to one of your questions: please read some introduction on how to use PHP with MySQL. A lot of problems will disappear when you get some sort of understanding on how these pieces fit together.

正如我在其中一个问题的其他答案中所写的那样:请阅读有关如何将PHP与MySQL结合使用的一些介绍。当你对这些部分如何组合起来有一些了解时,很多问题就会消失。

In your concrete example you have three problems:

在您的具体示例中,您有三个问题:

  1. Your INSERT-query is never executed as it's never send to the database server
  2. 您的INSERT查询永远不会执行,因为它永远不会发送到数据库服务器

  3. You only retrieve the first row from the result set of your query using mysql_fetch_array()
  4. 您只能使用mysql_fetch_array()从查询的结果集中检索第一行

  5. There is a ; missing - so your code is not even valid PHP
  6. 有一个;缺少 - 所以你的代码甚至不是有效的PHP

I won't give you a finished code snippet but some advice: carefully read the PHP manual for mysql_query() and mysql_fetch_array() and try to understand how they fit into your code.

我不会给你一个完整的代码片段,但有一些建议:仔细阅读mysql_query()和mysql_fetch_array()的PHP手册,并尝试理解它们如何适合你的代码。

#2


Try adding a semi-colon after $data = mysql_query("SELECT File FROM xml").

尝试在$ data = mysql_query(“SELECT File FROM xml”)之后添加分号。

#1


As I wrote in some other answer to one of your questions: please read some introduction on how to use PHP with MySQL. A lot of problems will disappear when you get some sort of understanding on how these pieces fit together.

正如我在其中一个问题的其他答案中所写的那样:请阅读有关如何将PHP与MySQL结合使用的一些介绍。当你对这些部分如何组合起来有一些了解时,很多问题就会消失。

In your concrete example you have three problems:

在您的具体示例中,您有三个问题:

  1. Your INSERT-query is never executed as it's never send to the database server
  2. 您的INSERT查询永远不会执行,因为它永远不会发送到数据库服务器

  3. You only retrieve the first row from the result set of your query using mysql_fetch_array()
  4. 您只能使用mysql_fetch_array()从查询的结果集中检索第一行

  5. There is a ; missing - so your code is not even valid PHP
  6. 有一个;缺少 - 所以你的代码甚至不是有效的PHP

I won't give you a finished code snippet but some advice: carefully read the PHP manual for mysql_query() and mysql_fetch_array() and try to understand how they fit into your code.

我不会给你一个完整的代码片段,但有一些建议:仔细阅读mysql_query()和mysql_fetch_array()的PHP手册,并尝试理解它们如何适合你的代码。

#2


Try adding a semi-colon after $data = mysql_query("SELECT File FROM xml").

尝试在$ data = mysql_query(“SELECT File FROM xml”)之后添加分号。