【问题标题】:How to find out automatically the numeric index of an specific MySQL column in PHP?如何在 PHP 中自动找出特定 MySQL 列的数字索引?
【发布时间】:2014-06-01 22:50:29
【问题描述】:

我是 PHP 和 SQL 的新手,这是我第一次在这里发帖,希望我做得对 =)

我需要从数据库中的某个字段中找出数字索引。事情是我还没有完成数据库结构,所以对于这个特定的字段,我想让它自动化,这样我就不必每次添加或删除列时都更改代码。

我还需要它同时具有两种索引类型,这样我就可以在部分代码中按名称调用列,并通过索引来调用其中的自动化部分。

我已经设法通过一个非常丑陋的代码实现了我想要的结果,因为我尝试过的所有 array_search 和 array_keys 由于某种原因没有成功。

这是我工作但丑陋的代码的 sn-p。你有更优雅的解决方案吗?

提前感谢您的回答,以及到目前为止我从阅读其他人的问题中学到的所有知识!

$dadosRes = mysqli_query($con, "SELECT * FROM Alunos WHERE Id=1");
$student = mysqli_fetch_array($dadosRes) or die(mysql_error());

$c = 0; // This block is to find out where the column "cont1" is and assign it to $cont1
$d=array_keys($student);
$cont1=0;

foreach ($student as $a=>$b){              
if ($a==='cont1') {
$cont1=$d[$c-1];
}

$c++;
}

for ($i=$cont1; $i<$cont1+12; $i+=3) { // Displays the students contacts, each in up to 3 rows
if ($student[$i]) {
echo "<p><ul><li>".$student[$i]."</li>";
if ($student[$i+1]) echo "<li>".$students[$i+1]."</li>";
if ($student[$i+2]) echo "<li>".$students[$i+2]."</li>";
echo "</ul></p>";
}
}

【问题讨论】:

  • 您在寻找AUTO_INCREMENT 专栏吗?
  • 我认为如果解释一下 $student 包含什么以及 $dadosRes 是什么会更容易。对我来说,这听起来像是你试图解决错误的问题
  • $cont1=$d[$c-1]; 给出cont1 前面的列的名称。
  • 编辑问题以包括 $dadosRes,它是来自数据库的资源。我想要实现的是一种更好的方法来提出 $cont1,它不必遍历所有列。还要在问题上说明这一点,谢谢!
  • 不要使用mysql_fetch_array,而是使用mysql_fetch_assoc,它会为您提供按名称的列。

标签: php mysql arrays indexing


【解决方案1】:

查找数字索引:

$student = mysqli_fetch_array($dadosRes,MYSQLI_ASSOC) or die(mysql_error());
$numericIndex = array_search("cont1",array_keys($student));

给出MYSQLI_ASSOC 将强制查询只获取关联键;否则它会给你数字和关联数组。

【讨论】:

  • 嗯,这给了我想要的数字,但现在上面的其余代码,我使用 $cont1 作为基本索引,不起作用...
  • 我看到您正在使用 $student 数组(现在是关联数组)作为数值数组。考虑在循环之前执行此操作:$nstudent = array_values($student); 将转换为数值数组;然后在循环中使用$nstudent
  • 我会试试看,但请让我问你:array_search 是否有任何理由不能与两个数组一起使用?它返回 0 给我。
  • 我问这个是因为你的解决方案意味着重复的数组数据,一个数组由数字索引,另一个数组是关联的。两个索引都只有一个似乎更好,不是吗?
  • array_search 如果找到键返回&gt;=0,否则返回FALSE。你应该像这样检查它!==FALSE
【解决方案2】:

PHP 函数,在本例中为 array_search,需要将第三个参数设置为 TRUE:

<?php

$dadosRes = mysqli_query($con, "SELECT * FROM Alunos WHERE Id=1");
$student = mysqli_fetch_array($dadosRes, MYSQLI_BOTH) or die(mysql_error());

$student_keys = array_keys($student);
$cont1 = $student_keys[array_search("cont1", $student_keys, true) - 1];

for ($i=$cont1; $i<$cont1+12; $i+=3) { // Displays the students contacts, each in up to 3 rows
    if ($student[$i]) {
        echo "<p><ul><li>".$student[$i]."</li>";
        if ($student[$i+1]) echo "<li>".$student[$i+1]."</li>";
        if ($student[$i+2]) echo "<li>".$student[$i+2]."</li>";
        echo "</ul></p>";
    }
}
?>

MYSQLI_BOTHmysqli_fetch_array 函数中使用,因为首先在数组中搜索列名,然后在数组中循环使用数字索引。这导致 $student 数组的第一个元素为整数 0,因此导致 array_search 阻塞。添加true 作为第三个参数会导致=== 比较解除函数的阻塞。 :)(解释:PHP array_search consistently returns first key of array

【讨论】:

  • 没有成功...但是 $dadosRes 是单行数组 (id=1),为什么要使用“while”?
  • 我无法让 array_search 找到列名,所以我在其中放置了一个循环。正如你所指出的,我退出了 while 循环,这没有必要。
  • 这几乎是我之前所做的,它有效,但你知道为什么 array_search 找不到列名吗?毕竟我想这就是我的问题应该放在首位,我要专门研究这个......
  • 如果数组的第一个值是整数0,那么array_search返回0,不管匹配的元素在数组中更远。 stackoverflow.com/questions/13259614/…
  • 我刚刚发现我的第一个数组元素正好是 INT(0)!不过不知道为什么,我认为它应该是“Id”列...打算尝试取消设置。
猜你喜欢
  • 2021-12-26
  • 2021-12-24
  • 2016-04-16
  • 2012-08-24
  • 1970-01-01
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
相关资源
最近更新 更多