Match Username (field from mysql table) To Session ID In PHP - php

How do i do it? I'v tried all different ways but I just cant do it!
Basically, im trying to pull out information from the "starters table" and display only the logged in users data.
Here is the code which gives me an error message in which I cannot solve:
<?php
session_start();
require_once '../database.php';
if (isset($_SESSION['myusername'])){
echo "Welcome ".$_SESSION['myusername'];
}
?>
<?php
include '../database.php';
$userid = $_SESSION["myusername"];
#the where clause is where im stuck at the moment!
Line 50:
$result = mysql_query("SELECT Recipename, Ingredients, Method, Time FROM starters WHERE username = $_SESSION['myusername']");
echo "<table border='0'><table border width=65%> <tr><th>Recipie Name</th><th>Ingredients</th><th>Method</th><th>Time</th></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Recipename']. "</td>";
echo "<td>" . $row['Ingredients']. "</td>";
echo "<td>" . $row['Method']. "</td>";
echo "<td>" . $row['Time']. 'minutes'."</td>";
echo "</tr>";
}
echo "</table>";
?>
</table>
the error message i get is the following:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/jahedhus/public_html/cook/usersloggedin/starters.php on line 50
line 50 is the select statement!
I would really appreciate your help,
Thanks so much!

You need to wrap the username in quotes:
$result = mysql_query("SELECT Recipename, Ingredients, Method, Time FROM starters WHERE username = '{$_SESSION['myusername']}'");
Additionally, you're trusting that $_SESSION['myusername'] is a valid username (not some string of attach SQL). If you don't know that data is safe, you need to at least escape the data.

Change the line:
$result = mysql_query("SELECT Recipename, Ingredients, Method, Time FROM starters WHERE username = $_SESSION['myusername']");
with this one:
$result = mysql_query("SELECT Recipename, Ingredients, Method, Time FROM starters WHERE username = '" . $_SESSION['myusername'] . "'");

Problem with the quotes is that they will get all gummed up, i'd seperate them either:
$result = mysql_query("SELECT Recipename, Ingredients, Method, Time FROM starters WHERE username = ".$_SESSION['myusername']);
OR
$result = mysql_query("SELECT Recipename, Ingredients, Method, Time FROM starters WHERE username = '$_SESSION[myusername]'");

Related

Show logged in user's data in profile.php

<?php
require('db.php');
$_SESSION['sloggedIn']="yes";
$data1['first_name'] = $_SESSION['sfirstname'];
$data1['email'] = $_SESSION['semail'];
$sqlQuery = "SELECT first_name, email FROM otium where id = :id";
file_put_contents('log/DBErrors.txt', 'Connection: '.'rivi8'.$sqlQuery."\n", FILE_APPEND);
$kysely1 = $DBH->prepare($sqlQuery);
$kysely1->execute($data1);
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysqli_fetch_array
//while($row = mysqli_fetch_array($rs)) {
// Write the value of the column FirstName (which is now in the array $row)
echo $sqlQuery['first_name'] . "<br />";
echo $sqlQuery['email'] . "<br />";
//}
?>
First time doing this php and i would like to print out user's logged in data, i have this code but is not working! please help
$sqlQuery is just string variable containing query, you can't expect it to be a array.
I guess you already got firstName and email from session then there is no need of executing sql query, just printing the session will make your job done.
echo $_SESSION['first_name'] . "<br />";
echo $_SESSION['email'] . "<br />";

getting warning while fetching rows from mysql using php

It seems to be duplicate question, but I tried all the answers, which has been already posted.
this is my code
include"db.php";
//$poster_id=1;
$sql = "SELECT pi.full_name,pi.poster_img,pi.poster_tag,
p.post_title,p.post_description,p.post_tag,
p.post_snap
FROM POSTS p JOIN POSTER_INFO pi ON p.poster_id=pi.auth_id";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_error();
$msg=" ";
while($row = mysql_fetch_array($result)) {
$msg_footer="<div class='post_footer'>";
$msg_footer=$msg_footer."<div class='post_img'>";
$msg_footer=$msg_footer."<img src='". 'data:image/jpeg;base64,' . base64_encode( $row[6] ) ."' width='330' height='130' /></div>";
$msg_footer=$msg_footer."<div class='post_author'><div class='author_img'><img src='". 'data:image/jpeg;base64,' . base64_encode( $row[1] ) ."' width='80' height='80' /></div>";
$msg_footer=$msg_footer."<div class='author_info'><b>". $row[0] ."</b><br/><span style='font-size:11px'>". $row[2] ."</span><br/><span style='font-size:10px'>1 Post</span></div></div></div>";
$msg .= " <li><br/><span class='post_title'>". $row[3] ."</span><br/><br/><span class='post_description'>" . $row[4] . "</span></li>";
$msg .=$msg_footer; //WARNING POINTING TO THIS LINE
}
I'm getting warning, which pointing last line of while loop;
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\labs\load_data.php on line 40
My sql query is correct, because, I tried to execute it in sql editor and in php page.
like this
<?php
$sql = "SELECT pi.full_name,pi.poster_img,pi.poster_tag,
p.post_title,p.post_description,p.post_tag,
p.post_snap
FROM POSTS p JOIN POSTER_INFO pi ON p.poster_id=pi.auth_id";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_error();
while($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row[0]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
</tr>
<?php }
?>
In this case, I'm not getting any warning.
I'm pretty sure that the error is not triggered by the code that you have posted.
Check the return value of mysql_query() at all positions in the code, like this:
if(mysql_query(...) === FALSE) {
die(mysql_error());
}
I see you are already doing it in the code you have shown, but the warning will be triggered at another point in the code I assume.
Another explanation might be, that you overwrite $result somewhere in the code - but this is not shown as well.
General advice: Don't use mysql_* functions for new code. They are deprecated. Use PDO or mysqli_* instead
I think your query is failing, so producing a boolean FALSE instead of a query resource. But it's very strange that the second one runs fine..

