How to echo users from table those are logged in? - php

I am trying to make a list that echos only members who are currently signed in.
Been googling for an answer all day and no luck.
I have an ENUM row for each column that represents when a user is logged on or logged off as 0 or 1. But how would I go about populating a list of users which only have their ENUM value set to 1?
This is what I got:
<?php
// Create connection
include_once '/phpscripts/credentials.php';
$conn = new mysqli($db_hostname, $db_username, $db_password, $db_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
$sql="SELECT * FROM users";
$INFO=mysqli_query($conn, $sql);
While($accounts=mysqli_fetch_assoc($INFO)){
$sql = "SELECT * FROM users WHERE loggedin = '1'";
echo $accounts['username'];
echo "<br>";
}//end while
}
?>

I have modified your code. Try the code below
<?php
// Create connection
include_once '/phpscripts/credentials.php';
$conn = new mysqli($db_hostname, $db_username, $db_password, $db_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
$sql = "SELECT * FROM users WHERE loggedin = '1'";
$INFO=mysqli_query($conn, $sql);
if (mysqli_num_rows($INFO) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($INFO)) {
echo "Username: " . $row["username"]. "<br>";
}
} else {
echo "0 results";
}
}
?>

Related

how to use database data to determine what action to take

I am looking to have a page with a redirect based on what is in the database.
I have a table called "nametable" and 2 columns "id" and "switch"
The id never changes, only the switch entry does. Sometimes it will have "on" as the entry, and sometimes it will have "off" as the entry (depending on what I enter in there at the time)
So I want a page where the website visitor will go to, and if the database says "on" then they will be redirected to, lets say "pageon.php" and if it says off, the visitor will be redirected to "pageoff.php"
I managed to do a simple echo to show on or off in text on the page. But I don't have the foggiest on how to have them redirected based on that.
Any thoughts on what I should search for to make this happen? And advice is appreciated.
PS. I tend to get a -1 because the site thinks I'm not specific on what I am wanting to do. If I am unclear, please tell me so I can revise before closing or -1
Thank you
EDIT: Based on the advice I was given in the comments, I have made this so far. I'm only getting a blank page though. Any thoughts?
<?php
$servername = " ";
$username = " ";
$password = " ";
$dbname = " ";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, switch FROM nametable";
$result = $conn->query($sql);
if ($row['switch'] == "on") header("Location: off.php"); else header("Location: on.php");
$conn->close();
?>
Try this:
<?php
$servername = " ";
$username = " ";
$password = " ";
$dbname = " ";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, switch FROM nametable";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if ($row['switch'] == "on"){
header("Location: off.php");
} else {
header("Location: on.php");
}
$conn->close();
?>
I got it. Thanks to the help in the comments, and the answer by #Edgaras except I made a tiny switch to have the == off, and that made it work. thank you all so much. Here is the solution.
<?php
$servername = " ";
$username = " ";
$password = " ";
$dbname = " ";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, switch FROM nametable";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if ($row['switch'] == "off"){
header("Location: off.php");
} else {
header("Location: on.php");
}
$conn->close();
?>

Why does mysqli() not recognize my database name?

I'm new to PHP and MySQL. While learning, I got this error that says my database is unkown.
I have already made this database.
and I have already made the table 'todos'
Here is my PHP
<?php
require 'functions.php';
$conn = new mysqli('localhost','root','', 'mytodo');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$statement = $conn->prepare('select * from todos');
$statement->execute();
var_dump($statement->fetchAll());
require 'index.view.php';
the PHP file is named 'Index.test.php'
but when I try to access localhost/Index.test.php on my browser
it returns this
Could you tell me why I am getting the error? Appreciate the help!
try to use this select:
<?php
require 'functions.php';
$conn = new mysqli('localhost','root','', 'mytodo');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//select query
$sql = "SELECT * FROM todos";
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
echo $row['your column name'];
}
//free result set
mysqli_free_result($result);
} else {
echo "no records found";
}
}
else {
echo "error: could not connect" . mysqli_error($conn);
}
require 'index.view.php';
?>

Sorting my guestbook posts on time/date (php/mySQL)

I need the posts on my guestbook to be sorted by time ascending.
I have been told that I need to add:
ORDER by datetime
in my code. But I dont know what the correct way to enter this line is.
Here is my code:
<?php
$host = "ZZZ"; // Host name
$username = "ZZZ"; // Mysql username
$password = "ZZZ"; // Mysql password
$db_name = "ZZZ"; // Database name
$tbl_name = "ZZZ"; // Table name
// Create connection
$conn = mysqli_connect($host, $username, $password, $db_name);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM ". $tbl_name ." ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "
<b> Name: ". $row["name"]."<br>
Date Added: : ". date('d-m-Y H:i', $row["datetime"]) ."</b><br><br>
Comment: ". $row["comment"]."<br>
<br>
";
}
} else {
echo "0 results";
}
mysql_close(); //close database
?>
$sql = "SELECT * FROM " . $tbl_name . " ORDER BY datetime ASC";
Also you have mysql_close and your other functions are mysqli

php read data from sql database

I'm trying to learn php and I can't read any data from my database!. I know the connection to the server is live and working but this line seem to be giving me problems.
$result = $conn->query($sql);
Where $sql = "SELECT firstName, middleName, lastName FROM Base";
I'm not sure what the problem is but any hints or answer are appreciated .
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>My first PHP page</h1>
<?php
// connect to database
$user_name = "superUser";
$password = "";
$database = "Base"$server = "127.0.0.5";
// open connection to the server
$conn = new mysqli($server, $user_name, $password);
// echo "Connection to the server is open";
// check connetion
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
print "Connection to the server is open";
}
// load the data from the table
$sql = "SELECT firstName, middleName, lastName FROM Base";
// echo $conn->server_info;
$result = $conn->query($sql);
if ($result) {
echo "Table was found";
}
else echo "no";
/*while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br />";
}
print $result["firstName"];// ": Number of Rows!" ;*/
// if ($result->num_rows > 0) {
// output data of each row
// close server connection
$conn->close();
?>
</body>
</html>
The following is a mysqli connect example.
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
You forgot to add your database name.
Change this,
$conn = new mysqli($server, $user_name, $password);
to this
$conn = new mysqli($server, $user_name, $password, $database);

Unable to select student: Access denied for user ''#'localhost' to database 'student'

Unable to select student: Access denied for user ''#'localhost' to
database 'student'
Error when tried to run the following codes.
none of the method described here works this is the code i've used
<?php
$servername = "localhost";
$username = "amal";
$password = "ZtFnzcDQB5K9hutM";
$dbname = "student";
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (!mysql_select_db("student")) {
echo "Unable to select student: " . mysql_error();
exit;
}
$sql = "SELECT id as Name, Course, DOB, Gender FROM application WHERE userstatus = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["id"];
echo " ";
echo $row["Name"];
echo " ";
echo $row["Course"];
echo " ";
echo $row["DOB"];
echo " ";
echo $row["Gender"];
echo "<br><br>";
}
mysql_free_result($result);
$conn->close();
?>
Try this something like this :
<?php
$servername = "localhost";
$username = "amal";
$password = "ZtFnzcDQB5K9hutM";
$dbname = "student";
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (!mysqli_select_db("student")) {
trigger_error('Database connection failed : ' .$conn->connect_error , E_USER_ERROR);
exit;
}
?>
You used both mysqli and mysql function. Use any one of then it will solve.

Categories