Hi guys,
Still going through these tutorials, and i've run into a message, and am wondering why I am getting it...

Notice: Array to string conversion in C:\wamp\www\lr\core\functions\users.php on line 26

this is what is on line 26:

	$fields = implode('', $func_get_args); // taking an array and converting it to a string

I searched the forums and found a few posts...like this
one
where to talks about looping it..I don't know if I would need to do this when the guy in the tutorial didn't need to...

When the guy in the tutorial echos out the variable - he gets:

useridusernamepasswordfirst_namelast_name_email

When I echo it out, I not only get the notice message, but also get:

Array,user_id,username,password,first_name,last name,email (mine has array in front of it, not sure if this is causing the issue?)

Here is my full code:

users.php

//pass in any number of fields and return it in an array.
//check line line in init.php - begins with $user_data - we created those lines in init.php
// so we can access more data passed through the function than just $user_id
function user_data($user_id) {
	$data = array();
	$user_id = (int)$user_id; //create an integer from that input, so remove any other characters

$func_num_args = func_num_args();  //shows 7 - how may paremeters we have passed through to this functino
$func_get_args = func_get_args();

// print_r($func_get_args);
//this line above prints the following out on the screen:
// Array ( [0] => 1 [1] => Array ( [0] => user_id ) [2] => user_id [3] => username [4] => password [5] => first_name [6] => last name [7] => email ) 
//spits out the data we defined in line 20 in init.php

if ($func_num_args > 1) {
	unset($func_get_args[0]); //unset first element of the array, the reason we remove it is b/c we want to create a field set from this data


//print_r($func_get_args); 
// When we print the array now, we get:
// Array ( [1] => Array ( [0] => user_id ) [2] => user_id [3] => username [4] => password [5] => first_name [6] => last name [7] => email ) 

$fields = implode('', $func_get_args); // taking an array and converting it to a string
echo $fields;
}
}

init.php

//create a variable, equal to the user_data function, and we might pass in the session id- $_SESSION ID
//$_SESSION['user_id'] (replaced with $session_user_id) - first name, last name, email, etc...and within the user_data function
// we now have access to anything beyond this and we can access these variable and that is useful
// because we don't need to define what we pass through to this and what we return, so essentially $user_data
// is going to be returning all this data from the database.

// if the user is logged in, we know want to grab this data

if(logged_in() === true) {
	$session_user_id = $_SESSION['user_id']; //we are taking this from line 64 in login.php
	$user_data = user_data($session_user_id,['user_id'], 'user_id', 'username', 'password', 'first_name', 'last name', 'email');
}

Thank you kindly for all the help I have received,

    Fixed it....I removed ['user_id'] from this line:

    $user_data = user_data($session_user_id,['user_id'], 'user_id', 'username', 'password', 'first_name', 'last name', 'email'); 

    now looks like:

    $user_data = user_data($session_user_id, 'user_id', 'username', 'password', 'first_name', 'last name', 'email'); 

    Thanks.

      I'm stuck again....this code below ouputs the following to the screen:

      SELECT user_id, username, password, first_name, last name, email FROM users WHERE user_id = 1

      function user_data($user_id) {
      	$data = array();
      	$user_id = (int)$user_id; //create an integer from that input, so remove any other characters
      
      $func_num_args = func_num_args();  //shows 7 - how may paremeters we have passed through to this functino
      $func_get_args = func_get_args();
      
      // print_r($func_get_args);
      //this line above prints the following out on the screen:
      // Array ( [0] => 1 [1] => Array ( [0] => user_id ) [2] => user_id [3] => username [4] => password [5] => first_name [6] => last name [7] => email ) 
      //spits out the data we defined in line 20 in init.php
      
      if ($func_num_args > 1) {
      	unset($func_get_args[0]); //unset first element of the array, the reason we remove it is b/c we want to create a field set from this data
      
      
      //print_r($func_get_args); 
      // When we print the array now, we get:
      // Array ( [1] => Array ( [0] => user_id ) [2] => user_id [3] => username [4] => password [5] => first_name [6] => last name [7] => email ) 
      
      $fields = '`' . implode('`, `', $func_get_args) . '`'; // taking an array and converting it to a string
      echo "SELECT $fields FROM `users` WHERE `user_id` = $user_id";
      die();

      However...

      When we input the query to access the database, I get a blank screen....here is my code below.

      function user_data($user_id) {
      	$data = array();
      	$user_id = (int)$user_id; //create an integer from that input, so remove any other characters
      
      $func_num_args = func_num_args();  //shows 7 - how may paremeters we have passed through to this functino
      $func_get_args = func_get_args();
      
      // print_r($func_get_args);
      //this line above prints the following out on the screen:
      // Array ( [0] => 1 [1] => Array ( [0] => user_id ) [2] => user_id [3] => username [4] => password [5] => first_name [6] => last name [7] => email ) 
      //spits out the data we defined in line 20 in init.php
      
      if ($func_num_args > 1) {
      	unset($func_get_args[0]); //unset first element of the array, the reason we remove it is b/c we want to create a field set from this data
      
      
      //print_r($func_get_args); 
      // When we print the array now, we get:
      // Array ( [1] => Array ( [0] => user_id ) [2] => user_id [3] => username [4] => password [5] => first_name [6] => last name [7] => email ) 
      
      $fields = '`' . implode('`, `', $func_get_args) . '`'; // taking an array and converting it to a string
      //echo $fields;
      // from echoing the above - we get this output:
      // `user_id`, `username`, `password`, `first_name`, `last name`, `email` 
      $mysqli = new mysqli( 'localhost','root','', 'lr' ); 
      $data = "SELECT $fields FROM `users` WHERE `user_id` = $user_id";
      
      if ($result = $mysqli->query($data)) 
      // fetch an associative array for each record returned by the result
      while ($row = $result->fetch_assoc())
      
      // a record was returned! Let's echo it out to see what's in it:
      print_r($row);

      Any idea what's going on?

      Also, instead of me putting in the mysqli connection in a variable each time I create a function, how can I access the connection just once?

      thanks

        6 days later

        Stepping back from this for a little while....still trying to figure it out and get my head wrapped around it...

        If I have a function, and specify the parameters in it, for example a variable for a databas connection...

        login($username, $password, $DB);

        How does it know where to look for $DB variable where it is defined? ...b/c its not defined in the same file as the function, so does it look within the folder of the "website" through all the files to pull the connection, or the variable?

        Thanks,

        Adrian

          php_adrian wrote:

          How does it know where to look for $DB variable where it is defined?

          It's (supposed to be) given a value at some point prior to the function being used - if you walk through the code you should find a place where the variable is set to some value, and then later on come to the place where that value is used.

          .....
          $DB = 'whatever';
          ...
          login($username, $password, $DB);
          

          The login function itself - i.e., the bit that starts

          function login($user, $pass, $database) {

          and ends with the matching [font=monospace]}[/font] doesn't know anything about those [font=monospace]$username[/font], [font=monospace]$password[/font] or [font=monospace]$DB[/font] variables - all it sees are the values those variables contained, which it puts into its own variables (here [font=monospace]$user[/font], [font=monospace]$pass[/font] and [font=monospace]$database[/font]).

            Write a Reply...