By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,369 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

Query issues - works manually... doesn't with mysql_real_query

P: n/a
Hey everyone. I have this query

select * from `TableName` where `SomeIDField` 0

I can open a mysql command prompt and execute this command with no
issues. However, Im trying to issue the same command inside of
mysql_real_query and I keep on getting this error back.

"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1"

I added \n's to my string to determine that its not liking the end of
my query. So here is the quick snippet of code.

stl::string query ="select * from `TableName` where `SomeIDField` 0";
// .... some other non-important stuff ...
if ( mysql_real_query(&m_Mysql, query.c_str(), query.length()+2) != 0 )
{
status->Error(mysql_errno(&m_Mysql), "Query failed! [" +
stl::string(mysql_error(&m_Mysql)) + "]");
}

So thats it. What am I missing?

Also, a side question. The above line only works with the third
parameter being query.length() + 2. +1 Gives me a seg fault. Why is
this? Isn't the null character a single character??

Thanks for all help in advance!

Aug 29 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Why the "+2"? Supplying the actual length seems to work just fine,
e.g.:

std::string query ="select * from `TableName` where `SomeIDField` 0";
if ( mysql_real_query(&m_Mysql, query.c_str(), query.length()) != 0 )

elitecodex wrote:
Hey everyone. I have this query

select * from `TableName` where `SomeIDField` 0

I can open a mysql command prompt and execute this command with no
issues. However, Im trying to issue the same command inside of
mysql_real_query and I keep on getting this error back.

"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1"

I added \n's to my string to determine that its not liking the end of
my query. So here is the quick snippet of code.

stl::string query ="select * from `TableName` where `SomeIDField` 0";
// .... some other non-important stuff ...
if ( mysql_real_query(&m_Mysql, query.c_str(), query.length()+2) != 0 )
{
status->Error(mysql_errno(&m_Mysql), "Query failed! [" +
stl::string(mysql_error(&m_Mysql)) + "]");
}

So thats it. What am I missing?

Also, a side question. The above line only works with the third
parameter being query.length() + 2. +1 Gives me a seg fault. Why is
this? Isn't the null character a single character??

Thanks for all help in advance!
Aug 29 '06 #2

P: n/a

sh**********@gmail.com wrote:
Why the "+2"? Supplying the actual length seems to work just fine,
e.g.:

std::string query ="select * from `TableName` where `SomeIDField` 0";
if ( mysql_real_query(&m_Mysql, query.c_str(), query.length()) != 0 )

elitecodex wrote:
Hey everyone. I have this query

select * from `TableName` where `SomeIDField` 0

I can open a mysql command prompt and execute this command with no
issues. However, Im trying to issue the same command inside of
mysql_real_query and I keep on getting this error back.

"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1"

I added \n's to my string to determine that its not liking the end of
my query. So here is the quick snippet of code.

stl::string query ="select * from `TableName` where `SomeIDField` 0";
// .... some other non-important stuff ...
if ( mysql_real_query(&m_Mysql, query.c_str(), query.length()+2) != 0 )
{
status->Error(mysql_errno(&m_Mysql), "Query failed! [" +
stl::string(mysql_error(&m_Mysql)) + "]");
}

So thats it. What am I missing?

Also, a side question. The above line only works with the third
parameter being query.length() + 2. +1 Gives me a seg fault. Why is
this? Isn't the null character a single character??

Thanks for all help in advance!
I cannot answer that in total honesty... But query.length() and
query.length() + 1 both resulted in segfaults. +2 is the only way I
could get it to run.... well, at least not crash. mysql_real_query did
not like just the length.

Aug 31 '06 #3

P: n/a

elitecodex wrote:
sh**********@gmail.com wrote:
Why the "+2"? Supplying the actual length seems to work just fine,
e.g.:

std::string query ="select * from `TableName` where `SomeIDField` 0";
if ( mysql_real_query(&m_Mysql, query.c_str(), query.length()) != 0 )

