Home > OS >  Hi, I am trying to display the logged in user's info but it doesn't work. I am new to php
Hi, I am trying to display the logged in user's info but it doesn't work. I am new to php

Time:09-16

This is my home/profile page code:

<?php

session_start();
require 'functions.php';


if (isset($_SESSION['username'])) {
    $userInfo = getUserInfo(getId($_SESSION['username']));
}

?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="home-style.css">
<title>Homepage</title>
</head>
<body>
    <section>
        <div class="head">
            <div class="logo">
                <img src="logo/logo.jpg" class="img" alt="logo">
            </div>
            <div class="info">
                <h1>
                    <?php echo $userInfo['username']; ?>
                </h1>
            </div>
        </div>
    </section>
</body>
</html>

This is my functions code:

            <?php

            require 'config.php';

            function getUserInfo($id) 
            {
                $array = array();
                $q = mysqli_query("SELECT * FROM users WHERE id=".$id);
                while ($row = mysqli_fetch_assoc($q)) {
                    $array['id'] = $row['id'];
                    $array['username'] = $row['username'];
                    $array['email'] = $row['email'];
                }
                return $array;
            }

            function getId($username)
            {
                $q = mysqli_query("SELECT id FROM users WHERE username=".$username."");
                while ($row = mysqli_fetch_assoc($q)) {
                    return $row['id'];
                }
            }

            ?>

When i try and open the home page it gives me this error:

"Fatal error: Uncaught ArgumentCountError: mysqli_query() expects at least 2 arguments, 1 given in D:\xampp\htdocs\test\functions.php:19 Stack trace: #0 D:\xampp\htdocs\test\functions.php(19): mysqli_query('SELECT id FROM ...') #1 D:\xampp\htdocs\test\home.php(8): getId('bob') #2 {main} thrown in D:\xampp\htdocs\test\functions.php on line 19"

CodePudding user response:

You are missing the connection parameter. Quoting the PHP manual...

Parameters ¶ mysql Procedural style only: A mysqli object returned by mysqli_connect() or mysqli_init()

query The query string.

https://www.php.net/mysqli-query

CodePudding user response:

in function getId, you have error, use this sql :

"SELECT id FROM users WHERE username='.$username."';"
  • Related