Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从一个表中获取值,将其插入到另一个表的查询中。如何?_Php_Mysql - Fatal编程技术网

Php 从一个表中获取值,将其插入到另一个表的查询中。如何?

Php 从一个表中获取值,将其插入到另一个表的查询中。如何?,php,mysql,Php,Mysql,这是一个有点复杂,不能做的innerjoin或类似的东西。我已经得到了帮助,但我想不出来。下面是我正在做的:我需要从一个表中获取一行文本,并在获取结果时将其插入到另一个表的查询中。在一个数据库中,我保存了('2'、'3'、'4'),它们是指向主表ID的链接。因此,为了获得它们,我必须首先将它们从表中取出,插入php,然后将相关行取出并保存到xml中。以下是重要部分: $name = ($_GET["name"]); $query = "SELECT Favorites FROM $table_

这是一个有点复杂,不能做的innerjoin或类似的东西。我已经得到了帮助,但我想不出来。下面是我正在做的:我需要从一个表中获取一行文本,并在获取结果时将其插入到另一个表的查询中。在一个数据库中,我保存了
('2'、'3'、'4')
,它们是指向主表ID的链接。因此,为了获得它们,我必须首先将它们从表中取出,插入php,然后将相关行取出并保存到xml中。以下是重要部分:

$name = ($_GET["name"]);
$query =  "SELECT Favorites FROM $table_id2 WHERE Name = $name";

while($favoritesstring = mysql_fetch_assoc(mysql_query($query, $dbconnect))){
$favoritestringresult = $favoritesstring['Favorites'];
} 

$string = stripslashes($favoritestringresult['Favorites']);

$query = "SELECT Name,Comment,ID FROM $table_id WHERE ID in " .$string;

$dbresult = mysql_query($query, $dbconnect) or die(mysql_error() . ' Query: ' . $query);

通过查看您的两个查询,可以如下所示:

“选择t1.ID、t1.Name、t1.Comment FROM$table\u ID t1 LEFT JOIN$table\u id2 t2 ON t1.ID=t2.Favorites,其中t2.Name=$Name”


我在这里加入左键,因为我实际上不知道
$table\u id
$table\u id 2

之间的关系。通过查看您的两个查询,可以这样做:

“选择t1.ID、t1.Name、t1.Comment FROM$table\u ID t1 LEFT JOIN$table\u id2 t2 ON t1.ID=t2.Favorites,其中t2.Name=$Name”


我之所以在这里加入
是因为我不知道
$table\u id
$table\u id2

有很多可能的解决方案。一种是使用子查询:

SELECT  name, comment, id
FROM    table_id
WHERE   ID in   (
                    SELECT  Favorites
                    FROM    table_id2
                    WHERE   Name = $name
                )
另一个更有效的方法是使用
内部联接

SELECT a.ID, a.Name, a.Comment 
FROM    table_id a 
            INNER JOIN JOIN table_id2 b 
                ON a.ID = b.Favorites 
WHERE b.Name = $name

有许多可能的解决办法。一种是使用子查询:

SELECT  name, comment, id
FROM    table_id
WHERE   ID in   (
                    SELECT  Favorites
                    FROM    table_id2
                    WHERE   Name = $name
                )
另一个更有效的方法是使用
内部联接

SELECT a.ID, a.Name, a.Comment 
FROM    table_id a 
            INNER JOIN JOIN table_id2 b 
                ON a.ID = b.Favorites 
WHERE b.Name = $name
警告:


请不要使用
mysql.*
函数来编写新代码。它们不再得到维护,社区已开始恢复。看到了吗

相反,您应该了解并使用或。应该给出一些关于决定使用哪个API的细节。对于PDO,这里有一个例子

考虑以下数据库设置:

-- This contains the elements: books, videos, albums.
CREATE TABLE IF NOT EXISTS `entries` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) NOT NULL,
    `comment` varchar(255) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB;

-- This contains what our users favourited.
-- row_id: row identifier index
-- name: the name of the user to search for
-- entry_id: the id of the entry we search for, this will
--           make our JOIN relation towards entries.id
CREATE TABLE IF NOT EXISTS `favorites` (
    `row_id` int(10) NOT NULL AUTO_INCREMENT,
    `name` varchar(64) NOT NULL,
    `entry_id` int(10) NOT NULL,
    PRIMARY KEY (`row_id`)
) ENGINE=InnoDB;
当然,输入一些任意数据:

INSERT INTO `entries` (`id`, `name`, `comment`) VALUES
    (1, 'Foobar', 'Bazqux'),
    (2, 'Barbaz', 'Quxfoo');

INSERT INTO `favorites` (`row_id`, `name`, `entry_id`) VALUES
    (1, 'someguy', 1),
    (2, 'someguy', 2),
    (3, 'person', 2);
我们需要一个基本脚本来查询数据库:

mysql_connect("localhost", "username", "password");
mysql_select_db("database");

