Discussion:
Query issues - works manually... doesn't with mysql_real_query
(too old to reply)
elitecodex
19 years ago
Permalink
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!
s***@gmail.com
19 years ago
Permalink
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
19 years ago
Permalink
...
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.
elitecodex
19 years ago
Permalink
...
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!
s***@gmail.com
19 years ago
Permalink
...
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?
elitecodex
19 years ago
Permalink
...
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.
nvgautam
14 years ago
Permalink
responding to
http://www.1-script.com/forums/Query-issues-works-manually-doesn-t-with-mysql-real-que-article5610--22.htm
nvgautam wrote:
I ran into the same problem. The issue is that when you use
mysql_real_query,
the sql string length that is passed in(3rd param) should be the exact
length of
the sql string passed in for the 2nd param.

Gautam Venkatesh
...
-------------------------------------

Loading...