Jump to content


Photo

Setting a variable to DB string results.. (solved)


  • Please log in to reply
14 replies to this topic

#1 SephirGaine

SephirGaine
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 04 August 2006 - 08:14 PM

<?php
	$query = "SELECT * FROM `EMPLOYEES` where loginName='$logname'";
		$result = mysql_query($query) or die(mysql_error());
		while ($text = mysql_Fetch_assoc($result)) {
			echo ''.$text['name'].'<br>
			'.$text['phone'].'<br>
			'.$text['email'].'';
  }
?>

Hopefully it's fairly simple.. however, I need to assign variables to each of the results that are echoed. Name, Phone, and Email. However.. what I'm trying does not work..

<?php
$EmpName = "'.$text['name'].'";
$EmpPhone = "'.$text['phone'].'";
$EmpEmail = "'.$text['email'].'";
?>

I get the following error:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /homepages/28/d152867434/htdocs/work-request/index.php on line 94


Line 94 is where I attempt to set the $EmpName variable.

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 04 August 2006 - 08:19 PM

just do this
$EmpName = $text['name'];

When you're assigning variables directly to another, quotes are not needed.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 04 August 2006 - 08:22 PM

You don't need to add the "'. when using PHP variables.

echo "$text[name]<br>
			$text[phone]<br>
			$text[email]";
<?php
$EmpName = $text[name];
$EmpPhone = $text[phone];
$EmpEmail = $text[email];
?>

I hope you defined the variables before echoing them out.
PHP Ninja

#4 SephirGaine

SephirGaine
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 04 August 2006 - 08:25 PM

Yeah.. they show up fine when I echo them, was just wondering how I could assign the results to variables. Easy enough. Thanks!

#5 SephirGaine

SephirGaine
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 04 August 2006 - 10:30 PM

Hm.. my query dies when I try to insert the information.. not too sure why? Any help would be appreciated..

<?php
	$query = "SELECT * FROM `EMPLOYEES` where loginName='$logname'";
		$result = mysql_query($query) or die(mysql_error());
		while ($text = mysql_Fetch_assoc($result)) {
			echo ''.$text['name'].'<br>
			'.$text['phone'].'<br>
			'.$text['email'].'';
  }
?>
<?php
$EmpName = $text['name'];
$EmpPhone = $text['phone'];
$EmpEmail = $text['email'];
?>

<?php
	$query = "SELECT * FROM `Techs` where Name='$Tech'";
		$result = mysql_query($query) or die(mysql_error());
		while ($text = mysql_fetch_assoc($result)) {
			echo ''.$text['Name'].'<br>
			'.$text['Phone'].'<br>
			'.$text['Email'].'';
			}
?>
<?php
$TechPhone = $text['Phone'];
$TechEmail = $text['Email'];
?>
<?php
	$insert_info = "INSERT into `WOR` where work_order_num='$work_order_num' (EmpName,EmpPhone,EmpEmail,TechPhone,TechEmail)
		VALUES ('$EmpName','$EmpPhone','$EmpEmail','$TechPhone','$TechEmail')";
	mysql_query($insert_info) or die("Unable to submit query insert_info.");
?>


#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 04 August 2006 - 10:35 PM

You query dies because you can't have WHERE inside an INSERT statement... if you want to update, use UPDATE.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#7 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 04 August 2006 - 10:37 PM

Since your INSERT failed, another thought: are you sure that all db columns are character type,
because you include them all in single quotes?
RTFM is an almost extinct art form, it should be subsidized.

#8 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 04 August 2006 - 10:38 PM

Since your INSERT failed, another thought: are you sure that all db columns are character type,
because you include them all in single quotes?

mysql will convert them to the appropriate type.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#9 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 04 August 2006 - 10:38 PM

Hm.. my query dies when I try to insert the information.. not too sure why?


You're not sure why because your error message is useless. For future reference, use the variant below. At least you'll know just what the problem is.

