Click here to Skip to main content
15,891,725 members
Please Sign up or sign in to vote.
2.25/5 (4 votes)
See more:
i m using phpmyadmin sql as database

there is a table with name users and my database name is vcs

there are 4 rows in table

i want to read it from database using c++

C++
#include <mysql.h>
#include <stdio.h>
#include <iostream>

MYSQL mysql,*connection;
MYSQL_RES *result;
MYSQL_ROW row;

char * ip = (char*)"192.168.49.131";
char * usr = (char*)"root";
char * pass = (char*)"admin";
char * db = (char*)"vcs";

unsigned fields;
using namespace std;
int main ()
{
	mysql_init(&mysql);

	connection = mysql_real_connect(&mysql, ip, usr, pass, db, 0, NULL, 0);

	if (connection==NULL)
	{
		std::cout << mysql_error(&mysql)<< std::endl;
	}

	else
	{
		if(mysql_query(&mysql, "INSERT INTO `vcs`.`users`  (`id`, `fname`, `sname`) VALUES (5, 'Rana', 'sameer')"));

		else
		{
			result = mysql_store_result(&mysql);
			fields = mysql_num_fields(result);

			while((row = mysql_fetch_row (result)))
			{
				unsigned long *length;
				length = mysql_fetch_lengths(result);

				for(int i=0; i<fields; i++)
				{
					cout <<  length[i], row[i] , row[i]  ;
					cout << "NULL";
				}
				cout << endl;

			}
		}
		//if (query_state !=0)
		//{
		//cout << mysql_error(connection) << endl;
		//return 1;
		//}
	}


}


i m using this code

but i m not be able to see data

plz help me how can i read all rows from database

i m new in c++
Posted
Updated 29-Feb-12 3:25am
v2
Comments
Schehaider_Aymen 29-Feb-12 9:05am    
Did you try to debug it to check if the code acts as u wish ?
enhzflep 29-Feb-12 10:56am    
Haven't used MySql for a while, though I notice that you've used both ` and ' single quotes. That couldn't be the source of your problems, could it?

I haven't used MySQL, but in any other RDBS you use SELECT to read data, and INSERT to write data.
Hope this helps,
Pablo.
 
Share this answer
 
I am not quite sure what you are trying to do, but, here is how you query all rows from a table in mysql.

SELECT * FROM TableName

You also might want to look at your if else containing the INSERT query. There looks to be a syntax error in there where there is a ; after your if statement.

I hope this helps.
 
Share this answer
 
Comments
prince_rumeel 1-Mar-12 0:03am    
yes you are right that is an error ; after if()

but bro these line of code is for insert data in database.
it is doing good work...

but my goal is to read the data which inserted in database using this code..

there are 5 entries in databse.
i want to read those.

plz help me and edit my above code.
and help me how can i solve my problem???

thanks in advance
Mohibur Rashid 1-Mar-12 0:19am    
that statement is valid; e.g.,
if(x==y);
else printf("they are not equal\n");

above one is very much valid statement
you didn't tell us your actual problem, you just said you are not able to see data. You can have connection problem, you can have Query problem.

Few Issues you need to know:
1. You are not using phpmyadmin sql as your database. you are using MySQL as your database.

2. Your cout will display only the first variable

3. When you declare name space you actually dont need to use std::cout, cout only is enough.

4. They way you defined and connected mysql is not wrong but not appropriate. follow the code below:

C++
 MYSQL *conn;

if (!mysql_real_connect(conn, server,
         user, password, database, 0, NULL, 0)) {
      //error message
      exit(1);
   }


5. user and host permission issue. make sure your mysql has a permitted host and user. i.e., your user may not have permission to connect with mysql database from ip: 192.168.49.131. ensure permission
 
Share this answer
 
Comments
prince_rumeel 1-Mar-12 0:51am    
bro my sql connection is ok

i just want to show all data which is there in the table users

i had enter yesterday
and today i want to see that data using c++.
prince_rumeel 1-Mar-12 0:52am    
#include <mysql.h>
#include <stdio.h>
#include <iostream>

MYSQL mysql,*connection;
MYSQL_RES *result;
MYSQL_ROW row;

char * ip = (char*)"192.168.49.131";
char * usr = (char*)"root";
char * pass = (char*)"admin";
char * db = (char*)"vcs";

unsigned fields;
using namespace std;
int main ()
{
mysql_init(&mysql);

connection = mysql_real_connect(&mysql, ip, usr, pass, db, 0, NULL, 0);

if (connection==NULL)
{
cout << mysql_error(&mysql)<< endl;
}

else
{
if(mysql_query(&mysql, "INSERT INTO `vcs`.`users` (`id`, `fname`, `sname`) VALUES (5, 'Rana', 'sameer')"));

else
{
result = mysql_store_result(&mysql);
fields = mysql_num_fields(result);

while((row = mysql_fetch_row (result)))
{
unsigned long *length;
length = mysql_fetch_lengths(result);

for(int i=0; i
Mohibur Rashid 1-Mar-12 1:52am    
You did not read my answer,

Please read my answer and then reply again, let us know where is your problem

Not only this, you did not read anybodies answer. also read answer 1
prince_rumeel 1-Mar-12 2:27am    
bro my user has permission
and i read all the above solution

bro i want to know

if(mysql_query(&mysql, "Select * from `vcs`.`users` ");

else
{
result = mysql_store_result(&mysql);
fields = mysql_num_fields(result);

while((row = mysql_fetch_row (result)))
{
unsigned long *length;
length = mysql_fetch_lengths(result);

is it compleat code for get data from database

sorry for my bad english
#include <mysql.h><br />
#include <stdio.h><br />
<br />
int main()<br />
{<br />
<br />
  MYSQL mysql;<br />
  MYSQL_ROW row;<br />
  MYSQL_RES *result;<br />
<br />
  unsigned int num_fields;<br />
  unsigned int i;<br />
  mysql_init(&mysql);<br />
  if (!mysql_real_connect(&mysql,"192.168.49.131","t","n","vcs",0,NULL,0))<br />
  {<br />
<br />
   fprintf(stderr, "Failed to connect to database: Error: %s\n",<br />
      mysql_error(&mysql));<br />
  }<br />
  else<br />
 {<br />
   if(mysql_query(&mysql, "SELECT * FROM users"));<br />
     //here goes the error message <!-- s:o --><img src=\"{SMILIES_PATH}/icon_surprised.gif\" alt=\":o\" title=\"Surprised\"><!-- s:o -->)<br />
   else<br />
 {<br />
     result = mysql_store_result(&mysql);<br />
     num_fields = mysql_num_fields(result);<br />
<br />
     while ((row = mysql_fetch_row(result)))<br />
     {<br />
        unsigned long *lengths;<br />
        lengths = mysql_fetch_lengths(result);<br />
<br />
        for(i = 0; i < num_fields; i++)<br />
        {<br />
            printf("[%.*s] \t", (int) lengths[i], row[i] ? row[i] : "NULL");<br />
        }<br />
<br />
        printf("\n");<br />
     }<br />
   }<br />
<br />
  }<br />
<br />
  return 0;<br />
}<br />


i was needed this
 
Share this answer
 
v2
Comments
Mohibur Rashid 1-Mar-12 3:52am    
So, You got your solution?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900