1

Hello

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

$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.

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.

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.

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

Chosse

5
  • You can specify the columns you want instead of using *. Commented Aug 10, 2017 at 10:56
  • select the columns you want in the sql not * (which is all)? Commented Aug 10, 2017 at 10:56
  • add required column name on your select query. it will optimized and you get rid from unnecessary columns on your array. Commented Aug 10, 2017 at 10:57
  • I need 3 specific columns, but I need to name the parametrs of WHERE too. So I need asomething like delete the WHERE parametrs before inserting the columns into array. I need "zaznam.date", "zaznam.rozdil" and "client.limit", but not that id_client in WHERE parametr Commented Aug 10, 2017 at 10:58
  • Delete where parameters before inserting into array I am lost with this sentence.. Are you saying you want further clauses? AND col2 = 'this' AND col3 = 'that'? Commented Aug 10, 2017 at 11:00

2 Answers 2

3

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

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
Sign up to request clarification or add additional context in comments.

6 Comments

This will select 4 columns with "id_client" as parametr of WHERE clause. But I need 3 specific columns without the "id_client" one, which I put into PHP array.
@ChosseMartiny just don't select it. Have you even googled about sql and learnt it? Pretty much step 1 is learning a select statement SELECT zaznam.date, zaznam.rozdil, client.limit FROM ...
I learnt, that in SELECT clause have to be named even columns of WHERE clause. E.g.: "SELECT date FROM zaznam WHERE id_client=1" wont work.
Think you need to go back and have another look. If you don't add id_client to the SELECT list, it won't be returned whether you have it in the WHERE clause or not. The SELECT list does not need to list all or any of the columns that are in the WHERE clause.
OK, I'll try ^^
|
0

A quick SQL tutorial..

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

ID | Name | Email | DateOfBirth

you can select ALL records from this table by doing

SELECT * FROM Users

If you only want 1 column you need to name it

SELECT Email FROM Users

Lets add some data in

ID | Name | Email | DateOfBirth

1 | Test | [email protected] | 01/01/1990

2 | Test2 | [email protected] | 01/01/1990

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

SELECT * FROM Users WHERE ID = 2

We can then choose to only select certain columns and only look at the row 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

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

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):

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

1 Comment

Oh, Thank you <3 School sucks then :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.