How to select all from table based on username - php

I would like to ask how to select all from table based on username? I mean for example my user 1 insert his/her data and send to localhost and in status page will display their own data only. Below is my status page php.
<?php
require_once('dbConnect.php');
//Creating sql query
$sql = "SELECT * FROM Table";
//getting result
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($r)){
//Pushing name and id in the blank array created
array_push($result,array(
"id"=>$row['id'],
"username"=>$row['username'],
"name"=>$row['name']
));
}
//Displaying the array in json format
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>

"SELECT * FROM Table WHERE username = 'john'"
This is assuming your table is actually named 'Table' as you have it above. Otherwise replace it with the actual table name.
If you have a variable for user name do this:
"SELECT * FROM Table WHERE username = '" . $username . "'";

You can use WHERE clause in the query like.
"SELECT * FROM Table WHERE username = 'lun L'"
But this is not a good way to fetch data by user name. You should select users on ID base and ID should be unique. Because there are so many users who might have the same name.
"SELECT * FROM Table WHERE youUniqueColName = '$uniqueVal'"
If unique column is id then use id='$id'

SELECT * FROM tablename WHERE username = 'username'
Use this if you want a fixed SQL Query in your variable, or use;
SELECT * FROM tablename WHERE username = '$username'
if you have an input that asks a username to display a specific username..

Related

SELECT statement returns first row instead of record that is looked up

I am trying to select a record from my database, and I am return instead the first one in the table. No matter what I try, the first one gets returned.
Here's the query:
$query_task_owner = "select user_id from users where full_name = '$c_task_owner_name'";
$response = #mysqli_query($dbc, $query_task_owner);
Then I try a test to see the value that is returned as such:
echo $response or die(mysql_error());
This is where I see the user_id of the first row.
Even if I try to put a specific value in the query, as follow, I am getting the same result:
$query_task_owner = "select user_id from users where full_name = 'LeBron James'";
I do not understand because when I trying this query directly in PHPMyAdmin, I am getting the right result. So the query itself is correct.
Any idea?
Fetch $response using mysqli_fetch_array().
<?php
$query_task_owner = "select user_id from users where full_name = '$c_task_owner_name'";
$response = #mysqli_query($dbc, $query_task_owner);
$row = mysqli_fetch_array($response,MYSQLI_ASSOC);
echo $row['user_id'];
?>
If, users are more related to that full name. Then, use while loop to fetch all record.
<?php
$query_task_owner = "select user_id from users where full_name = '$c_task_owner_name'";
$response = #mysqli_query($dbc, $query_task_owner);
while($row = mysqli_fetch_array($response,MYSQLI_ASSOC))
{
echo $row['user_id']."<br>";
}
?>

How to select Single ID in MYSQL

