Jump to content

Archived

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

SephirGaine

One more time..

Recommended Posts

Alright. Lemme start by posting the code that I've got thus far.

[code]<?php
$query = "SELECT Tech FROM `WOR` where work_order_num='$ordernum'";
$techname = mysql_query($query) or die(mysql_error());
?>
<?php
$techinfo = "SELECT * FROM `Techs` where Name='$techname'";
$tech2 = mysql_query($techinfo) or die(mysql_error());
while ($tech3 = mysql_Fetch_assoc($tech2)) {
echo ''.$tech3['Name'].'<br>
'.$tech3['Phone'].'<br>
'.$tech3['Email'].'';
  }
?>[/code]

Basically, this is what I've got. I have two DB tables, one with the information about work orders (WOR), including a technician's name. I have a second table (Techs) that has the rest of their information. I have a form where you select a work order number, and from there it recalls all the information on that particular work order. I'm trying to assign the assigned tech to a variable ($techname), and then recall their information from the second table, and finally echo it onto the page.

This is how I thought it would work, however nothing is showing up when I try to run the script, so any help would be much appreciated.

Share this post


Link to post
Share on other sites
again
Table names and field names dont need single quotes (')
Who ever first mentioned this needs a slap

Some MySQL Versions make all field names, table names ect Lowercase

So try making Tech and WOR lower case (tech, wor)
Same as Techs
Name
Phone
Email

also add
echo mysql_error();
after a query, if there was an error, this will tell you

Share this post


Link to post
Share on other sites
You assign  the result of the first query to $techname. Then you use that variable in your second query.
After the first query $techname contains an MySql structure on which you have to perform a mysql_fetch(_assoc) in order to get the content of the Tech column.

Ronald  8)

Share this post


Link to post
Share on other sites
[quote author=onlyican link=topic=102782.msg408463#msg408463 date=1154557452]
again
Table names and field names dont need single quotes (')
Who ever first mentioned this needs a slap

Some MySQL Versions make all field names, table names ect Lowercase

So try making Tech and WOR lower case (tech, wor)
Same as Techs
Name
Phone
Email

also add
echo mysql_error();
after a query, if there was an error, this will tell you
[/quote]
again, onlyican, those are not single quotes. they're backticks (`)... please read up on this before slapping people.

Sephir, you can also try to join the two tables together
[code]SELECT Techs.Name FROM Techs JOIN WOR ON Techs.Name=WOR.Tech WHERE WOR.work_order_num='$ordernum'[/code]

Share this post


Link to post
Share on other sites
whats the difference between ' and ' ???
and sorry, I posted this before i read your other post

Share this post


Link to post
Share on other sites
the key on the left side of 1 and above Tab. it's a slightly tilted quote.

Share this post


Link to post
Share on other sites
Well, I revised my code a bit, however I'm still having trouble. Hopefully the code it a bit more clear now.

And [b]ryanlwh[/b] - unfortunately I've never attempted to combine tables before, so I'm not sure exactly what I'm looking at or what it really does. Not only that but I've no idea how to 'properly' apply that code, so I'd have to do some kind of research before trying it out. Thanks for the input, though, I'll definitely look into it.

[code]<?php
$query = "SELECT Tech FROM `WOR` where work_order_num=`$ordernum`";
$result = mysql_query($query) or die(mysql_error(1));
$techname = mysql_Fetch_assoc($result);
?>
<?php
$techinfo = "SELECT * FROM `Techs` where Name=`$techname`";
$result2 = mysql_query($techinfo) or die(mysql_error(1));
$text = mysql_Fetch_assoc($result2);
echo ''.$result.'<br>
'.$text['Phone'].'<br>
'.$text['Email'].'';

?>[/code]

Share this post


Link to post
Share on other sites
php is case sencative
mysql_fetch_assoc
not
mysql_Fetch_assoc
Although that should not really matter
try
echo $techinfo below the query
then copy that, and run the query in your query analyzer (standard is php myadmin)

Share this post


Link to post
Share on other sites
ok I see it now

$techname = mysql_Fetch_assoc($result);
should be

while($row = mysql_fetch_assoc($result)){
$techname = $row["fieldname"];
}

where fieldname is the name of the field from the database

Share this post


Link to post
Share on other sites
Sorry, deleted my post from before because it was a stupid mistake on my part.. however I tried that last bit out and unfortunately still no beans.

[code]<?php
$query = "SELECT Tech FROM WOR where work_order_num='$ordernum'";
$result = mysql_query($query) or die(mysql_error(1));
while ($row = mysql_fetch_assoc($result)){
$techname = $row["Name"];
}
?>
<?php
$techinfo = "SELECT * FROM Techs where Name='$techname'";
$result2 = mysql_query($techinfo) or die(mysql_error(1));
while ($text = mysql_fetch_assoc('$result2')) {
echo ''.$text['Name'].'<br>
'.$text['Phone'].'<br>
'.$text['Email'].'';
}
?>[/code]

I'm just getting kind of disgruntled however, since I've been working on this single piece of code for the past 2 days to no avail. Really, it [b]should[/b] be possible to do, correct? Thanks a ton for the help thus far, onlyican, it's much appreciated.

Share this post


Link to post
Share on other sites
while ($text = mysql_fetch_assoc('$result2')) {
Now, they are single quotes, not ticks yea
while ($text = mysql_fetch_assoc($result2)) {

Share this post


Link to post
Share on other sites
Aint gone to sleep honest
One thing to note, dont make a difference tho
echo ' ' .$text
just use
echo $text

Try this
under
$techinfo = "SELECT * FROM Techs...
add this code

echo "The Query is ".$techinfo."<br />";

This will show if all the variables are parsing ok, to find out if the first query is working proper

Share this post


Link to post
Share on other sites
yeah try to echo the queries to screen. you never know what you'd see :)

Share this post


Link to post
Share on other sites
Hey.. back, and once again, thanks for the help you two. I tried to echo my very first query, and something's wrong up there.. and unfortunately I can't figure out what.

[code]<?php
$query = "SELECT Tech FROM `WOR` WHERE work_order_num='$ordernum'";
$result = mysql_query($query) or die(mysql_error(1));
while ($row = mysql_fetch_assoc("$result"));
$techname = $row['Tech'];
echo "The Query is ".$techname."";
?>[/code]

The echo simply says "The Query is  " and that's it. I'm not sure if there's something wrong in the code or for some reason with accessing the database. However, I've got another piece of code on this same page that returns other sections of this same DB row without any problems. So.. I'm basically completely stumped. I've got the table entry's name correct, I see no glaring errors within my code.. it's just not cooperating it seems like. I've tried any given number of things, trying different variable names, changing the echo statement to some other variable ($row for example), and still nothing.  ???

Share this post


Link to post
Share on other sites
thats because the varible query is $query not $techname

echo "The query is ".$query."<br />";

Share this post


Link to post
Share on other sites
Also
[code]while ($row = mysql_fetch_assoc("$result"));[/code]
Don't EVER surround the result with quotes....

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.