dongwen5019 2018-07-07 21:11
浏览 213
已采纳

从具有多个ID的相同Mysql列获取数据

Im trying to Fetch data from same Mysql column with multiple Ids and assign each to a variable.

My table is like this

|id |credit|
+----------+
|1  |10    |
+----------+
|2  |20    |
+----------+
|3  |30    |
+----------+

php code is

$sql = "SELECT credit FROM dailycredits Where id IN ('1','2','3')";
$result = $connect->query($sql);
while ($row = mysql_fetch_array($result)){
$id1= $row[0]["credit"];
$id2= $row[1]["credit"];
$id3= $row[2]["credit"];
}
  • 写回答

1条回答 默认 最新

  • dorkahemp972157683 2018-07-07 22:13
    关注

    Most of case id column is integer, then remove single quotes from following line

    change this

    $sql = "SELECT credit FROM dailycredits Where id IN ('1','2','3')";
    

    to

    $sql = "SELECT credit FROM dailycredits Where id IN (1, 2, 3)";
    

    Note: Single quotes removed from IN

    Try this...

    $sql = "SELECT credit FROM dailycredits Where id IN (1,2,3)";
    $result = $connect->query($sql);
    $rows = []; //empty array.
    while ($row = mysql_fetch_array($result)){
    $rows[] = $row; //assigning credit to array.
    }
    
    print_r($rows);
    

    Or you can now access like $rows[0], $rows[2].

    Since mysql_ is deprecated in php 5.5.0, I'm strongly not recommend to use mysql_, use mysqli_* instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?