php - Mysql 列未返回正确的值

标签 php mysql loops foreach

我正在尝试从特定列中获取类(class)剩余的名额数。但是,结果返回 4,这是不正确的。它应该是15

这是代码:

$courseID = $_GET['id'];

$result = mysql_query("SELECT * FROM course_dates WHERE id = '$courseID'");


$row = mysql_fetch_row($result);


foreach ($row as $record) {
    $placesLeft = $record['places'];
}


echo $placesLeft;

很明显,我是一个十足的菜鸟吗?

编辑:$row的var_dump:

   array(7) { 
    [0]=> string(1) "2"
    [1]=> string(1) "2"
    [2]=> string(56) "Essential leadership and mangement skills in the new nhs"
    [3]=> string(6) "London"
    [4]=> string(10) "2012-12-15"
    [5]=> string(2) "15"
    [6]=> string(3) "450"
   } 

最佳答案

我相信您的 ID 是主/唯一键

因此您只会得到一行作为结果集

所以使用:

$row['places']; 

已经足够了,不再迭代

关于php - Mysql 列未返回正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22613797/

相关文章:

php - 如何迭代php文本框中的多行文本?

Python - 并行化 2D 掩码数组的 python 循环?

替换 R 中的循环 : multivariate k-nearest neighbor regression example

php - 在 codeigniter 中从 mysql 迁移到 postgresql

php - 为什么在数组中没有这样的索引时返回一个字符串值?

php - 镜像网站时仅复制更改的文件

mysql - 选择顶部错误

mysql - 将 mysql 数据库移动到另一个 Amazon EC2 实例的性能影响

mysql - mysql 查询语法错误以在 python 中更新

algorithm - 降低排序算法中的大 O 复杂度