Hello all! I have what I’m going to guess is a REALLY simple PHP/MySQL coding question that I’m just stumped on. I’ll warn you that this is going to be kind of long, but I want to provide as much info about what I’m trying to do so you guys have all the info to help me out.
I’m in the process of re-writing some old PHP code. The current code is being used in an old CMS that I don’t want to use anymore. I’m re-writing it so it just straight PHP and I can put it anywhere. Anyway…
In my SQL statement I have two tables I’m doing a LEFT JOIN on. The tables are called trooptrack_costumes and trooptrack_memcostumes. Here is what they look like:
The costumes table is just a list of costumes that is used on a form where an admin can pick the costumes a person has. Since a member can have more than one, I made the memcostumes table which stores the member id (tkid) and the costumeid of the costumes they have. So, the values memcostumes.costumeid should match up with a record in costumes.id.
In my PHP code I’m running the following SQL statement:
In this statement, $id is a variable I declared at the top of the code as well as $conn. I’m using those same variables for other queries on this page and they all work fine (that is why this one is $sql7). If I run this statement in phpMyAdmin I get the results I expect. It looks something like this:
In the tkid column I get NULL values unless there is a record for the chosen TKID in the memcostumes table. You will see there are a couple of rows here with '00001' in the tkid field. These are the ones that match up in memcostumes.
The problem is when I try to use any of the results later in my PHP code. I'm trying to do something like this in a form:
Any thoughts as to what I may be doing wrong? It acts like there are no results of the query. I'm pretty sure my SQL syntax is right, as it works in phpMyAdmin. I'm just not sure what I'm missing. Any help would be greatly appreciated!
I’m in the process of re-writing some old PHP code. The current code is being used in an old CMS that I don’t want to use anymore. I’m re-writing it so it just straight PHP and I can put it anywhere. Anyway…
In my SQL statement I have two tables I’m doing a LEFT JOIN on. The tables are called trooptrack_costumes and trooptrack_memcostumes. Here is what they look like:
Code:
Trooptrack_costumes
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| designation | char(2) | NO | | | |
| costume | varchar(30) | NO | | | |
| hidden | tinyint(1) | NO | | 0 | |
| sort | tinyint(1) | NO | | 0 | |
+-------------+-------------+------+-----+---------+----------------+
Code:
trooptrack_memcostumes
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| tkid | char(5) | NO | | 00000 | |
| costumeid | int(11) | NO | | 0 | |
+-----------+---------+------+-----+---------+----------------+
The costumes table is just a list of costumes that is used on a form where an admin can pick the costumes a person has. Since a member can have more than one, I made the memcostumes table which stores the member id (tkid) and the costumeid of the costumes they have. So, the values memcostumes.costumeid should match up with a record in costumes.id.
In my PHP code I’m running the following SQL statement:
Code:
$sql7 = 'SELECT trooptrack_costumes.id, trooptrack_memcostumes.tkid, designation, costume, sort FROM trooptrack_costumes LEFT JOIN trooptrack_memcostumes ON trooptrack_costumes.id = trooptrack_memcostumes.costumeid AND trooptrack_memcostumes.tkid = '.$id.' ORDER BY sort, designation';
$result7 = mysqli_fetch_array($conn, $sql7);
In this statement, $id is a variable I declared at the top of the code as well as $conn. I’m using those same variables for other queries on this page and they all work fine (that is why this one is $sql7). If I run this statement in phpMyAdmin I get the results I expect. It looks something like this:
In the tkid column I get NULL values unless there is a record for the chosen TKID in the memcostumes table. You will see there are a couple of rows here with '00001' in the tkid field. These are the ones that match up in memcostumes.
The problem is when I try to use any of the results later in my PHP code. I'm trying to do something like this in a form:
Code:
<TD>
<?php
while ($row7=mysqli_fetch_array($result7,MYSQLI_ASSOC)) {
if ($row7["tkid"] == NULL) { ?>
<input type="checkbox" name="costumes[]" value="<?php echo $row7["id"]; ?>"> <?php echo $row7["designation"]; ?> - <?php echo $row7["costume"]; ?>
<BR>
<?php }
else { ?>
<input type="checkbox" name="costumes[]" value="<?php echo $row7["id"]; ?>" checked> <?php echo $row7["designation"]; ?> - <?php echo $row7["costume"]; ?><BR>
<?php } } ?>
</TD>
Any thoughts as to what I may be doing wrong? It acts like there are no results of the query. I'm pretty sure my SQL syntax is right, as it works in phpMyAdmin. I'm just not sure what I'm missing. Any help would be greatly appreciated!