Query that looks for current user in table? php, sql - php

I am using php to query a sql db/table to retrieve a message which is on the same row as the current user.
I want to write a query that looks for the current session user in the table, and displays all info in all rows matching their username.
$user = $_SESSION["sess_user"];
$sql=mysqli_query($con, "SELECT * FROM Mail WHERE recipient_username ='".$user."'");
i cannot find the proper arrangement of fetching and displaying, and could use a pointer! thank you for your time.
I have been looking for the answer to this, and I'm sure there is one out there, so if someone has a link, it is more than welcome.

i got it. thanks guys.
$user = $_SESSION["sess_user"];
$sql=mysqli_query($con, "SELECT * FROM Mail WHERE recipient_username ='".$user."'");
if($sql){
$result = mysqli_query($con, "SELECT * FROM Mail WHERE recipient_username ='".$user."'");
while($row = mysqli_fetch_assoc( $result)){
echo'<table border="1" ><th >Message Date</th><th>Sender</th><th>message</th>';
echo '<tr><td>'.$row['msg_date'].'</td>';
echo '<td>' .$row['sender_username']. '</td>';
echo '<td>' .$row['sender_message'].'</td> </tr>' ;
echo '</table>';

Related

Another beginner with a mysqli issue

I'm quite new to working with PHP and databases. So, like all beginners, I'm having problems with the mysqli extension. I was trying to avoid using mysqli_results, as I didn't want to deal with an array and a loop every time I want a simple piece of data. But that might not be possible.
I need to echo $user_count, but nothing seems to be stored there. My code seems to be okay according to the API, but maybe I'm just trying to use the wrong functions altogether. How do I put the result I need into $user_name?
mysqli_query($conn, "SELECT * FROM `wp_users` WHERE 1");
$result_user_count = mysqli_query($conn, "SELECT COUNT(`ID`) FROM `wp_users`");
$user_count = mysqli_fetch_field_direct($result_user_count, 0);
echo $user_count . ' users found on ' . $dbname . ':</br>';
Based on your provided code, you need to change it like below:-
$query = mysqli_query($conn, "SELECT COUNT(ID) AS COUNT FROM wp_users") or die(mysqli_error($conn));
while ($row = mysqli_fetch_assoc($query)) {
echo "Total Users Are:- ". $row["COUNT"];
}
Note:- try to add mysqli error reporting code so that you will what problem actually exist in your query code, and you can easily resolve them. thanks
Try this. comment if not worked.
$query = mysqli_query($conn, "SELECT COUNT(ID) AS COUNT FROM wp_users");
if ($u_count = $mysqli->query($query)) {
while ($res = $u_count->fetch_assoc()) {
echo "Total Users: ". $res["COUNT"];
}
}

Using MYSQL to call a row from a table

I am trying to show specific information for my logged in users, i can call the user_name, but i have been able also to call info from the "website" entry which holds the html for the page, i need to be able to call "website" to each logged in user, i.e Susan (user_name) has the html showing when she logs in, BUT Barry (user_name) has the same html showing as susan, as it is being called from Susans row! Please help it is driving me mad!
Here is the code that is on the myaccount.php:
Welcome To <?php echo $_SESSION['user_name'];?>'s Account Page</strong>
<?php
if (isset($_GET['msg'])) {
echo "<div class=\"error\">$_GET[msg]</div>";
}
?>
<?php
$data = mysql_query("SELECT * FROM users") or die(mysql_error());
$info = mysql_fetch_array( $data );
Print "<b></b> ".$info['$website'] . " ";
?>
You have to add a WHERE clause to your query. If you have a field in your table called user_name it will be like this :
"SELECT * FROM users WHERE user_name = '" . $_SESSION['user_name'] . "'";
Note : You are using a deprecated (mysql_query) you should use rather (mysqli_query) which has different syntax
http://php.net/manual/en/mysqli.query.php
Have you tried
$data = mysql_query("SELECT * FROM users WHERE username='$_SESSION[user_name]'") or die(mysql_error());

retrieve single row data in mysql using php

I created a website that has multiple logins and unique informations to it.. i want to retrieve the data from one user. example my username is qwert and my password is 1234, I want to retrieve his unique information to the database. I used the sample code in w3schools and it selects all the data but all I want to do is to retrieve the data from the user which is only logged in.
can anyone help me about this? any help will be much appreciated.
mysql_select_db("xone_login", $con);
$result = mysql_query("SELECT * FROM admin WHERE username = '$myusername' ");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['overtime'] . "</td>";
echo "<td>" . $row['daily_rate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Replace the code in SQL in that tutorial with this (and adapt the table and column names) one:
SELECT * FROM USERS where name ='qwert' and pass = MD5('1234')
And take care at sanitizing your variables in order to avoid SQL injection attacks !
You need to use a where clause
Also you will need to specify limits on the query to restrict the result set to 1 record
$select = "SELECT * FROM usertable WHERE username = '$user' LIMIT 0, 1";
$query = mysql_query($select) or die(mysql_error());
$result = mysql_fetch_assoc($query);
//Prints the array
print_r($result);

Displaying the users information from the database

I am trying to created a way to call information from the database for a user to view. Such as they log in and it has their registered information viewed. I have this
session_start();
if($_SESSION['id'])
$result = mysql_query("SELECT * FROM User WHERE `id` = $_SESSION[id]")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo '<b>First Name:</b>' .$row['fname'];
echo '<br>';
echo '<b>Last Name:</b>' .$row['lname'];
}
but nothing shows up. My database name is megan, table is user, fields i want displayed are first name (fname) and last name (lname).
Can someone point me in the right direction. Thank you in advance!
Bad array indexing.
$result = mysql_query("SELECT * FROM User WHERE `id` = " . $_SESSION['id'])
You should turn on PHP error displaying.
First change this,
if(isset($_SESSION['id']))
because you have to check if the session isset correctly then do the query,
then the sql change to this,
$result = mysql_query("SELECT * FROM User WHERE `id` = ".$_SESSION['id'])

Pulling data and printing it in an HTML table

From a MySQL table called "submission" containing the fields "loginid, submissionid, title, url, datesubmitted, displayurl", I would like to print an HTML table thats contains all "title" and corresponding "datesubmitted" where "loginid" equals "$profile." The code I am trying to use is below. It isn't working. Any ideas why it isn't working?
Thanks in advance,
John
$profile = $_GET['profile'];
$sqlStr = "SELECT loginid, submissionid, title, url, datesubmitted, displayurl
FROM submission
WHERE loginid = $profile
ORDER BY datesubmitted DESC";
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"samplesrec\">";
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td class="sitename1">'.$row["title"].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="sitename2">'.$row["datesubmitted"].'</a></td>';
echo '</tr>';
}
echo "</table>";
Your query is probably failing.
Try echoing the return from mysql_error(); after trying the query to see what the issue might be.
You should also protect your input against injection. If loginID is a username, you need to surround a string in a mySQL query with quotes - if loginID is a username. If it's an integer you may be okay.
There are more robust ways to do this but simply:
$profile = mysql_real_escape_string($_GET['profile']);
$sqlStr = "SELECT loginid, submissionid, title, url, datesubmitted, displayurl
FROM submission
WHERE loginid = '$profile'
ORDER BY datesubmitted DESC";
$result = mysql_query($sqlStr);
if($result) {
// Handle output
}
else {
echo 'query failed';
// don't leave this here in production!
echo mysql_error();
}
One problem I can see is you are not checking in the return value of mysql_query()
mysql_query() returns false if it fails to execute the query. So you need to do a check, something like:
$result = mysql_query($sqlStr);
if(! $result) {
//....error occured...prepare $message
die($message);
}
your question regards to debugging, the most important programming art. Noone can find an error for you, you have to do it yourself. With help of little tricks.
change $profile = $_GET['profile']; to $profile = intval($_GET['profile'];)
change $result = mysql_query($sqlStr); to
$result = mysql_query($sqlStr) or trigger_error(mysql_error()." in ".$sqlStr);
andd following 2 lines at the top of your code, run it again and see what it say. if still nothing, you don't have matching records in your table.
ini_set('display_errors',1);
error_reporting(E_ALL);

Categories