如何将多个选定的MySQL列插入到单个PHP数组中

时间:2021-03-14 21:33:45

Hello

I didn't find any solution on my problem with inserting multiple chosen MySQL Table Columns to single PHP array.

我没有找到任何解决我的问题的方法,将多个选定的MySQL表列插入单个PHP数组。

$sql = "SELECT * FROM zaznam,client WHERE zaznam.id_client=client.id_client 
AND zaznam.id_client=1";
$result = mysql_query($sql);

$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
echo json_encode($data);

-Edit: I need "zaznam.date", "zaznam.rozdil" and "client.limit", but not that id_client in WHERE parametr.

-Edit:我需要“zaznam.date”,“zaznam.rozdil”和“client.limit”,但不是WHERE parametr中的id_client。

This code insert into Array all columns of my table. But I want to reach solution, where I can insert into this array only some of the columns.

此代码将Array插入到表的所有列中。但我希望达到解决方案,我只能在这个数组中插入一些列。

I was trying around 3 hours to find any idea around this thing. Maybe I missed something, than if you link me the solution, it would be awesome too.

我试着用了大约3个小时来找到关于这个东西的任何想法。也许我错过了一些东西,比如果你把解决方案联系起来,它也会很棒。

I hope I put every information you need to help me out :)

我希望我把你需要的所有信息都用来帮助我:)

Chosse

2 个解决方案

#1


0  

A quick SQL tutorial..

一个快速的SQL教程..

A table can have as many columns as you want it to have e.g. lets call this table Users

一个表可以包含您希望它拥有的列数,例如让我们调用这个表用户

ID | Name | Email | DateOfBirth

ID |名称|电子邮件|出生日期

you can select ALL records from this table by doing

您可以通过执行此表来选择所有记录

SELECT * FROM Users

SELECT * FROM Users

If you only want 1 column you need to name it

如果您只需要1列,则需要为其命名

SELECT Email FROM Users

从用户选择电子邮件

Lets add some data in

让我们添加一些数据

ID | Name | Email | DateOfBirth

ID |名称|电子邮件|出生日期

1 | Test | test@example.com | 01/01/1990

1 |测试| test@example.com | 01/01/1990

2 | Test2 | test2@example.com | 01/01/1990

2 | Test2 | test2@example.com | 01/01/1990

If we want to only get the record with ID = 2 then we use a WHERE clause

如果我们只想获取ID = 2的记录,那么我们使用WHERE子句

SELECT * FROM Users WHERE ID = 2

SELECT * FROM Users WHERE ID = 2

We can then choose to only select certain columns and only look at the row where ID = 2

然后我们可以选择仅选择某些列,只查看ID = 2的行

SELECT Name, Email FROM Users WHERE ID = 2

SELECT Name,Email FROM Users WHERE ID = 2

Just because I am using ID in the WHERE doesn't affect what I've SELECTED in the select

仅仅因为我在WHERE中使用ID并不会影响我在select中选择的内容

I could go on but really.. go back to the basics and learn SQL from a tutorial

我可以继续但是真的......回到基础并从教程中学习SQL

For your particular query do this (personally, using INNER JOIN instead of , separated joins is better and easier to read - they are exactly the same however in terms of what they do):

对于您的特定查询,请执行此操作(个人而言,使用INNER JOIN代替,分离的联接更好,更容易阅读 - 但就他们所做的而言,它们完全相同):

SELECT zaznam.date, zaznam.rozdil, client.limit 
FROM   zaznam
INNER JOIN client ON zaznam.id_client = client.id_client 
WHERE  zaznam.id_client=1

#2


3  

Instead of selecting all columns (SELECT *), list the columns that you want after SELECT clause.

而不是选择所有列(SELECT *),而是在SELECT子句后列出所需的列。

E.g.:

SELECT zaznam.id_client, zaznam.other_field, client.name, client.something_else 
FROM zaznam,client 
WHERE zaznam.id_client=client.id_client 
    AND zaznam.id_client=1

#1


0  

A quick SQL tutorial..

一个快速的SQL教程..

A table can have as many columns as you want it to have e.g. lets call this table Users

一个表可以包含您希望它拥有的列数,例如让我们调用这个表用户

ID | Name | Email | DateOfBirth

ID |名称|电子邮件|出生日期

you can select ALL records from this table by doing

您可以通过执行此表来选择所有记录

SELECT * FROM Users

SELECT * FROM Users

If you only want 1 column you need to name it

如果您只需要1列,则需要为其命名

SELECT Email FROM Users

从用户选择电子邮件

Lets add some data in

让我们添加一些数据

ID | Name | Email | DateOfBirth

ID |名称|电子邮件|出生日期

1 | Test | test@example.com | 01/01/1990

1 |测试| test@example.com | 01/01/1990

2 | Test2 | test2@example.com | 01/01/1990

2 | Test2 | test2@example.com | 01/01/1990

If we want to only get the record with ID = 2 then we use a WHERE clause

如果我们只想获取ID = 2的记录,那么我们使用WHERE子句

SELECT * FROM Users WHERE ID = 2

SELECT * FROM Users WHERE ID = 2

We can then choose to only select certain columns and only look at the row where ID = 2

然后我们可以选择仅选择某些列,只查看ID = 2的行

SELECT Name, Email FROM Users WHERE ID = 2

SELECT Name,Email FROM Users WHERE ID = 2

Just because I am using ID in the WHERE doesn't affect what I've SELECTED in the select

仅仅因为我在WHERE中使用ID并不会影响我在select中选择的内容

I could go on but really.. go back to the basics and learn SQL from a tutorial

我可以继续但是真的......回到基础并从教程中学习SQL

For your particular query do this (personally, using INNER JOIN instead of , separated joins is better and easier to read - they are exactly the same however in terms of what they do):

对于您的特定查询,请执行此操作(个人而言,使用INNER JOIN代替,分离的联接更好,更容易阅读 - 但就他们所做的而言,它们完全相同):

SELECT zaznam.date, zaznam.rozdil, client.limit 
FROM   zaznam
INNER JOIN client ON zaznam.id_client = client.id_client 
WHERE  zaznam.id_client=1

#2


3  

Instead of selecting all columns (SELECT *), list the columns that you want after SELECT clause.

而不是选择所有列(SELECT *),而是在SELECT子句后列出所需的列。

E.g.:

SELECT zaznam.id_client, zaznam.other_field, client.name, client.something_else 
FROM zaznam,client 
WHERE zaznam.id_client=client.id_client 
    AND zaznam.id_client=1