-1

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

I search a little in the same subject as mine but i dont find my answer.

I want to know why it gives me a mistake. Maybe it's my variable... but I try everything I know. The QUERY WORKS in PHPMYADMIN (without the variable) and it supposes to give me one only row Here's my code:

<?php 
            session_start(); 
            $choix = $_POST["choix"];
 ?>
<html>
    <head>
        <link rel="icon" href="images/favicon.ico" type="image/ico"/>
        <style>
            .corps{  width:745px;  }
            .image{
                width: 150px;
                height: 100px;
                float:left;
            }

            .descr{
                width: 560px;
                height: 100px;
                float:right;
                margin-right: 30px;
            }

            .bouton{ margin-top: 15px;} 
        </style>
    </head>
    <body>
    <h2>Résultat</h2>
<?php
    require('manip/connexionClass.php');
    $connexion = new Connexion();
    $con = $connexion->open();
    if($con){
        mysql_select_db("test",$con);

    //RECUPERATION DES INFORMATIONS DES PRODUITS
    $query = "
        SELECT *
        FROM `materiel` , `produit`
        WHERE  `produit`.`idProduit` = (
                SELECT `idProduit`
                FROM `materiel`
                WHERE `materiel`.`type` LIKE '%$choix%'
        )
        AND `materiel`.`idProduit` = `produit`.`idProduit`
        UNION
        SELECT *
        FROM `medicament` , `produit`
        WHERE  `produit`.`idProduit` = (
                SELECT `idProduit`
                FROM `medicament`
                WHERE `medicament`.`typeMed` LIKE '%$choix%'
        )
        AND `medicament`.`idProduit` = `produit`.`idProduit`
    ";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
?>
        <div class = "corps">
                    <div  class = 'image'>
                        <img src = 'images/imgMedic/tylenol.png' />                     
                    </div>
                    <div class = 'descr'>
                        <div class = "title"> <?php echo $row["nom"]; ?></div>
                        <div class = "plus"> <?php echo $row["descr"]; ?></div>
                        <div class = 'bouton'>
                            <img src = 'images/icones Web/basket_add.png' />&nbsp Ajouter au panier
                            <img style = 'margin-left: 15px;' src = 'images/icones Web/pill_go.png' />&nbsp Détails
                        </div>
                    </div>          
            </div>
<?php
    }
}//$con
?>
    </body>
</html> 
Community
  • 1
  • 1
Hulk
  • 7
  • 3
  • 8

2 Answers2

4

Do this:

$result = mysql_query($query) or die(mysql_error());

As per the billions of other answers on this site, the mysql functions return a boolean FALSE when things fail, which you're not checking for. Even if the query string is syntactically perfect, there's literally hundreds of other reasons why the query might still fail. Blindly assuming success is a very bad thing to do.

Marc B
  • 348,685
  • 41
  • 398
  • 480
  • it gives me: Subquery returns more than 1 row – Hulk Aug 12 '11 at 14:56
  • You're doing a `WHERE somevalue = (subquery)`. For that kind of comparison, the subquery can return only a single row/value. You'd have to conver to a `WHERE somevalue IN (subquery)`, which allows for multiple rows. – Marc B Aug 12 '11 at 14:59
  • OOO I see my mistake. Thanks for this trick. I'll use it all the time now :D – Hulk Aug 12 '11 at 15:01
1

change

$result = mysql_query($query);

to

$result = mysql_query($query) or die(mysql_error());

It will give you answer (error) needed to resolve your issue

genesis
  • 49,367
  • 20
  • 94
  • 122