0

I currently have an array in php that stores product names, normally 5 - 10. I would like to create something where the results of a SQL query is matched against the array to make sure they are all correct, and if not to show an error.

So far I have put the results into an array, then ran the query to get the results. I believe I need to put some sort of while loop with the results of the query and check the array in that while loop?

2
  • 2
    Please give us the code where you have tried to do this. Commented Mar 26, 2013 at 8:43
  • withouth any code or hint it's quite hard to answer you correctly. What do you want to check and why? a foreach loop with in_array() function may be enough for your needs but who knows. Commented Mar 26, 2013 at 8:44

2 Answers 2

1

You have the array with the product name($products) and the results of the query($row). While you loop trough the results you can check if the element is present,otherwise echo error and break the loop:

While(...) {     
    if(!in_array($row['retrivedprod'],$products)) {
        echo 'error';
        break;
     }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can cycle while receiving results from db with a while loop and checking results with the in_array function http://php.net/manual/en/function.in-array.php $availability = 0;

$cart_products = array("book", "album");
$available_products = array();

while($row = mysql_fetch_array($result)) {
    $available_products[] = $row['product'];
}

foreach($cart_products as $key => $value){  
    if (in_array($value, $available_products)){
       $availability = 1;
    }
} 

1 Comment

If the only contribution is a link, it should be made in comment form. ^^ Otherwise, please provide a code sample explaining the why/how-to.

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.