显示mysql中的多个值

zf2sa74q  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(273)

我是一个新的php初学者
这是我table上的照片。单击此处查看照片
我想展示“教育”的所有价值。
例如:

  1. My 1st value is 53
  2. My 2nd value is 43
  3. My 3rd value is 57
  4. My 4th value is 44
mu0hgdu0

mu0hgdu01#

正如乌穆德指出的,你可以使用 explode 要将字符串拆分为一个值数组,然后遍历该列表-http://php.net/manual/en/function.explode.php
下面,我定义了一个新函数 ordinal 它将输出第1,第2,第3等给定的任何数字。
除此之外,你还可以 sprintf 使用占位符格式化字符串。
前任amplehttp://sandbox.onlinephpfunctions.com/code/1459ec55d6bc9f28a03645625a22261ede093342
编辑添加的代码以打开错误报告。

  1. <?php
  2. // Turn on error reporting
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6. // Turn MySQL errors into PHP exceptions
  7. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  8. // Establish DB connection
  9. $db = new mysqli("localhost","username","password","dbname");
  10. $sql = "select * from tbldatingusermaster order by userid desc";
  11. $result = $db->query($sql);
  12. while($data = $result->fetch_object()){
  13. // Split the list of values into an array
  14. $arrayOfEducationValues = explode(',', $data->education);
  15. // Define what you want your output to look like.
  16. $format = "My %s value is %d \n";
  17. // Loop over the list of values and then output each one using the
  18. // formatted string
  19. foreach ($arrayOfEducationValues as $key => $education) {
  20. // `$key` here refers to current index of the array. Since
  21. // array idexes usually start at 0 you need to add `1` to it.
  22. echo sprintf($format, ordinal($key+1), $education);
  23. }
  24. }
  25. /*
  26. * Taken from https://stackoverflow.com/a/3110033/296555
  27. * /
  28. function ordinal($number) {
  29. $ends = array('th','st','nd','rd','th','th','th','th','th','th');
  30. if ((($number % 100) >= 11) && (($number%100) <= 13))
  31. return $number. 'th';
  32. else
  33. return $number. $ends[$number % 10];
  34. }
展开查看全部
rbl8hiat

rbl8hiat2#

  1. $education = explode(',', $data->education); // explode it using comma
  2. for($i=0;$i<count($education);$i++){ // iterate the array
  3. echo $education[$i];
  4. }

相关问题