NeverPhased
NeverPhased

Reputation: 1566

PHP/MYSQL: Unsure why variable does not contain a value

Can someone please explain why this code is not outputting a value for $consultant When the data base is as follows:

enter image description here

And below is the code...

<?php include ("include/config.php");


$SUC = mysql_query("

    SELECT `decisionValue` FROM `teamdecision` WHERE `decisionType` = 'SUconsultant'

")or die($SUC."<br/><br/>".mysql_error());

$SUNumR  = mysql_num_rows($SUC);
$consultant = array();

$i="0";

while ($i<$SUNumR && $row = mysql_fetch_assoc($SUC))
{

    $consultant[$i] = $row['SUconsultant'];
    echo $consultant[$i];
    $i++;

}
?>

Thanks

Upvotes: 1

Views: 102

Answers (4)

spidEY
spidEY

Reputation: 944

In addition to other answers, The $i counter in your script is completely unnecessary. Here's a much simpler approach:

<?php 
include('include/config.php');

$SUC = mysql_query("SELECT decisionValue FROM teamdecision WHERE decisionType = 'SUconsultant'") or die(mysql_error());

$consultant = array();

while ($row = mysql_fetch_assoc($SUC))
{
    echo $consultant[] = $row['decisionValue'];
}
?>

Upvotes: 3

animuson
animuson

Reputation: 54729

Shouldn't it be $consultant[$i] = $row['decisionValue'];?

Upvotes: 3

MMM
MMM

Reputation: 7310

You are only selecting decisionValue in your SQL query, change it to:

SELECT `decisionValue`, `SUconsultant` FROM `teamdecision` WHERE `decisionType` = 'SUconsultant'

Upvotes: 0

Amber
Amber

Reputation: 526573

You're not selecting SUconsultant as one of your columns, so why are you expecting it to be in the result row?

SELECT `decisionValue` FROM `teamdecision`

Whatever key you're referencing in your result set, you should select it as well.

Upvotes: 2

Related Questions