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) di开发者_Go百科e('Question not found.');
$question = $result->fetch_object()->question;
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.
mysql -h (hostname) -u (username)
use questions;
SELECT * FROM questions;
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 :
$result = mysql_query("SELECT * FROM questions where Name='{$name}'", $db) or die('Question not found.');
if ( $db->mysql_num_rows($result) == 1) {
$result_arr = $db->mysql_fetch_assoc($result);
$question = $result_arr['Question'];
echo $question;
}
If the column is called Question, you need $result->fetch_object()->Question
instead - properties are case sensitive.
精彩评论