472,145 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

How to fetch a specific row in a mysql database?

Hai to all,
this is senthil...
i'm now working in the field of VC++...
i want to connect a mysql database...

just now tried to retrieve rows from a simple
mysql database by using VC++

My database is like this...

---------------
Name roll no
--------------
asd 101
dff 102
sdf 103
fdf 104
dfd 105


i have used the following codings for connecting the database..
And the output also comes clearly.. No probs for the output..
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h> 
  2. #include <mysql.h> 
  3. #include <conio.h>
  4. #include <windows.h>
  5. #include<string.h>
  6.  
  7.  
  8. #define host "192.168.0.2"
  9. #define username "root" 
  10. #define password "" 
  11. #define database "senthil"
  12.  
  13. void main()
  14. {
  15.  
  16. MYSQL *conn; 
  17. conn = mysql_init(NULL);
  18.  
  19. mysql_real_connect(conn,host,username,password,database,0,NULL,0); 
  20. MYSQL_RES *res_set; 
  21. MYSQL_ROW row;
  22.  
  23. unsigned int i=0; 
  24. mysql_query(conn,"SELECT * FROM sen"); 
  25. res_set = mysql_store_result(conn); 
  26. unsigned int numrows = mysql_num_rows(res_set);
  27.  
  28. while ((row = mysql_fetch_row(res_set))!= NULL)
  29. {
  30. i=0;
  31. printf("%s\t", row[i]!=NULL? row[i]:"NULL");
  32. i++;
  33. printf("%s\n", row[i]!=NULL? row[i]:"NULL");
  34. }
  35.  
  36. mysql_close(conn);       
  37. getch();
  38.  }
  39.  
  40.  
Here row[0]= asd,dff,...
row[1]= 101,102,....

Here MYSQL_ROW row; -return data as array of strings
I also used the following codings for fetching a single field from the database by using
the following codings....

For example if i want to fetch 'sdf' field from the database
i gave the condition like this in select statement...
mysql_query(conn,"SELECT * FROM sen where Name='sdf'");

and here also the output comes clearly..

But i dont know how to fetch from a specific row...

for example...

i dont want the first two rows.... and if i want to fetch from
the third row only! what condition shall i give for this?

Very important point is that,
here MYSQL_ROW row; -return data as array of strings,
ie rows can only be returned as strings...
ie the first row will be return as a string..(asd,dff,....)
and the second row wil be returned as another string...(101,102)..


But i want to split that row....

How can i do that?

plzz explain me, this concept is going to be used for my project....

itz very important for me....

Can any one plzz reply me...

thanks...
senthil
Dec 20 '06 #1
7 16580
ronverdonk
4,258 Expert 4TB
Please read the Posting Guidelines before you post in this forum!

You can get the result's exact row by using command mysql_data_seek().
Expand|Select|Wrap|Line Numbers
  1. $res=mysql_query("SELECT name from table");
  2. mysql_data_seek($res, 2);
  3. $row=mysql_fetch_assoc($res));
  4.  
sets the pointer to the third row and fetches that row.

Getting a specific field from a specific row use mysql_result()
Expand|Select|Wrap|Line Numbers
  1. $res=mysql_query("SELECT name from table");
  2. mysql_result($res, 2);
gets you the content of column 'name' from the third row.

Caveat: you must check if these functions are available in VC++!

Ronald :cool:
Dec 20 '06 #2
Hai ronald,
thanks for ur reply...
ur conditions worked supervly in VC++,

i fetched the specific row in a mysql database..

Now i want to fetch only the first three rows and dont want the
next rows in the above database...

What condition shall i give for this...

i'm beginner for this type of works..

so plzz explain me...
Dec 21 '06 #3
Hai ronald,

i tried the following two conditions for fetching the fetching the
first 3 rows but nothing happened...

condition 1:
mysql_data_seek(res_set, -2);

condition 2:
mysql_query(conn,"SELECT * FROM sen fetch first 3 rows only");


will u plzz explain me how to fecth the first 3 rows...

thanks and regards..
senthil
Dec 21 '06 #4
Hai ronald,

i found the condition for fetching first 3 rows..
itz very simple..


mysql_query(conn,"SELECT * FROM sen LIMIT 0,3");

this is the condition...

Any way i cant do it without ur help....

thanks for ur support and expecting ur support in the future...

bye
senthil
Dec 21 '06 #5
ronverdonk
4,258 Expert 4TB
Glad you found the solution yourself, before I could answer that. Happy to have helped you out.

Ronald :cool:
Dec 21 '06 #6
Hai ronald,

i'm working in the field of
creating Adobe Indesign plugin's using VC++..

R u having knowledge in that?

Or any of ur friends working in that field?

i'm a beginner to this field and i'm going to work inidividually...

so i'm expecting the supports of experienced persons like u...

plzz give me ur friends mail id who r knowing about that field...

Will u plz help me?

Expecting for ur reply...
senthil
Dec 22 '06 #7
ronverdonk
4,258 Expert 4TB
You'd better turn to the C/C++ forum for these questions. Link is
http://www.thescripts.com/forum/forum129.html

Ronald :cool:
Dec 22 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Daniel Tonks | last post: by
3 posts views Thread by nymano | last post: by
17 posts views Thread by broughcut | last post: by
4 posts views Thread by Marcus | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.