Jump to content

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


SephirGaine

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.
Link to comment
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.
Link to comment
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]
Link to comment
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.
Link to comment
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]
Link to comment
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.
Link to comment
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.
Link to comment
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.
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.