$result = mysql_query('SELECT entries.id, entries.name, entries.comment
    FROM entries
    INNER JOIN favorites ON entries.id = favorites.entry_id
    WHERE favorites.name = "' .mysql_real_escape_string($_GET['name']). '"');

while ( $row = mysql_fetch_assoc($result) )
    var_dump($row);

mysql_close();
如果我们给
$\u GET['name']
someguy
的值,我们的结果将是:

array (
  'id' => '1',
  'name' => 'Foobar',
  'comment' => 'Bazqux',
)
array (
  'id' => '2',
  'name' => 'Barbaz',
  'comment' => 'Quxfoo',
)
当值为
person
时,我们只会得到:

array (
  'id' => '2',
  'name' => 'Barbaz',
  'comment' => 'Quxfoo',
)
诀窍在于
内部联接
。我们的搜索模式是
WHERE
子句,它告诉数据库我们搜索
收藏夹。entry\u id
其中
收藏夹。name
是输入。从这里开始,
JOIN
在扩展搜索的两个表之间建立链接
internaljoin
表示只返回
条目.id
等于
收藏夹.entry\u id
的行。当然,我们获取
SELECT
语句中列出的字段。毕竟,这是我们告诉数据库要做的

从这里开始,在
while(){}
构造中,可以对检索到的数据执行任何操作

与获取所有ID然后将其插入(value1,value2)语句中的
字段不同,
internal-JOIN
利用了MySQL的关系结构,从而提高了性能和可维护性

正如我在与另一个人讨论类似问题时所说:

有时,你可以用MySQL建立起来的复杂性是无法想象的。底线是,首先需要确保数据库建模正确。之后,问题就在于如何将一个字段链接到另一个字段。phpMyAdmin、libreofficebase或microsoftaccess等工具对于图形界面建模键之间的关系非常有帮助

警告:


请不要使用
mysql.*
函数来编写新代码。它们不再得到维护,社区已开始恢复。看到了吗

相反,您应该了解并使用或。应该给出一些关于决定使用哪个API的细节。对于PDO,这里有一个例子

考虑以下数据库设置:

-- This contains the elements: books, videos, albums.
CREATE TABLE IF NOT EXISTS `entries` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) NOT NULL,
    `comment` varchar(255) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB;

-- This contains what our users favourited.
-- row_id: row identifier index
-- name: the name of the user to search for
-- entry_id: the id of the entry we search for, this will
--           make our JOIN relation towards entries.id
CREATE TABLE IF NOT EXISTS `favorites` (
    `row_id` int(10) NOT NULL AUTO_INCREMENT,
    `name` varchar(64) NOT NULL,
    `entry_id` int(10) NOT NULL,
    PRIMARY KEY (`row_id`)
) ENGINE=InnoDB;
当然,输入一些任意数据:

INSERT INTO `entries` (`id`, `name`, `comment`) VALUES
    (1, 'Foobar', 'Bazqux'),
    (2, 'Barbaz', 'Quxfoo');

INSERT INTO `favorites` (`row_id`, `name`, `entry_id`) VALUES
    (1, 'someguy', 1),
    (2, 'someguy', 2),
    (3, 'person', 2);
我们需要一个基本脚本来查询数据库:

mysql_connect("localhost", "username", "password");
mysql_select_db("database");

$result = mysql_query('SELECT entries.id, entries.name, entries.comment
    FROM entries
    INNER JOIN favorites ON entries.id = favorites.entry_id
    WHERE favorites.name = "' .mysql_real_escape_string($_GET['name']). '"');

while ( $row = mysql_fetch_assoc($result) )
    var_dump($row);

mysql_close();
如果我们给
$\u GET['name']
someguy
的值,我们的结果将是:

array (
  'id' => '1',
  'name' => 'Foobar',
  'comment' => 'Bazqux',
)
array (
  'id' => '2',
  'name' => 'Barbaz',
  'comment' => 'Quxfoo',
)
当值为
person
时,我们只会得到:

array (
  'id' => '2',
  'name' => 'Barbaz',
  'comment' => 'Quxfoo',
)
诀窍在于
内部联接
。我们的搜索模式是
WHERE
子句,它告诉数据库我们搜索
收藏夹。entry\u id
其中
收藏夹。name
是输入。从这里开始,
JOIN
在扩展搜索的两个表之间建立链接
internaljoin
表示只返回
条目.id
等于
收藏夹.entry\u id
的行。当然,我们获取
SELECT
语句中列出的字段。毕竟,这是我们告诉数据库要做的

从这里开始,在
while(){}
构造中,可以对检索到的数据执行任何操作

与获取所有ID然后将其插入(value1,value2)语句中的
字段不同,
internal-JOIN
利用了MySQL的关系结构,从而提高了性能和可维护性

正如我在与另一个人讨论类似问题时所说:

有时,你可以用MySQL建立起来的复杂性是无法想象的。底线是,首先需要确保数据库建模正确。之后,问题就在于如何将一个字段链接到另一个字段。phpMyAdmin、libreofficebase或microsoftaccess等工具对于图形界面建模键之间的关系非常有帮助


这是一种切线,但您永远不应该只接受未初始化的用户输入(就像GET一样)并在查询中使用它。请看