php link from mysql

I am revisiting php and mySQL from a long time off.
mysql_select_db($database_conn, $conn);
$query = sprintf("SELECT DISTINCT Username FROM Entries ");
$result = mysql_query($query);
$row_results = mysql_fetch_assoc($result);
<?php do {
//$name = $row_results['Username'];
//echo $name, "<br/>";
echo '<a href=\"$row_results['Username'],.php\">';
echo '$row_results['Username'],'s overview </a><br/>';
}
while ($row_results = mysql_fetch_assoc($result)); ?>
What I am trying to do is echo out the link from the results the link is in the form username.php the username is stored in the database.
I have used single quotes and double quotes with escaped /" in but get different errors I know its going to be something as simple as a ; or " .
If you could be as kind to explane what is wrong abd if there is a better way to do this?
The query is correct and the commented out code also works on its own.
Thanks
You cannot parse variables through single quotes:
echo '<a href=\"$row_results['Username'],.php\">';
use
echo '<a href="'.$row_results['Username'].'">';
mysql_select_db($database_conn, $conn);
$query = sprintf("SELECT DISTINCT Username FROM Entries ");
$result = mysql_query($query);
$row_results = mysql_fetch_assoc($result);
<?php do {
//$name = $row_results['Username'];
//echo $name, "<br/>";
echo '<a href="'.$row_results['Username'].'php">';
echo $row_results['Username'].'\'s overview </a><br/>';
}
while ($row_results = mysql_fetch_assoc($result)); ?>
Looks to me like the concatenation of your html string and the username is incorrect.
Currently, your string that you are writing to the page is
<a href=\"$row_results['Username'],.php\">
What you need to be doing is joining the html with the value coming out from the database.
This can be done like so:
echo '<a href=\"'.$row_results['Username'].'.php\">';
echo $row_results['Username'].'\'s overview </a><br/>';
Notice the '.' to concatenate two potions of the string, and the escapement of the apostrophe in the 's
Here are your errors :
Even though you are using DISTINCT in your SQL Query, there might be multiple entries, so you must use a while loop.
You had a comma in there (probably by typo).
You need to escaping the php variable.
So after doing the above, this is what it should be.
while ($row_results = mysql_fetch_assoc($result)) {
echo '<a href="' .$row_results["Username"]. '.php">';
}
try:
echo '<a href="'.$row_results['Username'].'.php">';
echo $row_results['Username'].'\'s overview </a><br/>';
Seems like you got pretty mixed up with the " and ' there. You should really avoid using anything other than ' - it will only bite you on the long run.
Also: You concatenate with ., not with ,.
mysql_select_db($database_conn, $conn);
$query = sprintf('SELECT DISTINCT Username FROM Entries ');
$result = mysql_query($query);
$row_results = mysql_fetch_assoc($result);
do {
$name = $row_results['Username'];
echo $name.'<br/>';
echo '<a href="'.$name.'.php">';
echo $name.'\'s overview </a><br/>';
} while ($row_results = mysql_fetch_assoc($result)); ?>

How could I show only selected row in PHP?

I'm working at this thing that takes information from a MySQL database that comes from a contact form and displays it all to the user. Well, I got that far without issues using:
$result = mysql_query("SELECT * FROM contact");
while($row = mysql_fetch_array($result))
{
echo $row['name'] . "<br/>" . " " . $row['email'] . "";
echo "<br/>";
echo $row['message'];
echo "<br />";
}
Now I would like to add a button next to each item that would single them out, basically taking me to a page where only the selected row shows up, not all of them, as happens on this page.
The thing is that i don't know how to do that. So, does anyone have any ideas?
Thanks!
Add a button that refreshes the page with a "GET" parameter, such as yourpage.php?uid=123; Then do
if ($_GET['uid'] !== null) {
$id = (int) $_GET['uid'];
$result = mysql_query('SELECT * FROM contact WHERE id='.$id);
...
}

Problem with a loop inside a PHP SQL statement

Here is my code, part of it is working flawlessly while the other isn't.
<?php
$query = "Select * from Query ORDER BY time DESC";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
?>
<span class="hotspot" onmouseover="tooltip.show('<center><b><?php echo $row['name'] ?></b></center>');" onmouseout="tooltip.hide();">
<?php
echo "<img src='/" . $row['name'] . ".gif'> ";
}
?>
Now there is like 100+ rows, the second $row['name'] is working fine with the loop, but the first one is using the First rwos result for every result.Any Solution?
have you tried assigning $row['name'] to a variable then use the variable to build the HTML?
I'm not sure if this matters but you may want to put a semicolon in your first piece of echo code:
<?php echo $row['name']; ?>
I don't spot any obvious errors-- try adding one line code so it echos the $row['name'] earlier in the loop, which might help you understand better what's going on. See below:
<?php
$query = "Select * from Query ORDER BY time DESC";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo $row['name']."<br>"; // <-------- ADD THIS LINE
?>
<span class="hotspot" onmouseover="tooltip.show('<center><b><?php echo $row['name'] ?></b></center>');" onmouseout="tooltip.hide();">
<?php
echo "<img src='/" . $row['name'] . ".gif'> ";
}
?>
I found the problem.
The end </span> was after the loop.

Categories