簡體   English   中英

無法弄清楚簡單的PHP / MySQL“問答”數據庫中的錯誤

[英]Cannot figure out the error in simple PHP/MySQL “Q&A” DB

我正在創建一個簡單的問題DB,這里有一些細節要開始:

第一關,我沒有使用PDO - 這是一個舊的webapp,這個問題不是關於升級到PDO。

數據庫

CREATE TABLE IF NOT EXISTS `questions` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `question` varchar(255) NOT NULL,
  `userID` int(11) unsigned NOT NULL,
  `active` tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

已經在DB中:

INSERT INTO `apl_questions` (`id`, `question`, `userID`, `active`) VALUES
(1, 'Do you have a dog?', 1, 1),
(2, 'Have you ever been arrested?', 1, 1),
(3, 'Pick yes or no...', 1, 1);

PHP

$questionsResult = mysql_query("select * from questions where userID = 1 AND active = 1"); // Get all activated questions
    if(mysql_num_rows($questionsResult) > 0){
        $questions = mysql_fetch_assoc($questionsResult);
        var_dump($questions);
    }

結果(PHP)

array(4) { ["id"]=> string(1) "1" ["question"]=> string(18) "Do you have a dog?" ["userID"]=> string(1) "1" ["active"]=> string(1) "1" } 

正如你所看到的,這只是抓住第一行 - 它應該抓住所有行,因為所有3行的userID和“active”列都是1.任何人在這里看到任何可能導致此問題的內容? 如果我將查詢放入PHP MyAdmin,查詢工作正常 - 所以我必須假設這是PHP ...這讓我大吃一驚!

謝謝。

似乎mysql_fetch_assoc是提取,與批量提取相反,這意味着它將在給定數組內每行獲取一行。

你必須循環,例如在php頁面中:

while ($row = mysql_fetch_assoc($result)) {
   echo $row["userid"];
   echo $row["fullname"];
   echo $row["userstatus"];
}

RGDS。

嘗試:

$questionsResult = mysql_query("select * from questions where userID = 1 AND active = 1"); // //Get all activated questions
if(mysql_num_rows($questionsResult) > 0){
    while($questions = mysql_fetch_assoc($questionsResult)){
      var_dump($questions);
    }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM