Jump to content

[SOLVED] variables have no values when echo'd


thefollower

Recommended Posts

I have a code which is meant to load up the values from a SQL search yet when echo'd none appear even though the row in the database deffinatly exists =/

Cant figure out why...

 

include("include.php");

$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='$_SESSION[Current_User]'");
if (!($row = mysql_fetch_assoc($GetLetters))) {
header("Location: letterbox.php");
die;
}
Else{
include("energybarinclude.php");
$Subject = $row['Subject'];
$From = $row['Sender'];
$SentOn = $row['Senttime'];
$MessageOne = $row['MessageText'];

$FindUser1 = mysql_query("SELECT * FROM userregistration WHERE UserID='$From'");
//Fetch the row from the database
$rowuser = mysql_fetch_assoc($FindUser1);

$UserName1 = $rowuser['UserName'];

Link to comment
Share on other sites

<?php
//This is wrong
$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='$_SESSION[Current_User]'");
//try this instead (also no session_start();?)
$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='".$_SESSION['Current_User']."'");


include("energybarinclude.php");
$Subject = $row['Subject']; 
$From = $row['Sender'];//Where is this being set ? (from the include are they correct?)

?>

Link to comment
Share on other sites

<?php
//This is wrong
$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='$_SESSION[Current_User]'");
//try this instead (also no session_start();?)
$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='".$_SESSION['Current_User']."'");


include("energybarinclude.php");
$Subject = $row['Subject']; 
$From = $row['Sender'];//Where is this being set ? (from the include are they correct?)

?>

 

can you tell me why this is wrong

$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='$_SESSION[Current_User]'");

 

hmmm

 

i know what you mean that it array issue but since the array index is not inclose with '' i believe it will work without error

 

 

Link to comment
Share on other sites

<?
include("include.php");

$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='".$_SESSION['Current_User']."'");
if (!$GetLetters ){
header("Location: letterbox.php");
exit();
}
else{
include("energybarinclude.php");
$row = mysql_fetch_assoc($GetLetters);
$Subject = $row['Subject'];
$From = $row['Sender'];
$SentOn = $row['Senttime'];
$MessageOne = $row['MessageText'];
$FindUser1 = mysql_query("SELECT * FROM userregistration WHERE UserID='$From'");
$rowuser = mysql_fetch_assoc($FindUser1);

print_r($rowuser);
echo 'netxquery<br>';
print_r($rowuser);//to check wether you fetch somthing

 

try to check your queries

Link to comment
Share on other sites

<?php
//This is wrong
$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='$_SESSION[Current_User]'");
//try this instead (also no session_start();?)
$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='".$_SESSION['Current_User']."'");
?>

 

Both of those are perfectly valid.  I use the former all the time.

 

http://www.php.net/manual/en/language.types.array.php#language.types.array.donts

Link to comment
Share on other sites

Try this instead.

 

include("include.php");

$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='$_SESSION[Current_User]'");

$row = mysql_fetch_assoc($GetLetters) or die(mysql_error());

include("energybarinclude.php");
$Subject = $row['Subject'];
$From = $row['Sender'];
$SentOn = $row['Senttime'];
$MessageOne = $row['MessageText'];

$FindUser1 = mysql_query("SELECT * FROM userregistration WHERE UserID='$From'");
//Fetch the row from the database
$rowuser = mysql_fetch_assoc($FindUser1);

$UserName1 = $rowuser['UserName'];

Link to comment
Share on other sites

<?php
//This is wrong
$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='$_SESSION[Current_User]'");
//try this instead (also no session_start();?)
$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='".$_SESSION['Current_User']."'");
?>

 

Both of those are perfectly valid.  I use the former all the time.

 

http://www.php.net/manual/en/language.types.array.php#language.types.array.donts

 

Erm.. No they are NOT..

1. $_SESSION[Current_User] will cause a notice error (as Current_User is not a Const but PHP will use the value as a string), it works but is bad pratice..

 

2. an array can not be used in a sting unless it escape but {} or concatenated ie "test".array['blar']."er")

 

please review the referance you posted.

Why is $foo[bar] wrong?

 

You should always use quotes around a string literal array index. For example, use $foo['bar'] and not $foo[bar]. But why is $foo[bar] wrong? You might have seen the following syntax in old scripts:

 

and

// This will not work, results in a parse error such as:

// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'

// This of course applies to using superglobals in strings as well

print "Hello $arr['fruit']";

Link to comment
Share on other sites

yah i know its just that we are refering to the error of this tread and you site that part wich i think

it doesnt solve the issue

any way and any how

 

LISTEN to madtechi  ;D

<?php
//invalid
echo "test ".$teng[astig]; //but will work, but bad pratice
echo "test  $teng['astig']";

//valid
echo "test ".$teng['astig'];
echo "test {$teng["astig"]}";
echo "test {$teng['astig']}";
?>

Link to comment
Share on other sites

also need to findout

 

$From = $row['Sender'];//Where is this being set ? (from the include are they correct?)

 

think we need to wait for a post back lol

 

i think this should give some insight!

 

<?php
include("include.php");

$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='{$_SESSION['Current_User']}'") or die(mysql_error());
if (!($row = mysql_fetch_assoc($GetLetters))) {
header("Location: letterbox.php");
die;
}
Else{
include("energybarinclude.php");
$Subject = $row['Subject'];
$From = $row['Sender'];
$SentOn = $row['Senttime'];
$MessageOne = $row['MessageText'];

$FindUser1 = mysql_query("SELECT * FROM userregistration WHERE UserID='$From'") or die(mysql_error());
//Fetch the row from the database
$rowuser = mysql_fetch_assoc($FindUser1);

$UserName1 = $rowuser['UserName'];
?>

Link to comment
Share on other sites

I did not see in the original post where you actually define $row, but yet you are stating that $Subject=$row['Subject']; What row? Try this.

include("include.php");

$current_user=$_SESSION['Current_User'];

$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever=$current_user ");

$row=mysql_fetch_assoc($GetLetters);

if (!$row) {
header("Location: letterbox.php");
die;
}
Else{
include("energybarinclude.php");
$Subject = $row['Subject'];
$From = $row['Sender'];
$SentOn = $row['Senttime'];
$MessageOne = $row['MessageText'];

$FindUser1 = mysql_query("SELECT * FROM userregistration WHERE UserID='$From'");
//Fetch the row from the database
$rowuser = mysql_fetch_assoc($FindUser1);

$UserName1 = $rowuser['UserName'];

Link to comment
Share on other sites

i think we need to way for some more feed back from thefollower, i'll admit i missed the row being set but as i said, this should give us some better feedback

<?php
include("include.php");

$GetLetters = mysql_query("SELECT * FROM messages WHERE reciever='{$_SESSION['Current_User']}'") or die(mysql_error());
if (!($row = mysql_fetch_assoc($GetLetters))) {
header("Location: letterbox.php");
die;
}
Else{
include("energybarinclude.php");
$Subject = $row['Subject'];
$From = $row['Sender'];
$SentOn = $row['Senttime'];
$MessageOne = $row['MessageText'];

$FindUser1 = mysql_query("SELECT * FROM userregistration WHERE UserID='$From'") or die(mysql_error());
//Fetch the row from the database
$rowuser = mysql_fetch_assoc($FindUser1);

$UserName1 = $rowuser['UserName'];
?>

Link to comment
Share on other sites

MadTechie i just put your code and no values are echo'd

 

To answer your question earlier current_user is in my include its global, and is created as the user logs in. And it does 100% work otherwise it wouldn't allow me to access the page as i have put validation to log me out if there was a problem with it in the global include.

 

My echo's use short tags <?=$blah?> and yes they are turned on in my apache.

 

This is the code at current + echo's:

 

 

<?
include("include.php");

$GetLetters = mysql_query("SELECT * FROM messages WHERE Reciever='{$_SESSION['Current_User']}'") or die(mysql_error());
if (!($row = mysql_fetch_assoc($GetLetters))) {
header("Location: letterbox.php");
die;
}
Else{
include("energybarinclude.php");
$Subject = $row['Subject'];
$From = $row['Sender'];
$SentOn = $row['Senttime'];
$MessageOne = $row['MessageText'];

$FindUser1 = mysql_query("SELECT * FROM userregistration WHERE UserID='$From'") or die(mysql_error());
//Fetch the row from the database
$rowuser = mysql_fetch_assoc($FindUser1);

$UserName1 = $rowuser['UserName'];

}
?>
<div id="bv_" style="position:absolute;left:375px;top:468px;width:224px;height:128px;z-index:19" align="center">
<font style="font-size:13px" color="#000000" face="Arial"><?= $MessageOne ?></font></div>
<div id="bv_" style="position:absolute;left:182px;top:465px;width:150px;height:16px;z-index:22" align="left">
<font style="font-size:13px" color="#FFFFFF" face="Arial">Message:</font>
</br>
<font style="font-size:13px" color="#FFFFFF" face="Arial"><b>Subject: <?= $Subject ?><br>
<br><br>
From: <?=$From?>
<br>
Sent On:</br><?=$SentOn?>
<br>
<br>
Reply!</b></font></div></body>
</html>

 

 

Just to add, no errors occur, but it must be finding the row otherwise it would take me to the header("location: letterbox.php"); page.

I am wondering could the second include cause a problem if theres a header before it ? =/

Link to comment
Share on other sites

Try :-

<?php
include("include.php");

$GetLetters = mysql_query("SELECT * FROM messages WHERE Reciever='{$_SESSION['Current_User']}'") or die(mysql_error());
if (mysql_num_rows($GetLetters) == 0) {
header("Location: letterbox.php");
exit;
} else {
include("energybarinclude.php");
$row = mysql_fetch_assoc($GetLetters);
$Subject = $row['Subject'];
$From = $row['Sender'];
$SentOn = $row['Senttime'];
$MessageOne = $row['MessageText'];

$FindUser1 = mysql_query("SELECT * FROM userregistration WHERE UserID='$From'") or die(mysql_error());
//Fetch the row from the database
$rowuser = mysql_fetch_assoc($FindUser1);

$UserName1 = $rowuser['UserName'];

}
?>

 

You might want to use a JOIN instead of using another query to get the username

<?php
$GetLetters = mysql_query("SELECT UserID, Subject, Sender, Senttime, MessageText FROM messages INNER JOIN userregistration ON (messages.sender = userregistration.UserID) WHERE Reciever = '{$_SESSION['Current_User']}'";
?>

Link to comment
Share on other sites

$row=mysql_fetch_assoc($GetLetters); //Setting $row

if(!$row = mysql_fetch_assoc($getLetters)) { //Checking if $row is false

 

I did not see $row as defined or set since you used "!" in front if it.

 

if (!($row = mysql_fetch_assoc($GetLetters))) { like

if false or empty $row go to letterbox page? Your just asking and not setting with the if(! this is why you don't get a variable return echoed.

 

Link to comment
Share on other sites

$row=mysql_fetch_assoc($GetLetters); //Setting $row

if(!$row = mysql_fetch_assoc($getLetters)) { //Checking if $row is false

 

I did not see $row as defined or set since you used "!" in front if it.

 

if (!($row = mysql_fetch_assoc($GetLetters))) { like

if false or empty $row go to letterbox page? Your just asking and not setting with the if(! this is why you don't get a variable return echoed.

 

 

try testing it i can assure ya it does set it :P

Link to comment
Share on other sites

I think i got it!

 

Do not Capitalize

Else

Not valid

This is why you get no echo.

 

I would try this anyways

include("include.php");

$GetLetters = mysql_query("SELECT * FROM `messages` WHERE `reciever`='$_SESSION[Current_User]' AND `Subject`=Subject AND `Senttime`=Senttime AND `MessageText`=MessageText ");
$row=mysql_fetch_array(mysql_query($GetLetters));
if (!$row) {
header("Location: letterbox.php");
die;
}
else{
include("energybarinclude.php");
$Subject = $row['Subject'];
$From = $row['Sender'];
$SentOn = $row['Senttime'];
$MessageOne = $row['MessageText'];

$FindUser1 = mysql_query("SELECT * FROM userregistration WHERE UserID='$From'");
//Fetch the row from the database
$rowuser = mysql_fetch_assoc($FindUser1);

$UserName1 = $rowuser['UserName'];
$_SESSION['Current_User']

Link to comment
Share on other sites

I  cannot do the inner join because the Sender is up via ID but im trying to Echo the name of the user not the ID.. so it would have to be a separate sql query .. right?

 

You can grab what ever column you want once the tables are joined.

 

 

<?php
$GetLetters = mysql_query("SELECT Username, Subject, Sender, Senttime, MessageText FROM messages INNER JOIN userregistration ON (messages.sender = userregistration.UserID) WHERE Reciever = '{$_SESSION['Current_User']}'";

?>

Link to comment
Share on other sites

ok i tried the non capital e but made no difference with the else... it does run throufh the else as i put :

 

echo 'bwfsffsdfsd'; and it echo'd , ive never seen php be fussy over a capital E for else before... all my scripts have capital E and they work fine =/

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\readnewletters.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\readnewletters.php:5) in C:\xampp\htdocs\readnewletters.php on line 7

 

It won't allow the header for some reason even though your code has it encased in a if statement =/

 

Referring to monkeybidz's code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.