开发者

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 st开发者_C百科ring

The error:

Notice: Undefined index: subject_id in C:\Program Files\EasyPHP at the line 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?


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've given the error as

Notice: Undefined index: subject_id in C:\Program Files\EasyPHP at the line 17

, so subject_id should be the problem. In the example code, you have menu_name as the key of the array. 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.

The problem should be the name of the column that you want to access. Check that again.


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).

$Row=mysql_fetch_array($SubjectSet, MYSQL_ASSOC)


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. Use the mysql_fetch_assoc() method instead if you wish to access columns using their name.


Have you tryed?

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


:) use MYSQL_ASSOC with mysql_fetch_array() or use mysql_fetch_assoc() instead:

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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