Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql_num_rows() Segfaults
    primarykey
    data
    text
    <p>I'm writing a program using C++ and the MySQL C API (version 5.1.31 ubuntu2). However, if the query is <em>UPDATE</em> then I get a <em>Segmentation Fault</em> error when executing the line "RowsReturned = mysql_num_rows( Result );".</p> <pre><code>//this code snippet contains only the relevant code MYSQL_RES *Result; long RowsReturned; MYSQL_RES *MYSQLDB::RunQuery( const char* Query ) { if( mysql_query( Connection, Query) ) { std::cout &lt;&lt; "Error: " &lt;&lt; mysql_error( Connection ); exit(1); } Result = mysql_store_result( Connection ); RowsReturned = mysql_num_rows( Result ); return Result; } </code></pre> <p>Compiled using g++ 4.3.3 (g++ test.cpp -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient_r -o Test)</p> <p>Thanks in advance.</p> <pre><code>//this snippet contains the entire class code class MYSQLDB { public: void Connect( const char* DB ); MYSQL_RES *RunQuery( const char* Query ); long NumRows(); void Close(); MYSQL_ROW GetRow(); private: MYSQL *Connection; MYSQL_RES *Result; MYSQL_ROW Row; long RowsReturned; }; void MYSQLDB::Connect( const char* DB ) { Connection = mysql_init( NULL ); if( Connection == NULL ) { std::cout &lt;&lt; "Error: " &lt;&lt; mysql_error( Connection ); exit( 1 ); } if ( mysql_real_connect( Connection, "localhost", "root", "password", DB, 0, NULL, 0 ) == NULL) { std::cout &lt;&lt; "Error: " &lt;&lt; mysql_error( Connection ); exit(1); } } MYSQL_RES *MYSQLDB::RunQuery( const char* Query ) { if( mysql_query( Connection, Query) ) { std::cout &lt;&lt; "Error: " &lt;&lt; mysql_error( Connection ); exit(1); } Result = mysql_store_result( Connection ); RowsReturned = (long)mysql_num_rows( Result ); //ERROR!!!!!!! return Result; } long MYSQLDB::NumRows() { return RowsReturned; } void MYSQLDB::Close() { mysql_free_result( Result ); mysql_close( Connection ); } MYSQL_ROW MYSQLDB::GetRow() { return mysql_fetch_row( Result ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload