Reputation:
How can i get values from mysql database usin while loop?
Sry for my bad english and i'm new in vb.net so please do not insult me because I do not know vb.net, I'm trying to learn, i'll try to explain what i need. :)
Lets go:
We know that EmailAddress
is real email address, now first query must be something like this
Query1 = "select ID from database.table_users where email='" & EmailAddress & "'"
Now we know that Query1
will return value id from table_users
.
Now we need create new query like this
Query2 = "select friend_id from database.table_friends where user_id='" & query1 & "'"
Okay, now Query2
return friend_id
from table_friends
.
One more query like this
Query3 = "select displayname from database.table_users where id='" & Query2 & "'"
Query3
must return displaynames
of all users which we got in query2
.
Example:
First i need query ID from table_users where email=EmailAddres
Then i need query friend_id from table_friends where user_id=ID
After that i nedd query displayname from table_users where id=friend_id
*these are just examples and they are not correct as you can see :D
I need 3 querys in while loop, and only last query (displayname) will go to the listbox. While must return all mysql values from table_friends where ID= id of EmailAddress user.
Upvotes: 0
Views: 144
Reputation: 77846
why loop and why can't you make it single query like below using JOIN
select tf.displayname from table_users tu
join table_friends tf on tu.id = tf.friend_id
and tu.email = '" & EmailAddress & "';
Upvotes: 0
Reputation: 218798
Join your tables into a single query. Something like this:
SELECT
Friends.displayname
FROM
table_users AS Users
INNER JOIN table_friends AS Connections
ON Users.user_id = Connections.user_id
INNER JOIN table_users AS Friends
ON Connections.friend_id = Friends.user_id
WHERE
Users.email = @EmailAddress
This creates two references to the table_users
table, one for the initial user and one for the friends, connected by your many-to-many linking table. (Note also the use of a query parameter, which I highly recommend instead of directly concatenating input into the query.)
SQL is very powerful for querying structured data (hence its name), you'll be much better off building your query logic into SQL rather than keeping the SQL queries overly simple and making multiple trips to the database.
Upvotes: 1