0

Hello i im confuzed with this problem, i have converted the php code into the right format MySQLi and i get this wierd error Notice: Array to string conversion in C:\xampp\htdocs\Training\core.php on line 25 So i dont know where is the problem in the login.php or core.php look at the codes and please tell me where should i correct something thank you :)

function getUserData($field) {
        $DBServer = 'localhost';
        $DBUser   = 'root';
        $DBPass   = '';
        $DBName   = 'upstrey';
        $conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT `$field` FROM `users` WHERE `ID`='".$_SESSION['user_id']."'"; // this is the line 25 in the error
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            if($row = fetch_assoc($result)) {
                return $row["$field"];
            }
            // so i want to make getUserData function so i can use it like getUserData('First Name') in other files
        }
        $conn->close();
    }

login.php

if($result->num_rows > 0) {
                    session_start();
                    $user_id = mysqli_fetch_row($result);
                    $_SESSION['user_id'] = $user_id;
                    /*$_SESSION['user_id'] = "1"; <-- this line giving me data from the first ID in the database*/
                    header('Location: home.php');
                    echo "Email and Password Accepted";
                } else {
                    session_start();
                    $_SESSION['user_id'] = ''; // and also tell me if this line is ok ?
                    echo "Invalid Email or Password";
                }

Thank you again

Edited by Stefan_1

3
Contributors
6
Replies
38
Views
2 Years
Discussion Span
Last Post by Stefan_1
0

Line 4 in login.php stores an array (a result row) in your session variable. I think it should be something like this:

$row = mysqli_fetch_row($result);
$_SESSION['user_id'] = $row['user_id']; // you only want to store the user id, so change it to the right column name.
0

Thank you now doesn't show any error but the values arent showing any other options ?

Edited by Stefan_1

0
header('Location: home.php');
echo "Email and Password Accepted";

header redirect before text output, will never provide confirmation
put confirmation in home.php

can't see any code trying to output array values.
what: values are,
where: not showing

I don't think anyone minds a 'bump', people who may answer are in all 24 timezones

Edited by almostbob

0

Yeah, i dont know why that is still there and i know it will never be shown :D
And yeah i forgoted to put the header.php code, but its just simple 2 PHP lines which is getting data from the core.php

<?php
    $firstname = getUserData("First Name");
    $lastname = getUserData("Last Name");
    echo "<a style='color: white;'>$firstname $lastname</a>";
?>

Edited by Stefan_1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.