I have script that is almost done, I just need
help with one more thing.

I'll describe the relevant structure the best I can, then explain what I need.

TABLE carlot
FIELDS trackpt, id

TABLE cars
FIELD userid, carid

TABLE grpusers
FIELD id

Field id from table carlot = field carid from table cars
Field userid from table cars = field id from table grpusers

The value I need to display is in the field trackpt, what PHP code do I need to get this value (associated with the userid).

What I am trying to do is add that value to other values in the grpusers table.

Any suggestions? I'm sure this isn't difficult for people that work with php and mysql a lot but this is day 3 for me with no formal training and I'm basically tweaking code to make this work.

Any help is greatly appreciated!

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

Presumably you want an inner join?

Let's assume the table carlot is:

+---------+----+
| trackpt | id |
+---------+----+
| blah    |  1 | 
| wah     |  2 | 
| woot    |  3 | 
+---------+----+

And the table cars is:

+--------+-------+
| userid | carid |
+--------+-------+
|      1 |     3 | 
|      2 |     5 | 
|      3 |     5 | 
+--------+-------+

Now you want to link the two by carlot(id) and cars(userid).

Thus...

mysql> SELECT trackpt, carid FROM carlot
    -> INNER JOIN cars ON carlot.id = cars.userid;
+---------+-------+
| trackpt | carid |
+---------+-------+
| blah    |     3 | 
| wah     |     5 | 
| woot    |     5 | 
+---------+-------+
3 rows in set (0.01 sec)

Thank you very much, you make it look so easy.

I get this:
Parse error: syntax error, unexpected T_STRING in /home/l1376/public_html/folder/citizens.php on line 4

Line 4 is/was:
mysql> SELECT trackpt, carid FROM carlot

Also, once this is working what code is needed to use 'trackpt' in an equation? Such as HP + trackpt = $challengerpts

Thank you so much...

Member Avatar for iamthwee

Thank you very much, you make it look so easy.

I get this:
Parse error: syntax error, unexpected T_STRING in /home/l1376/public_html/folder/citizens.php on line 4

Line 4 is/was:
mysql> SELECT trackpt, carid FROM carlot

Also, once this is working what code is needed to use 'trackpt' in an equation? Such as HP + trackpt = $challengerpts

Thank you so much...

Try:-

<?php
$conn = mysql_connect("localhost","username","yourpassword");
//pick the database to use
mysql_select_db("test",$conn);
//create sql statement
$sql = "SELECT trackpt, carid FROM carlot INNER JOIN cars ON carlot.id = cars.userid";
//execute statement
$result = mysql_query($sql, $conn) or die(mysql_error());
//go through each row in the result set and display data
while ( $newArray = mysql_fetch_array($result) )
{
  //give a name to the fields
  $id = $newArray['trackpt'];
  $testField = $newArray['carid'];
  //echo the results to screen
  echo " $id $testField <br>";
}
?>

Obviously replacing username password with your actual username and password.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.