Jump to content

Archived

This topic is now archived and is closed to further replies.

SephirGaine

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

Recommended Posts

[code]<?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'].'';
  }
?>[/code]

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..

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

I get the following error:

[quote]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[/quote]

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

Share this post


Link to post
Share on other sites
just do this
[code]$EmpName = $text['name'];[/code]

When you're assigning variables directly to another, quotes are not needed.

Share this post


Link to post
Share on other sites
You don't need to add the "'. when using PHP variables.

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

I hope you defined the variables before echoing them out.

Share this post


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

Share this post


Link to post
Share on other sites
Hm.. my query dies when I try to insert the information.. not too sure why? Any help would be appreciated..

[code]<?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.");
?>[/code]

Share this post


Link to post
Share on other sites
You query dies because you can't have WHERE inside an INSERT statement... if you want to update, use UPDATE.

Share this post


Link to post
Share on other sites
Since your INSERT failed, another thought: are you sure that all db columns are character type,
because you include them all in single quotes?

Share this post


Link to post
Share on other sites
[quote author=ronverdonk link=topic=103021.msg409870#msg409870 date=1154731029]
Since your INSERT failed, another thought: are you sure that all db columns are character type,
because you include them all in single quotes?
[/quote]
mysql will convert them to the appropriate type.

Share this post


Link to post
Share on other sites
[quote author=SephirGaine link=topic=103021.msg409864#msg409864 date=1154730648]
Hm.. my query dies when I try to insert the information.. not too sure why?[/quote]

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.

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

Share this post


Link to post
Share on other sites
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..

[code]<?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");
?>[/code]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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..

[code]UPDATE `WOR` SET EmpName='',EmpPhone='',EmpEmail='' where work_order_num='24'[/code]

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..

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

That.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
And that's why you guys rock. Changed it with IF, and it works [b]flawlessly[/b]. 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

Share this post


Link to post
Share on other sites

×

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.