$result = mysql_query($insert_info) or die("Error ". mysql_error(). " with query ". $insert_info);

Legend has it that reading the manual never killed anyone.
My site

#10 SephirGaine

SephirGaine
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 04 August 2006 - 11:12 PM

Alright. I changed INSERT with UPDATE, and added an error report that actually reports something. So, thanks for the tips thus far -- just a shame I kept forgetting to do so beforehand. I managed to run the query in phpmyadmin and it worked successfully, however it still is not seeing the variables it seems, as when I substituted my variables in my query with regular text (put in Test1, Test2, Test3) it worked perfect. So at least my query isn't jacked up anymore, so I guess that's a semi-good sign..

<?php
	$query = "SELECT * FROM `EMPLOYEES` where loginName='$logname'";
		$result = mysql_query($query) or die("Error ". mysql_error(). " with query empinfo");;
		while ($text = mysql_Fetch_assoc($result)) {
			echo ''.$text['name'].'<br>
			'.$text['phone'].'<br>
			'.$text['email'].'';
  }
?>
<?php
$EmpName = $text['name'];
$EmpPhone = $text['phone'];
$EmpEmail = $text['email'];
?>

<?php
	$query = "SELECT * FROM `Techs` where Name='$Tech'";
		$result = mysql_query($query) or die("Error ". mysql_error(). " with query techinfo");
		while ($text = mysql_fetch_assoc($result)) {
			echo ''.$text['Name'].'<br>
			'.$text['Phone'].'<br>
			'.$text['Email'].'';
			}
?>
<?php
$TechPhone = $text['Phone'];
$TechEmail = $text['Email'];
?>

<?php
	$query2 = "UPDATE `WOR` SET EmpName='$EmpName',EmpPhone='$EmpPhone',EmpEmail='$EmpEmail' where work_order_num='$work_order_num'";
		mysql_query($query2) or die("Error ". mysql_error(). " with query query2");
?>

That's what I've got. All of the Employee and Tech information echo correctly, but it appears that I messed up when trying to assign variables to them. So, I guess this goes right back to my original post.. but at least there's a light at the end of the tunnel, more or less. Hopefully I can stop bugging you guys soon.  ;) Thanks a crapload for the help thus far, much appreciated per usual. I'll have to drop a donation to the site sometime, love the assistance I've gotten from you fine folk.

EDIT: Oh, and I took out $TechPhone and $TechEmail out of my query for now, until I can get this knocked out, then I'll place them back in.

#11 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 04 August 2006 - 11:18 PM

all you goto do is output the query and then debug the problam that easy

print_r($query2);

then see whats missing i guess that you took the tecphone and tecemail out off the loop but debug ok.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#12 SephirGaine

SephirGaine
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 04 August 2006 - 11:20 PM

Ah, that's just the thing though, all three of the mentioned queries are working perfectly, it's when I'm setting the variables where something gets messed up. I'll give it a shot nontheless and see if anything turns up.

EDIT: Yeah.. this is what it prints..

UPDATE `WOR` SET EmpName='',EmpPhone='',EmpEmail='' where work_order_num='24'

Which is pretty much what it should be. It recognizes the $work_order_num variable, but the rest have nothing -- so the problem is 99% going to be somewhere in..

<?php
$EmpName = $text['name'];
$EmpPhone = $text['phone'];
$EmpEmail = $text['email'];
?>

That.

#13 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 04 August 2006 - 11:21 PM

ok mate where see
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#14 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 04 August 2006 - 11:25 PM

ok, you'll not get anything if you set the variables after the while loop... why? because $text becomes "false" (thus not an array) once the loop exits.

if there is only one row in the result set, either replace "while" with "if", or omit the check.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#15 SephirGaine

SephirGaine
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 04 August 2006 - 11:30 PM

And that's why you guys rock. Changed it with IF, and it works flawlessly. Gotta love it. Thanks a ton for the help! Think I can finally stop making topics, since my real major project on the site is working just fine!  ;D ;D ;D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users