I have a table citizens with a field id, title and link. If I use these codes it will display all the content of id, what I need is when I click the first id id it should display the content of the row. I tried to remove the loop and change the array to assoc but when I select the second id it will always display the 1 id.
mysql_select_db("do",$con1);
$sql = "SELECT * FROM citizens ORDER BY id DESC";
$myData = mysql_query($sql,$con1);
while($record = mysql_fetch_array($myData)){
echo $record['div'];
If you want to get single record from database with id=1, try following:
$myID = 1;
mysql_select_db("do",$con1);
$sql = "SELECT * FROM citizens WHERE id = $myID ORDER BY id DESC";
$myData = mysql_query($sql, $con1);
$record = mysql_fetch_row($myData);
echo $record['div'];

Displaying a SQL table data for a specific user

I want to display the table participantes with the columns sorteo, nombre and fecha.
The user info is on another table sellify_users (usern column).
I want to display only that user data using:
SELECT * FROM participantes WHERE nombre = 'usern'
But usern is not in the same table, so if possible I want to call the sellify_users to get the usern data.
<?php
$user = 'database_user';
$password = 'database_pass';
$database="database_name";
mysql_connect(localhost,$user, $password);
#mysql_select_db($database) or die( "Unable to select database");
echo $query = "SELECT * FROM participantes WHERE nombre='usern'";
$result = mysql_query($query);
mysql_close();
?>
If you meant that a user has a record in the table sellify_users and you need to find the usern from it in order to use it in the next query:
$query = "SELECT * FROM participantes WHERE nombre='usern'";
Then all you need to do is to run a query for the table sellify_users first and get the value usern from it, store it in a variable and then use that in your second query. Something like:
$query1 = "SELECT * FROM sellify_users";
$result1 = mysqli_query($con, $query1);
$row1 = mysqli_fetch_assoc($result1);
$usern = $row1['usern'];
$query2 = "SELECT * FROM participantes WHERE nombre='usern'";
$result2 = mysqli_query($con, $query2);
while($row2 = mysqli_fetch_assoc($result2)){
echo $row2['ColumnNameHere1'];
echo $row2['ColumnNameHere2'];
}
Notice: I've passed $con which denotes a connection variable, i.e.
you should read about mysqli or PDO and if there's anything you do not understand, feel free to shoot a query.
EDIT:
If by any chance you are trying to use the last inserted record, you should look for mysqli_insert_id($con); and use that instead.

PHP displaying info from two seperate tables

I have a web page that has a user database, each user has a unique User ID. Now I have created a login page where it starts a session, and the session includes the user ID.
I also have an "update status" option where the user types in a status and it submits the status and the users Unique ID into a new table. So the table will have the Users ID and the status that the user put in.
Now I want to display the users status on a page, and I want to display the user's Username along with it. So basically the code must take the user ID from the status table and then match it with the user ID from the Users table, and from that it must give me the username from the matching ID in the users table.
*FINAL WORKING CODE*
//Connect to mysql
mysql_connect("localhost","root","");
//connect to database
mysql_select_db("users");
//query the database
$query = "select * \n"
. " from status inner join users \n"
. " on status.user_id = users.id\n ORDER BY users.id DESC";
//fetch results / convert results into an array
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$s_firstname = $row['firstname'];
$s_lastname = $row['lastname'];
$s_status = $row['status']
}
I recommend spending some time learning about joins in sql. You would only need one SQL query, and it would be something like this...
select *
from status inner join users
on status.userid = users.userid
where active = '1' and connect = '1'
Edit: Although using mysql functions is not recommended (see the note at the top of this page - http://uk3.php.net/mysql_query), your lines should be like this..
$query = "select * from status inner join users on status.user_id = users.id where active = '1' and connect = '1'";
$result = mysql_query($query);
WHILE ($row = mysql_fetch_array($result)){
... rest of your code....

MySql query on different input fields where some of them are NULL

Hello I have 3 fields on input form which are set via POST method to external php
$id=$_POST['id'];
$nombre=$_POST['nombre'];
$cedula=$_POST['cedula'];
where I would like to make a search option depending on which field have data inside it or if a user put data in all 3 or in only 2 fields to search from the input fields which are not NULL fields in the same table where there is a result.
my sql query is something like that $sql = "SELECT * FROM users WHERE userID = $id AND nombre = $nombre AND cedula = $cedula) ";
obviosly which is not working, what should I do to make it work. Do I need to change only the query or I need to put something before it to check first what is not NULL. Thanks
Firstly, your SQL statement should be updated to have enclosed ' (commas) around string values.
So, modify it to:
$sql = "SELECT * FROM users WHERE userID = '$id' AND nombre = '$nombre' AND pass = '$pass'";
// ----------------------------------------^---^--------------^-------^------------^-----^
Second thing is that you should search a field only when it has a value otherwise, it of no use.
So, your modified PHP code should be:
$sql = "SELECT * FROM users WHERE 1 ";
if (!empty($id)) {
$sql .= " AND userID = '$id' ";
}
if (!empty($nombre)) {
$sql .= " AND nombre= '$nombre' ";
}
if (!empty($pass)) {
$sql .= " AND pass= '$pass' ";
}
And your Database will be searched for the fields only if they have data filled in the form.
Try to add quote:
$sql = "SELECT * FROM users WHERE userID = ".$id." AND nombre = ".$nombre." AND pass = '".$pass."' ";
Yes, you will need to put a check before which will ignore the fields which are null.
Also, you would need to put the $variable inside single quotes ' if they are VARCHAR or CHAR types.

Categories