Assigning value to variable doesn't work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
berto
Forum Newbie
Posts: 2
Joined: Mon Nov 14, 2005 4:54 pm

Assigning value to variable doesn't work

Post 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]
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
berto
Forum Newbie
Posts: 2
Joined: Mon Nov 14, 2005 4:54 pm

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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
Post Reply