2

I'm having a lot of trouble understanding why sometimes my queries return values and sometimes it get something else, pointers?

I think there is a concept I'm missing about variables.

$sql = "SELECT 
    customerData.studFirstName,
    customerData.studLastName, 
    customerData.custPhone,
    customerData.studPhone, 
    classRoll.courseNumber, 
    classRoll.payFull, 
    classRoll.payAmount
FROM classRoll, customerData
WHERE classRoll.custId = customerData.custId
AND classRoll.courseNumber =  '".$_REQUEST['cnum']."'
ORDER BY customerData.studLastName;";

$result = $conn->query("SELECT COUNT(*) AS count FROM classRoll");
while ($row= $result->fetch_assoc()){
    $output[]=$row[studLastName].", ".$row[studFirstName];
};
var_dump($output);
?>

The query works in phpadmin, but I get this

array(1) { [0]=> string(2) ", " }

I don't want a solution as bad as an understanding. I appreciate any guidance.

1 Answer 1

1
  • If you are getting single value, you don't need to put that in while loop.

  • secondly $output[]=$row[studLastName].", ".$row[studFirstName]; these keys are not exist, as you are querying count.

  • You are not using $sql

You may simply fetch, results and use it, see example below:-

$result = $conn->query("SELECT COUNT(*) AS count FROM classRoll");
$row = $result->fetch_row();
echo $row[0];

but, I guess you probably want to you $sql, for that code looks fine.

$result = $conn->query($sql);
while ($row= $result->fetch_assoc()){
    $output[]=$row['studLastName'].", ".$row['studFirstName'];
}
var_dump($output);
Sign up to request clarification or add additional context in comments.

3 Comments

I've changed so much in here, I missed that. I can't say I understand much better, but that worked. Thanks.
$result->fetch_row(); will return array and the index, starts from 0 to number of columns - 1 $result->fetch_assoc() will return array with column names as index. does this make sense ? I suggest you to go through with [PHP Mannual ](php.net/manual/en/class.mysqli-result.php)
I think i get it. I'll keep digging through documentation. It's just a bit heavy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.