简体   繁体   English

可以使用整数索引访问SQL行数组字段,但不能使用字符串索引访问

[英]SQL Row array field can be reached with integer index but not with string index

I'm getting error when i try to refer index with string 当我尝试使用字符串引用索引时出现错误

The error: 错误:

Notice: Undefined index: subject_id in C:\\Program Files\\EasyPHP at the line 17 注意:未定义的索引:C:\\ Program Files \\ EasyPHP中的subject_id在第17行

The code: 编码:

$Pages = mysql_query("select * from pages where subject_id ={$Row["subject_id"]}",$Connection);

while ($PageRow = mysql_fetch_array($Pages))
{
    echo "<li>{$PageRow["menu_name"]}</li>";
}

But it works fine if i use the integer index instead, 但是如果我改用整数索引,它会很好地工作,

    echo "<li>{$Row[2]}</li>";

I've seen the same code in an example code and it works, is the problem with the MySQl database setting? 我在示例代码中看到了相同的代码,并且可以正常工作,MySQl数据库设置是否有问题?

You are not fetching the result row as an associative array & although you are using curly braces I would change the quotes to single quotes just for consistency . 您不会以关联数组的形式获取结果行,尽管您使用的是花括号,但为了保持一致性,我会将引号更改为单引号。 Change to this: 更改为此:

 $SubjectSet = mysql_query("select * from subjects" , $Connection);
 while($Row = mysql_fetch_assoc($SubjectSet)) // change to mysql_fetch_assoc()
 {
     echo "<li>{$Row['menu_name']}</li>"; // use single quotes
 }

you can check what keys & values are begin returned by using: 您可以使用以下命令检查开始返回的键和值:

$SubjectSet = mysql_query("select * from subjects" , $Connection);
 while($Row = mysql_fetch_assoc($SubjectSet)) // change to mysql_fetch_assoc()
 {
     print_r($Row);
 }

You could try using single quotes instead: 您可以尝试使用单引号代替:

 $SubjectSet=mysql_query("select * from subjects",$Connection);
  while($Row=mysql_fetch_assoc($SubjectSet))
  {
    echo"<li>{$Row['menu_name']}</li>";// line 17
  }

CORRECTION: 更正:

Just noticed you didn't use assoc fetching. 只是注意到您没有使用assoc提取。 Use the mysql_fetch_assoc() method instead if you wish to access columns using their name. 如果希望使用列名访问列,请改用mysql_fetch_assoc()方法。

You've given the error as 您给出的错误为

Notice: Undefined index: subject_id in C:\\Program Files\\EasyPHP at the line 17 注意:未定义的索引:C:\\ Program Files \\ EasyPHP中的subject_id在第17行

, so subject_id should be the problem. ,因此subject_id应该是问题所在。 In the example code, you have menu_name as the key of the array. 在示例代码中,您将menu_name作为数组的键。 So, which one is it? 那么是哪一个呢? What are the columns of this table? 该表的列是什么? The code that you've given will work if there is a "menu_name" column in the table. 如果表中有“ menu_name”列,则您给出的代码将起作用。

The problem should be the name of the column that you want to access. 问题应该是您要访问的列的名称。 Check that again. 再次检查。

Have you tryed? 你有尝试过吗?

$SubjectSet=mysql_query("select * from subjects",$Connection);
while($Row=mysql_fetch_array($SubjectSet)) {
    echo"<li>".$Row["menu_name"]."</li>";
}

It's the quote marks in the echo... you're using double quotes in both instances which will break the string (it shouldn't since you've wrapped it in curly braces, but still...). 它是回声中的引号...在两种情况下您都使用双引号,这会破坏字符串(由于您已经将其用大括号括起来了,所以不应该这样做,但是仍然...)。

Try: 尝试:

echo"<li>{$Row['menu_name']}</li>";

If that doesn't work - ensure you're fetching an associative array from the database; 如果那行不通-请确保您正在从数据库中获取关联数组; you may need to pass either the the constant MYSQL_ASSOC or MYSQL_BOTH to the query (though, by default, it should use MYSQL_BOTH ). 您可能需要将常量MYSQL_ASSOCMYSQL_BOTH传递给查询(尽管默认情况下应使用MYSQL_BOTH )。

$Row=mysql_fetch_array($SubjectSet, MYSQL_ASSOC)

:) use MYSQL_ASSOC with mysql_fetch_array() or use mysql_fetch_assoc() instead: :)使用带有mysql_fetch_array() MYSQL_ASSOC或使用mysql_fetch_assoc()代替:

while($Row=mysql_fetch_array($SubjectSet, MYSQL_ASSOC))

or 要么

while($Row=mysql_fetch_assoc($SubjectSet))

You can read this . 您可以阅读此内容 I tried to make this as short as possible for you! 我试图让这个尽可能简短!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM