我做错了吗将数组转换为数组值

时间:2010-03-03 05:30:23

标签: php mysql

  

可能重复:
  mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

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

我得到了这个:

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 ) 

这是我的代码:

<?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));

                    }

        }   


?>

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您的SQL查询从您的数据库中提取note_id字段 - 而且只提取该字段。

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

echo $data_row['note_id'] . '<br />';

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

$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表中的一些附加数据,则必须在SQL查询中选择更多字段(例如)

$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']读取每个不同的列。