0

I'm almost afraid to post this, as the top 20 questions that the auto search returns are all downvoted to oblivion. I have read through each of the questions that have already been asked, and my question seems to be different.

My query is working fine, in that, it is giving me the correct data as a response. However, I am still getting this error and I can't figure out why. Here's the code:

$acOneLowestCostQuery  = "SELECT * FROM $acSupplierOne where quotePartNumber = '$acPartNumberOne' ORDER BY quoteCost ASC LIMIT 1" ;
$acOneLowestCost    = mysqli_query($con, $acOneLowestCostQuery);
 while ($row = mysqli_fetch_array($acOneLowestCost)) {
    $acOnePartNumber       = $row['quotePartNumber'];
    $acOneLowestCost       = $row['quoteCost'];    
  ?>

My table outputs the correct information, but above the table is:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in ... on line 199

line 199 is

while ($row = mysqli_fetch_array($acOneLowestCost)) {

What I don't understand is that:

  • I copied this entire code (and just changed the variable names) from another page where's it working perfectly, (without any errors)
  • The code executes just fine AND returns the right data into my table
  • I don't understand why it's telling me I'm passing it a string - unless it's because the result of the query only gives back a SINGLE line (from the ASC LIMIT 1), and maybe you need two lines to make an array???

Would someone help me understand why this error is occurring? I'd rather fix the problem then use error reporting to not show it.

2
  • Have you tried putting var_dump($acOneLowestCost); above the while? Commented Jan 13, 2015 at 19:24
  • object(mysqli_result)#6 (5) { ["current_field"]=> int(0) ["field_count"]=> int(18) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }. I've looked into var_dump a bit, but this is way more complex than I'm used to reading... Can you help me interpret it? Commented Jan 13, 2015 at 19:26

1 Answer 1

3

Problem is in variable naming:

$acOneLowestCost    = mysqli_query($con, $acOneLowestCostQuery);
// here $acOneLowestCost is mysqli_result
while ($row = mysqli_fetch_array($acOneLowestCost)) {
    $acOnePartNumber       = $row['quotePartNumber'];
    // and here it becomes a string which then passed to mysqli_fetch_array
    $acOneLowestCost       = $row['quoteCost'];    
Sign up to request clarification or add additional context in comments.

2 Comments

Good eye. @OP you should get in the habit of using unique variable names and only re-using a variable name if it contains the same type of data.
stupid stupid...... I would never knowingly have reused that... I was making too many new variables names and I guess my brain ran out of creative things to say. Thank you so much for pointing out where I went wrong!

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.