首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >警告: array_values()期望参数1是数组,字符串给定

问警告: array_values()期望参数1是数组,字符串给定
EN

Stack Overflow用户
提问于 2010-03-03 05:30:23
回答 2查看 1.5K关注 0票数 0

我正在尝试将数组转换为它的值。

我明白了:

代码语言:javascript
复制
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 31 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 32 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 33 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 34 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 35 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 36 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 37 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 38 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 39 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 40 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 41 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 42 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 43 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 44 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 45 )
Warning: array_values() expects parameter 1 to be array, string given in C:\xampp\htdocs\ReadNotes\mynote.php on line 42

Array ( [0] => 46 ) 

这是我的代码:

代码语言:javascript
复制
<?php

//Starting session

session_start();

//Includes mass includes containing all the files needed to execute the full script
//Also shows homepage elements without customs

include ('includes/mass.php');

//Set the  session variable

$username = $_SESSION['username'];


//Check to see if logged in

    if (isset($username))
    
        {

            //Check all databases assoc with the notes for username submissions
            
            $sql_for_username_submission = "SELECT note_id FROM notes WHERE user = '$username' ORDER BY note_id";

            $get_data = mysql_query($sql_for_username_submission);
            
            while ($data_row = mysql_fetch_assoc($get_data))
            
                    {    //Get name_id's in arrays
                            
                         $name_id_array = $data_row; 
                         
                         //Make each one display all information associated with it.
                         
                         foreach($name_id_array as $name_id)
                         
                                  { 
                                        //Convert name_id array to its id number
                                        
                                        $number_on_name_id = array_values($name_id);    
                                  
                                        echo $number_on_name_id."</br>";                                        

                                  } 
                        
                         print_r (array_values($name_id_array));
                        
                    }
            
        }   
        

?>
EN

回答 2

Stack Overflow用户

发布于 2010-03-03 05:34:59

您正在迭代$name_id,因此通常不会在循环中处理整个$name_id。目前还不清楚您想要做什么,但是作为第一步,您应该在循环之外处理array_values()。

票数 1
EN

Stack Overflow用户

发布于 2010-03-03 05:37:41

SQL查询从数据库中获取note_id字段--只有这个字段。

使用mysql_fetch_assoc,每次will循环迭代时,$data_row将包含便笺表一行的数据,这些数据可以使用以下方法访问:

代码语言:javascript
复制
echo $data_row['note_id'] . '<br />';

因此,您的代码可能如下所示:

代码语言:javascript
复制
$sql_for_username_submission = "SELECT note_id FROM notes WHERE user = '$username' ORDER BY note_id";
$get_data = mysql_query($sql_for_username_submission);
while ($data_row = mysql_fetch_assoc($get_data))
{
    // $data_row contains the data for one entry in the DB
    // You could display $data_row here, to see what's in it :
    //print_r($data_row);
    echo $data_row['note_id'] . '<br />';
}

如果您希望从notes表中获得一些额外的数据,那么您必须在select中再选择几个字段(例如):

代码语言:javascript
复制
$sql_for_username_submission = "SELECT note_id, your_value FROM notes WHERE user = '$username' ORDER BY note_id";
$get_data = mysql_query($sql_for_username_submission);
while ($data_row = mysql_fetch_assoc($get_data))
{
    // $data_row contains the data for one entry in the DB
    // You could display $data_row here, to see what's in it :
    //print_r($data_row);
    echo $data_row['note_id'] . ' : ' . $data_row['your_value'] . '<br />';
}

也就是说,不需要两个循环:while循环足以遍历从数据库返回的每一行数据--您可以使用$data_row['name of the column']读取每个不同的列。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2368984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档