Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue retrieving value using mysql fetch row
    primarykey
    data
    text
    <p>I am having a problem returning a specific value from a mysql table i have. The code seems to be failing at the:</p> <pre><code>$role = $row[7]; </code></pre> <p>line. I've tested this using echo.</p> <p>My function looks like:</p> <pre><code>function fetchRole(){ $query = "SELECT * FROM membersTable WHERE username='$user'"; $result = queryMysql($query); $row = mysql_fetch_row($result); $role = $row[7]; switch ($role) { case "HeadCoach": $role = 'Head Coach'; break; case "Coach": $role = 'Team Coach'; break; case "Science": $role = 'Sport Scientist'; break; case "Strength": $role = 'Strength &amp; Conditioning'; break; case "Physio": $role = 'Physiotherapist'; break; case "GK": $role = 'Goalkeeper'; break; case "FB": $role = 'Full-Back'; break; case "LB": $role = 'Left-Back'; break; case "CB": $role = 'Centre-Back'; break; case "RB": $role = 'Right-Back'; break; case "LM": $role = 'Left-Midfielder'; break; case "CM": $role = 'Central-Midfielder'; break; case "RM": $role = 'Right-Midfielder'; break; case "AML": $role = 'Attacking-Midfielder (Left)'; break; case "AMC": $role = 'Attacking-Midfielder (Central)'; break; case "AMR": $role = 'Attacking-Midfielder (Right)'; break; case "WF": $role = 'Wide Forward'; break; case "CF": $role = 'Centre Forward'; break; default: $role = 'Unknown'; } return $role; } </code></pre> <p>The <code>membersTable</code> schema looks like this:</p> <pre><code>id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstName VARCHAR(16), lastName VARCHAR(16), dob DATE, height CHAR(3), weight CHAR(3), type CHAR(1), position VARCHAR(16), teamName VARCHAR(32), status VARCHAR(16), username VARCHAR(16), pass VARCHAR(16), INDEX(firstName(6)), INDEX(lastName(6)) </code></pre> <p>I queryMysql is a function that i know works from other areas on my site.</p> <p>Currently echoing $row prints out RESOURCE #19.</p> <p>This previous function works:</p> <pre><code>function generateStafflist() </code></pre> <p>{</p> <pre><code>echo "&lt;h3&gt;Team Staff&lt;/h3&gt;"; $positionArray = array('HeadCoach','Coach','Science','Strength','Physio'); $query = "SELECT * FROM membersTable WHERE type='S'"; $result = queryMysql($query); $rows = mysql_num_rows($result); $count = count($positionArray); for($i = 0; $i &lt; $count; ++$i) { $result = queryMysql($query); for($j = 0; $j &lt; $rows; ++$j) { $row = mysql_fetch_row($result); $position = $row[7]; $currentPosition = $positionArray[$i]; if($position == $currentPosition) { echo "&lt;div class='snapshot'&gt;"; if(file_exists($row[9] . ".jpg")) { echo "&lt;img src='".$row[9].".jpg' height='210' width='180' /&gt;"; } else { echo "&lt;img src='imgs/profiles/noimage.png' height='210' width='180' /&gt;"; } echo $row[1] . " " . $row[2] . "&lt;br /&gt;"; switch($row[7]) { case "HeadCoach": $role = 'Head Coach'; break; case "Coach": $role = 'Team Coach'; break; case "Science": $role = 'Sport Scientist'; break; case "Strength": $role = 'Strength &amp; Conditioning'; break; case "Physio": $role = 'Physiotherapist'; break; default: $role = ''; } echo '[' . $role . ']&lt;br /&gt;'; echo '&lt;/div&gt;'; } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload