数据库问题,无法正确回显数据
我正在尝试创建一个简单的问题/答案网站来获得编码体验。在主页上,输入问题,然后问题和人员姓名存储在数据库中,然后在另一个页面上回显。我无法将数据显示在第二页上。
这是 INSERT 语句:
if ($name && $question) {
safe_query($db, 'INSERT INTO questions (Name, Question) VALUES (%s, %s)',
$name, $question);
redirect('answers.php/'.$name);
}
这是 SELECT 语句:
$result = safe_query($db, 'SELECT * FROM questions where name=%s', $name);
if (!$result->num_rows) die('Question not found.');
$question = $result->fetch_object()->question;
I am trying to create a simple question/ answer website for coding experience. At the home page, you type the question in, and then the question and the person's name are stored in a database, then echoed on another page. I am having trouble getting the data to display on the second page.
Here is the INSERT statement:
if ($name && $question) {
safe_query($db, 'INSERT INTO questions (Name, Question) VALUES (%s, %s)',
$name, $question);
redirect('answers.php/'.$name);
}
Here is the SELECT statement:
$result = safe_query($db, 'SELECT * FROM questions where name=%s', $name);
if (!$result->num_rows) die('Question not found.');
$question = $result->fetch_object()->question;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于缺少一些内容,很难回答您的问题。在不知道你有什么的情况下,检查 mysql 中是否有数据的一种方法是登录并在命令行中检查。
接下来,不确定您的“safe_query”函数是什么或者它正在做什么和返回什么。假设 $db 是您的数据库对象,并且您正在寻找最多一个结果:
There are a few things missing that make it difficult to answer your question. Without knowing what you have, one way to check if you have data in mysql is to login and check at the command line.
Next, not sure what your "safe_query" function is or what it's doing and returning. Assuming $db is your database object and you are looking for at most one result :
如果该列名为 Question,则需要
$result->fetch_object()->Question
- 属性区分大小写。If the column is called Question, you need
$result->fetch_object()->Question
instead - properties are case sensitive.