How do I get fname from the result of SQL query and store it in $_SESSION['fname'].
<?php
include('init.php');
session_start();
if(isset($_POST))
{
$loginemail=$_POST["loginemail"];
$loginpassword=$_POST["loginpassword"];
$fname="";
$sql = "select count(*),fname from users where password='$loginpassword' and email='$loginemail'";
$result=mysqli_query($con,$sql);
if($result){
$response =array();
while($row=mysqli_fetch_array($result))
{
array_push($response,array("Count"=>$row[0],"name"=>$row[1]));
}
$_SESSION["fname"]=$fname;
echo json_encode(array("server_response"=>$response),JSON_FORCE_OBJECT);
}
else{
echo "error";
}
mysqli_close($con);
}
?>
This is my php code to get the count and fname(username) from database using sql query
init.php has the connection details
my aim here is to retrieve fname using emailid and password
i get the count which is always 1 as per my database restrictions and a fname i want to declare a session variable as this fname
how do i decode the array to get fname
thanks in advance
Seems that you don't assign a value to $fname
<?php
include('init.php');
session_start();
if(isset($_POST))
{
$loginemail=$_POST["loginemail"];
$loginpassword=$_POST["loginpassword"];
$fname="";
$sql = "select count(*),fname from users where password='$loginpassword' and email='$loginemail'";
$result=mysqli_query($con,$sql);
if($result){
$response =array();
while($row=mysqli_fetch_array($result))
{
array_push($response,array("Count"=>$row[0],"name"=>$row[1]));
//
// try assign $fname here if you want the first you can
//
if($fname ==""){
$fname = $row[1];
}
//
}
$_SESSION["fname"]=$fname;
echo json_encode(array("server_response"=>$response),JSON_FORCE_OBJECT);
}
else{
echo "error";
}
mysqli_close($con);
}
?>
Since you are making an associative array, you need to access Count and name as keys. Like:
foreach($response as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br/>";
}
But if you are sure that you will always get a single row, why don't you just store them in scalar variables like $count and $fname and inside your code do this:
$count; $fname = "";
while($row=mysqli_fetch_array($result))
{
$count = $row[0];
$fname = $row[1];
}
Related
Hi i have an slight problem i'm trying top geht tow Results of My pdo query and Print Them but No such luck i've probably just Made a Stupid mistake i'm Not seeing The query seems to be finde so it does make a difference if the name is in the database (and it makes a difference if you put it in quotes) probably the variables are getting a null value or something...
$username="xxx";
$firstname="xxx";
$check=0;
if (isset($_GET['u'])){
$username=strip_tags(#$_GET['u']);
if (ctype_alnum($username)){
$check=$stmt=$link->prepare("SELECT * FROM
users WHERE username = ?");
$stmt->execute(array($username));
$check=$stmt->fetchAll();
if(count($check)==1){
$get=$stmt->fetch(PDO::FETCH_BOTH);
echo "$get";
$username =$get["username"];
$firstname = $get["first_name"];
}else{
echo "<h2> User does not exist!</h2>";
exit();
}
}
}
?>
<h2>Profilepage for: <?php echo "$username"; ?></h2>
<h2>First name: <?php echo "$firstname"; ?></h2
$stmt->fetchAll() is fetching all the results of the query. Once this is done, there are no more results available for $stmt->fetch() to fetch. You should get the data from the $check array.
if (count($check) == 1) {
$get = $check[0];
$username = $get["username"];
$firstname = $get["first_name"];
} else {
echo "<h2> Username does not exist </h2>";
exit();
}
Or you could just replace the fetchAll with fetch.
$stmt->execute(array($username));
$get = $stmt->fetch(PDO::FETCH_ASSOC);
if ($get) {
$username = $get["username"];
$firstname = $get["first_name"];
} else {
echo "<h2> Username does not exist </h2>";
exit();
}
Also, echo "$get" makes no sense. $get is an array, you can't echo it, you need to use print_r($get) or var_dump($get).
I am attempting to display the contents of an array. I have a SELECT query that transfers two rows from the data base with a WHERE statement;
<?php
session_start();
include 'connection.php';
$inspectit = array();
$update1 = $_POST['inspect'];
$query1 = "SELECT title, reason FROM daysoff WHERE DATE(start) = '$update1' AND gighold = 1";
$day = mysql_query($query1);
while($requesting = mysql_fetch_array($day)) {
$inspectit[] = $requesting;
}
$_SESSION['inspect'] = $inspectit;
header('Location: '. $_SERVER['HTTP_REFERER']) ;
?>
I place the session variable into a new variable,$inspect, defined on a different page. I then try to display the array using;
<?php
session_start();
$inspect = $_SESSION['inspect'];
if($inspect == true) {
echo " </br>Name: </br><a> ". implode($inspect) ."</a></br></br>";
unset ($_SESSION['inspect']);
}
?>
If i var_dump($inspect) all the expected data is there, but it looks like an array holding another array. If i execute code as shown above, this gives a notice of array to string conversion. Where am i going wrong?
<?php
session_start();
$inspect = $_SESSION['inspect'];
if($inspect) {
echo " </br>Name: </br><a> ". implode($inspect) ."</a></br></br>";
unset ($_SESSION['inspect']);
}
?>
You are adding an array of query result into an array. try to change:
from:
while($requesting = mysql_fetch_array($day)) {
$inspectit[] = $requesting;
}
to:
$inspectit = mysql_fetch_array($day);
$_SESSION['inspect'] is an array, so you can used check like
$inspect = $_SESSION['inspect'];
if(count($inspect)>0) {
instead of
if($inspect == true) {
try
<?php
session_start();
$inspect = $_SESSION['inspect'];
if($inspect) {
echo " </br>Name: </br><a> ". implode("-",$inspect) ."</a></br></br>";
unset ($_SESSION['inspect']);
}
?>
or you can use empty() function too like
<?php
session_start();
$inspect = $_SESSION['inspect'];
if(!empty($inspect)) {
echo " </br>Name: </br><a> ". implode("-",$inspect) ."</a></br></br>";
unset ($_SESSION['inspect']);
}
?>
With $inspectit = array(); you are setting $inspectit to an array, then you are trying to set the string (or non-array) $_SESSION['inspect'] to equal that array. You can iterate multiple $_SESSION variables to each iteration of $inspectit as follows:
$n=0;
while($requesting = mysql_fetch_array($day)) {
$_SESSION['inspect'+$n] = $requesting;
$n++;
}
$_SESSION['inpect_count']=$n;
Thus you will know how many variables you setup with the $_SESSION['inpect_count']
I tried all above methods to no avail. I did however get it to work by doing;
<?php
session_start();
include 'connection.php';
$inspectname = array();
$inspectreason = array();
$update1 = $_POST['inspect'];
$query1 = "SELECT title, reason FROM daysoff WHERE DATE(start) = '$update1' AND gighold = 1";
$day = mysql_query($query1);
while($requesting = mysql_fetch_array($day)) {
$inspectname[] = $requesting['title'];
$inspectreason[] = $requesting['reason'];
}
$_SESSION['inspectname'] = $inspectname;
$_SESSION['inspectreason'] = $inspectreason;
header('Location: '. $_SERVER['HTTP_REFERER']) ;
?>
then to call and display the arrays;
<?php
if($inspectname == true) {
$size = sizeof($inspectname) - 1;
for($count = 0; $count <= $size; $count++){
echo " </br>Name: </br><a> ". $inspectname[$count] ."</a>";
echo " </br>Details: </br><a> ". $inspectreason[$count] ."</a></br></br>";
unset ($_SESSION['inspectname']);
unset ($_SESSION['inspectreason']);}
}
?>
<?php
include 'config.php'; //connect to db
if(isset($_REQUEST["pwd"]) && isset($_REQUEST["name"])) {
$password = $_REQUEST['pwd']; //pass from previous page
$name = $_REQUEST['name']; //pass from previous page
$checkUserPass = mysql_query("SELECT * FROM validPersonnel WHERE Passkey = '$password' and Name = '$name'", $conn); //check if the user exist
if(mysql_num_rows($checkUserPass) == 1) {
$personnelId = mysql_query("SELECT PersonnelID FROM validPersonnel WHERE Passkey = '$password' and Name = '$name'", $conn); //query user id
while($row = mysql_fetch_assoc($personnelId)) {
echo $row['PersonnelD']; // print user id
}
mysql_close($conn);
//echo "<br/><br/>";
//echo "<script>alert('Logged In.')</script>";
//header("Refresh: 1; url=profile/profile.php?id="'.$id.');
//header('Refresh: 1; url=test.php?id=$personnelId');
} else {
echo "<br/><br/>";
echo "<script>alert('Wrong Password.')</script>";
header('Refresh: 1; url=personnelselect.php');
}
}
?>
i cannot echo the $row['PersonnelD'] the page shows blank. i cannot understand where did i go wrong. this page quesion have been solved
Looks like you have mistake in code:
echo $row['PersonnelD'];
shouldn't it be following?
echo $row['PersonnelID'];
check the mysql_fetch_assoc() function may be its parameter is empty so it can't enter the while loop
Try to debug and check the values came in the variables using var_dump() function. Ex: var_dump($row); in while loop.
In both your querys, you have
"SELECT * FROM validPersonnel WHERE Passkey = '$password' and Name = '$name'"
It should be:
"SELECT * FROM validPersonnel WHERE Passkey = '".$password."' and Name = '".$name."';"
PHP doesn't recognize the $var unless you close the quotes. The period adds the $var to the string.
I am trying to echo out all of the user rows in my database as a select in a form.
It is only showing a blank space. Nothing else.
Here is my code.
<?php
session_start();
require('../../config.php');
$user = $_SESSION['user'];
$qry=("SELECT `rank`, `uname` FROM users WHERE `uname` = '$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row['rank'];
$logged = $_SESSION['loggedin'];
if ($logged == true) {
if ($rank >= 3) {
echo "Succesful, $user.<br />
<form method='POST' action='delete.php'>
<select><option>Please select</option>";
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
}
echo "<option>$lol</option>";
echo "</select>
</form>";
} else {
echo "Your not an admin. $user";
}
} else {
echo "Please login.";
}
?>
First, please stop using mysql_ functions as they are being deprecated. Look into mysqli_ or PDO. Be aware that your script is vulnerable to SQL injection.
The reason your script is not working is because it appears you are calling mysql_fetch_assoc twice. When calling it the second time, there won't be any output if your query only returns a single row.
$qry=("SELECT `rank`, `uname` FROM users WHERE `uname` = '$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row['rank'];
You will need to resubmit a query (something like below) and call that result separately to populate the drop down, or store the result in an array.
$qry=("SELECT `uname` FROM users");
$result=mysql_query($qry);
while ($row = mysql_fetch_assoc($result)) {
echo '<option>' . ucwords($row['uname']) . '</option>';
}
It looks like your while loop is set up badly, I think you need to change
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
}
echo "<option>$lol</option>";
to this
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
echo "<option>$lol</option>";
}
UPDATE:
I reread your question and it seems that you want to display all unames from your table.
The original query was to check if the user is an admin.
Here's how it will look like. But it's just a duplicate of njk's answer.
<?php
session_start();
require('../../config.php');
$user = $_SESSION['user'];
$qry=("SELECT `rank`, `uname` FROM users WHERE `uname` = '$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row['rank'];
$logged = $_SESSION['loggedin'];
if ($logged == true) {
if ($rank >= 3) {
echo "Succesful, $user.<br />
<form method='POST' action='delete.php'>
<select><option>Please select</option>";
// added these 2 lines
$qry=("SELECT `rank`, `uname` FROM users");
$result=mysql_query($qry);
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
echo "<option>$lol</option>";
}
echo "</select>
</form>";
} else {
echo "Your not an admin. $user";
}
} else {
echo "Please login.";
}
?>
Are you sure your database has more than 1 row? You are calling the fetch 1 time so the second time it will try to fetch the second row.
Try also running the query directly into mysql and see if you get the rows you're expecting back, may be its not returning what you expected
I want to access the data in the php array. I have tried array[0] and array['Name'] but i dont get an output. here is my code:
session_start();
$user = $_SESSION['username'];
$sql_1 = "SELECT UserID FROM users WHERE username=$user";
$result_1 = mysqli_query($sql_1);
$uID = mysqli_fetch_array($result_1);
if ($uID=NULL) {
echo 'null';
} else {
echo $uID[0];
}
Now I am not getting any output from the echo command. So what am i doing wrong here?
The Part if ($uID=NULL) is always true[UPDATED:false], because you're doing an assignment rather than a comparism (that would be if ( $uID == NULL ))