Page 1 of 1

Assigning value to variable doesn't work

Posted: Mon Nov 14, 2005 5:06 pm
by berto
Hello guys,
I have very strange problem. Here is my php code

Code: Select all

<? // profil.php

include("include/common.php");
include("include/db.php");

if (!isset($_POST['submitok'])):
    // Display the user profile

$email = $_SESSION['email'];
$haslo = $_SESSION['haslo'];

dbConnect("etcg_etcg");
$sql = "SELECT * FROM users WHERE 
    email = '$email' AND haslo = PASSWORD('$haslo')";
$result = mysql_query($sql);
if (!$result) {
  error('error');
    exit;
}

$name = mysql_result($result,0,'name');
echo "Hello : ", $name;
mysql_close();

?>
<center>
<h4>Twoj Profil: </h4>
<form method="post" action="index.php?action=Profil"> 
<table cellspacing="0" cellpadding="3" align="center" border="1" bordercolor="#333333" bgcolor="#272424">
<tr><td align="right" width="98">
<td align="right" width="98"><b>Nick:</b> </td><td><input type="text" name="nick" value="<?=$nick?>"></td></tr>
<tr><td align="right"><b>Imiê:</b> </td><td><input type="text" name="name" value="<?=$name?>"></td>
<tr><td colspan="2"><center><b>Pogrubienie</b> oznacza pola obowi±zkowe!</center></td>
<td colspan="2"><center><input type="Submit" name="submitok" value=" ZMIEN "></center></td>
</td></tr>
</table>
</form>
</center>
<?
endif;
?>
For some reason assignments: $name = mysql_result($result,0,'name'); doesn't work at all. I received on the screen nothing. When I changed this as follows:

Code: Select all

$$name = mysql_result($result,0,'name');
echo "Hello : ", $$name;
It works and displayes variable name correctly, but in form displays nothing.

Please help guys!

Burrito: Please use

Code: Select all

tags when [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting PHP Code In The Forums[/url][/size]

Posted: Mon Nov 14, 2005 5:11 pm
by twigletmac
Are short tags enabled on the server? Do you get any difference if you change:

Code: Select all

<?=$name?>
to

Code: Select all

<?php echo $name; ?>
Mac

Posted: Mon Nov 14, 2005 11:45 pm
by berto
I've tried both. But even simple echo $name doesn't work!

When I use in form whole function

Code: Select all

<?=mysql_query($sql)?>
Then it works. But question is why sinle $ doesn't work and double works?

Posted: Tue Nov 15, 2005 6:01 am
by twigletmac
That seems very strange (and I must admit I thought it was a typo - sorry), that variable variable assignment would work but not normal variable assignment.

Out of interest, does the following code work any different:

Code: Select all

$sql = "SELECT name FROM users WHERE email = '$email' AND haslo = PASSWORD('$haslo')";
	
	$result = mysql_query($sql);

	if (!$result) {
		error('error');
	    exit;
	}

	$row = mysql_fetch_assoc($result);
	$name = $row['name'];

	echo 'Hello : ', $name;
	mysql_close();
Mac

Posted: Tue Nov 15, 2005 6:15 am
by jmut
Maybe you use sessions without session_start() first....hence null value from $_SESSION[...]
put error_reporting(E_ALL) at top of the script.
It will all come out I think.
Enjoy