- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一些嵌入到 C++ 中的 MYSQL,但出现了一些我无法查明原因的行为。
我有一个主要功能和两个可以按作者或出版商搜索的功能。如果我按作者搜索,我会在第一时间得到正确的结果。如果我按出版商搜索,我第一次不会得到任何结果。但是,无论我做什么,第二次调用其中一个函数时,我在 while 循环中的“mysql_fetch_row”行遇到了段错误。我假设我正在打开某种与第二次调用冲突的连接,但我一直无法弄清楚。
对于代码墙,我深表歉意,但我不确定问题出在程序的哪个位置。我试图描述每个部分,并指出程序在两个函数中的每一个中崩溃的位置。
主函数,连接数据库,调用其他两个函数
#include <mysql.h>
#include <iostream>
#include <string>
using namespace std;
void searchByAuthor(MYSQL *connect);
void searchByPublisher(MYSQL *connect);
int main()
{
int choice = 0;
MYSQL *connect, mysql;
connect = mysql_init(&mysql);
//connect to the database
connect = mysql_real_connect(connect, SERVER, USER, PASSWORD, DATABASENAME, 0,
NULL, 0);
//Get user input here to select what to search by
//Loops until user quits the program
return 0;
}
按作者搜索 - 获取用户输入并进行 SQL 调用。第一次正确返回结果。
void searchByAuthor(MYSQL *connect)
{
string name;
string sql = "select Title, Price, Type, AuthorFirst, AuthorLast from Book, Author, Wrote where Author.AuthorNum=Wrote.AuthorNum and Wrote.BookCode = Book.BookCode and AuthorLast=\'" + name + "\'";
MYSQL_RES *res_set;
MYSQL_ROW row;
cout << "Enter the last name of the author" << endl;
cin >> name;
mysql_query(connect, (sql).c_str());
res_set = mysql_store_result(connect);
//Crashes here on second call
while ((row = mysql_fetch_row(res_set)) != NULL)
{
cout << endl << row[0] << " "
<< row[1] << " " << row[2] << " "
<< row[3] << " " << row[4] << endl;
}
mysql_free_result(res_set);
mysql_close(connect);
}
按发布者搜索 - 获取用户输入并进行 SQL 调用 - 永远不会返回任何结果。
void searchByPublisher(MYSQL *connect)
{
string publisher;
string sql = "select Book.Title from Book, Publisher where Publisher.PublisherName =\'" + publisher + "\' and Publisher.PublisherCode = Book.PublisherCode";
MYSQL_RES *res_set;
MYSQL_ROW row = NULL;
cout << "Enter the name of the publisher" << endl;
cin >> publisher;
mysql_query(connect, sql.c_str());
res_set = mysql_store_result(connect);
//Crashes here on second call
while ((row = mysql_fetch_row(res_set)) != NULL)
{
cout << endl << row[0] << endl;
}
mysql_free_result(res_set);
mysql_close(connect);
}
最佳答案
您使用 mysql_close(connect)
关闭连接,但不确定是否在再次查询之前重新打开连接?检查here当您关闭它时, handle 也会关闭。
同时检查你的 mysql 数据库是否有空值。
关于c++ - 第二次调用 mysql_fetch_row 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29980398/
这个问题在这里已经有了答案: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expec
我有一个函数可以查询数据库并计算其中的行数。 if($action=='shipping_list') { $row; $shipping=shipping_list(); $
我试图返回一组由开始和结束 session 变量决定的值。该代码可以工作,但由于某种原因不会从初始变量返回任何内容。 例如如果输入范围 ABC001 到 ABC010,则返回 ABC002 - ABC
$sql = "SELECT `description`,`auction_id` FROM `auctions` WHERE `description` REGEXP '(97(8|9))?[[:
mysql_fetch_row当有结果时返回一个数组,当没有更多行时返回 false。 但是,当运行命令时出现与连接相关的错误时,预期的行为是什么? 或者更确切地说,我应该问...mysql_fetc
一头雾水,不知道怎么回事。我要将所有数据从我的第一个表传输到另一个表。这是我的代码: $getdata = mysql_query("SELECT Quantity, Description, Tot
这两个函数,返回的都是一个数组,区别就是第一个函数返回的数组是只包含值,我们只能$row[0], $row[1],这样以数组下标来读取数据,而mysql_fetch_array()返回的
我想要 make 函数来检查特定数据是否在 mysql 的 mydb 中。我写了下面的函数,但我在这行出现错误 $is_ok_array=mysql_fetch_row($is_ok); 有人可以帮我
我试图掌握 PHP 和 MYSQL 编程,但在看到示例后,我无法理解 mysql_fetch_row 如何知道要返回哪一行。例如: 在上面的代码中,echo $row[0]; 返回表中第一列的数据,
我想从我的表格中打印特定类别计数,如何使用正确的引用来做到这一点? mysql: while($row = mysql_fetch_array($result)) { echo $row['c
我有以下代码: $username = $_SESSION['username']; $query = ("SELECT id FROM users WHERE username = '$u
如何在codeigniter中使用mysql_fetch_row?在 php 中,当我用户 mysql_fetch_row 时,我会得到这样的结果 $result = mysql_query("SEL
如果我们知道有 3 个结果。我们可以替换下面的 while 循环吗: res = mysql_use_result(co); row = mysql_fetch_row(res); while ((r
考虑这个简单的代码: $q=mysql_query('SELECT [...]'); while($row=mysql_fetch_assoc($q)){ //Do something with
我不知道获取行时的预期问题是什么:\为什么结果是这样的? $result=mysql_query($query); $tbl.=" ";
抱歉,标题含糊不清,但我已经尝试解决这个问题一个星期了,但已经没有想法了。 表:名称:scores id name password intuition (int, varchar
我正在编写一些嵌入到 C++ 中的 MYSQL,但出现了一些我无法查明原因的行为。 我有一个主要功能和两个可以按作者或出版商搜索的功能。如果我按作者搜索,我会在第一时间得到正确的结果。如果我按出版商搜
我遇到了一个关于简单的 MySQL 函数的问题,它是 mysql_fetch_row,每当我使用它时,我的应用程序都会崩溃,它会在执行时崩溃。无论我运行什么查询,它都会崩溃。核心转储内容如下: (gd
我正在为 MySQL 5.5 编写我的第一个 C 客户端并且偶然发现了 the following page在文档中。几乎在最后,它指出(粗体强调我的,斜体强调不是我的): An advantage
我有以下列出数据库中所有表的代码,我想做的是从列表中排除表名。 $listtables = mysql_query("SHOW TABLES"); if($listtables){ while ($
我是一名优秀的程序员,十分优秀!