elitecodex wrote:
Hey everyone. I have this query
>
select * from `TableName` where `SomeIDField` 0
>
I can open a mysql command prompt and execute this command with no
issues. However, Im trying to issue the same command inside of
mysql_real_query and I keep on getting this error back.
>
"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1"
>
I added \n's to my string to determine that its not liking the end of
my query. So here is the quick snippet of code.
>
stl::string query ="select * from `TableName` where `SomeIDField` 0";
// .... some other non-important stuff ...
if ( mysql_real_query(&m_Mysql, query.c_str(), query.length()+2) != 0 )
{
status->Error(mysql_errno(&m_Mysql), "Query failed! [" +
stl::string(mysql_error(&m_Mysql)) + "]");
}
>
So thats it. What am I missing?
>
Also, a side question. The above line only works with the third
parameter being query.length() + 2. +1 Gives me a seg fault. Why is
this? Isn't the null character a single character??
>
Thanks for all help in advance!

I cannot answer that in total honesty... But query.length() and
query.length() + 1 both resulted in segfaults. +2 is the only way I
could get it to run.... well, at least not crash. mysql_real_query did
not like just the length.
Ok, I figured since the way I am suppose to do this is just use
query.length() I decided to change it back to that and figure out the
segfault.

Here is the gdb output.

#0 0xb7d3cd31 in free_root () from /usr/lib/libmysqlclient.so.15
#1 0xb7d677cc in free_old_query () from /usr/lib/libmysqlclient.so.15
#2 0xb7d6c54f in mysql_close () from /usr/lib/libmysqlclient.so.15
#3 0xb7d6cc04 in mysql_real_query () from
/usr/lib/libmysqlclient.so.15
#4 0xb7ea8fdc in will::db::DbMysqlProtocol::QueryEx (this=0x8053108,
query=
{static npos = 4294967295, _M_dataplus =
{<std::allocator<char>= {<__gnu_cxx::new_allocator<char>= {<No data
fields>}, <No data fields>}, _M_p = 0x8054ef4 "select * from
TableName"}}, resultPointer=0x8053d08, numRows=0x8054478) at
db_mysql_protocol.cpp:118

Why would mysql_real_query() call mysql_close() ?? I cannot find the
implementation in the Mysql source code that has anything to do with
mysql_close. Can someone point me in the correct direction here?
Thanks alot!

Aug 31 '06 #4

P: n/a
elitecodex wrote:
elitecodex wrote:
sh**********@gmail.com wrote:
Why the "+2"? Supplying the actual length seems to work just fine,
e.g.:
>
std::string query ="select * from `TableName` where `SomeIDField` 0";
if ( mysql_real_query(&m_Mysql, query.c_str(), query.length()) != 0 )
>
>
>
elitecodex wrote:
Hey everyone. I have this query

select * from `TableName` where `SomeIDField` 0

I can open a mysql command prompt and execute this command with no
issues. However, Im trying to issue the same command inside of
mysql_real_query and I keep on getting this error back.

"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1"

I added \n's to my string to determine that its not liking the end of
my query. So here is the quick snippet of code.

stl::string query ="select * from `TableName` where `SomeIDField` 0";
// .... some other non-important stuff ...
if ( mysql_real_query(&m_Mysql, query.c_str(), query.length()+2) != 0 )
{
status->Error(mysql_errno(&m_Mysql), "Query failed! [" +
stl::string(mysql_error(&m_Mysql)) + "]");
}

So thats it. What am I missing?

Also, a side question. The above line only works with the third
parameter being query.length() + 2. +1 Gives me a seg fault. Why is
this? Isn't the null character a single character??

Thanks for all help in advance!
I cannot answer that in total honesty... But query.length() and
query.length() + 1 both resulted in segfaults. +2 is the only way I
could get it to run.... well, at least not crash. mysql_real_query did
not like just the length.

Ok, I figured since the way I am suppose to do this is just use
query.length() I decided to change it back to that and figure out the
segfault.

Here is the gdb output.

#0 0xb7d3cd31 in free_root () from /usr/lib/libmysqlclient.so.15
#1 0xb7d677cc in free_old_query () from /usr/lib/libmysqlclient.so.15
#2 0xb7d6c54f in mysql_close () from /usr/lib/libmysqlclient.so.15
#3 0xb7d6cc04 in mysql_real_query () from
/usr/lib/libmysqlclient.so.15
#4 0xb7ea8fdc in will::db::DbMysqlProtocol::QueryEx (this=0x8053108,
query=
{static npos = 4294967295, _M_dataplus =
{<std::allocator<char>= {<__gnu_cxx::new_allocator<char>= {<No data
fields>}, <No data fields>}, _M_p = 0x8054ef4 "select * from
TableName"}}, resultPointer=0x8053d08, numRows=0x8054478) at
db_mysql_protocol.cpp:118

Why would mysql_real_query() call mysql_close() ?? I cannot find the
implementation in the Mysql source code that has anything to do with
mysql_close. Can someone point me in the correct direction here?
Thanks alot!
It's hard to tell without seeing your code, but are you checking the
return codes from the calls to mysql_init() and mysql_real_connect()
prior to your call to mysql_real_query()? Also, have you tried just
hard-coding a simple query like "SELECT 'x'" or "SELECT 1"? Tried using
mysql_query() instead of mysql_real_query?

Aug 31 '06 #5

P: n/a
sh**********@gmail.com wrote:
elitecodex wrote:
elitecodex wrote:
sh**********@gmail.com wrote:
Why the "+2"? Supplying the actual length seems to work just fine,
e.g.:

std::string query ="select * from `TableName` where `SomeIDField` 0";
if ( mysql_real_query(&m_Mysql, query.c_str(), query.length()) != 0 )



elitecodex wrote:
Hey everyone. I have this query
>
select * from `TableName` where `SomeIDField` 0
>
I can open a mysql command prompt and execute this command with no
issues. However, Im trying to issue the same command inside of
mysql_real_query and I keep on getting this error back.
>
"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1"
>
I added \n's to my string to determine that its not liking the end of
my query. So here is the quick snippet of code.
>
stl::string query ="select * from `TableName` where `SomeIDField` 0";
// .... some other non-important stuff ...
if ( mysql_real_query(&m_Mysql, query.c_str(), query.length()+2) != 0 )
{
status->Error(mysql_errno(&m_Mysql), "Query failed! [" +
stl::string(mysql_error(&m_Mysql)) + "]");
}
>
So thats it. What am I missing?
>
Also, a side question. The above line only works with the third
parameter being query.length() + 2. +1 Gives me a seg fault. Why is
this? Isn't the null character a single character??
>
Thanks for all help in advance!
>
I cannot answer that in total honesty... But query.length() and
query.length() + 1 both resulted in segfaults. +2 is the only way I
could get it to run.... well, at least not crash. mysql_real_query did
not like just the length.
Ok, I figured since the way I am suppose to do this is just use
query.length() I decided to change it back to that and figure out the
segfault.

Here is the gdb output.

#0 0xb7d3cd31 in free_root () from /usr/lib/libmysqlclient.so.15
#1 0xb7d677cc in free_old_query () from /usr/lib/libmysqlclient.so.15
#2 0xb7d6c54f in mysql_close () from /usr/lib/libmysqlclient.so.15
#3 0xb7d6cc04 in mysql_real_query () from
/usr/lib/libmysqlclient.so.15
#4 0xb7ea8fdc in will::db::DbMysqlProtocol::QueryEx (this=0x8053108,
query=
{static npos = 4294967295, _M_dataplus =
{<std::allocator<char>= {<__gnu_cxx::new_allocator<char>= {<No data
fields>}, <No data fields>}, _M_p = 0x8054ef4 "select * from
TableName"}}, resultPointer=0x8053d08, numRows=0x8054478) at
db_mysql_protocol.cpp:118

Why would mysql_real_query() call mysql_close() ?? I cannot find the
implementation in the Mysql source code that has anything to do with
mysql_close. Can someone point me in the correct direction here?
Thanks alot!

It's hard to tell without seeing your code, but are you checking the
return codes from the calls to mysql_init() and mysql_real_connect()
prior to your call to mysql_real_query()? Also, have you tried just
hard-coding a simple query like "SELECT 'x'" or "SELECT 1"? Tried using
mysql_query() instead of mysql_real_query?
Ok... my stupidity. Sometimes I wish I could find these things prior
to posting online. My issue that I thought that mysql_library_init was
a replacement for mysql_init... so I never called mysql_init. Before I
was doing this

mysql_library_init()
mysql_real_connect()
// queries
mysql_library_end()

So I changed it to this and it worked
mysql_library_init()
mysql_init()
mysql_real_connect()
// queries
mysql_library_end()
mysql_close()

just a bit of confusion on my part from the Mysql docs. Perhaps my
stupidity will help someone else :) Thanks alot for your help
shakahshakah.

Sep 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.