简体   繁体   English

无法弄清楚简单的PHP / MySQL“问答”数据库中的错误

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

I'm creating a simple questions DB, here are some details to start: 我正在创建一个简单的问题DB,这里有一些细节要开始:

1st off, I am not using PDO - it's an old webapp, and this question is not about upgrading to PDO. 第一关,我没有使用PDO - 这是一个旧的webapp,这个问题不是关于升级到PDO。

The DB 数据库

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 ;

Already inside the DB: 已经在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);

The PHP 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);
    }

The RESULT (of the PHP) 结果(PHP)

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

As you can see, this is only grabbing the first row - when it should be grabbing all rows since the userID and "active" column of all 3 rows are 1. Anyone see anything here that could be causing this problem? 正如你所看到的,这只是抓住第一行 - 它应该抓住所有行,因为所有3行的userID和“active”列都是1.任何人在这里看到任何可能导致此问题的内容? If I put the query into PHP MyAdmin, the query works correctly - so I have to assume at this point that it's the PHP... This is blowing my mind! 如果我将查询放入PHP MyAdmin,查询工作正常 - 所以我必须假设这是PHP ...这让我大吃一惊!

Thanks. 谢谢。

It seems mysql_fetch_assoc is row fetching, opposite to bulk fetching, which means it will fetch row per row inside a given array. 似乎mysql_fetch_assoc是提取,与批量提取相反,这意味着它将在给定数组内每行获取一行。

You have to loop over, for example in the php page: 你必须循环,例如在php页面中:

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

rgds. RGDS。

try: 尝试:

$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