简体   繁体   中英

PDO query returns nothing

I have a problem with my query, it returns nothing.

  if($champ == "type_id")       
            {
                $bdd = new PDO("mysql:dbname=maruecondi_db;host=localhost","root","");
                $request = $bdd->prepare('SELECT * FROM type_commercant WHERE type=:old');
                $request->execute(array(':old' => $old));
                while($row = $request->fetch())
                {
                    $bdd1 = new PDO("mysql:dbname=maruecondi_db;host=localhost","root","");
                    $request1 = $bdd1->prepare('UPDATE commercant SET type_id=:type_id WHERE id=:id');
                    $request1->execute(array(':type_id' => $row['id'],':id' => $id));
                }
            }

I'm getting variables from ajax request (JQUERY) and i initialize them before, i avoid you the code. Other requests on the page works. I have currently no way to see if somethings got wrong, due to ajax call. (No php orange boxes / pdo message) I tried to solve to problems, and i discovered that we go into the if. I deleted the first query which contains the while, i replaced $row['id'] by a value, and i worked. Since the beginning, i keep copying and pasting the connection to my database so no problem.

So my problem is here:

                $request = $bdd->prepare('SELECT * FROM type_commercant WHERE type=:old');
                $request->execute(array(':old' => $old));
                while($row = $request->fetch())

I don't see what i've done wrong...

 $request = $bdd->prepare('SELECT * FROM type_commercant');
                $request->execute();
                while($row = $request->fetch())

This works, so i tried this:

                    $request = $bdd->prepare('SELECT * FROM type_commercant');
                $request->execute();
                while($row = $request->fetch())
                {
                    if($row['type'] == $old)
                    {
                        $request1 = $bdd->prepare('UPDATE commercant SET type_id=:type_id WHERE id=:id');
                        $request1->execute(array(':type_id' => $row['id'],':id' => $id));
                    }

                }

We don't go in the condition if($row['type'] == $old), but i delete this condition, and when i replace with something like this:

while($row = $request->fetch())
                {
                        $request1 = $bdd->prepare('UPDATE commercant SET adresse=:type_id WHERE id=:id');
                        $request1->execute(array(':type_id' => $row['id'],':id' => $id));
                 }

It works... i checked $row['type'], $row['id'], $old in array(':type_id' => $row['id'], all variable got the string attented. So what's the problem? Thanks in advance !

Hmm, how to say that...

I was updating the data with the old data i explain myself:

$bdd = new PDO("mysql:dbname=maruecondi_db;host=localhost","root","");
            $request = $bdd->prepare('SELECT id FROM type_commercant WHERE type=:old');
            $request->execute(array(':old' => $old));

was getting the old ID from type_commercant, and i was doing this:

 $request1 = $bdd1->prepare('UPDATE commercant SET type_id=:type_id WHERE id=:id');
                $request1->execute(array(':type_id' => $row['id'],':id' => $id));

So i replaced the old ID by... the old ID.

Sorry for that mistake, thanks anyway for reading me and trying so solve this problem :D

Try to call PDO::exec() for doing UPDATE or DELETE .

$bdd = new PDO("mysql:dbname=maruecondi_db;host=localhost","root","");

if($champ == "type_id")       
{
    $stmt = $bdd->prepare('SELECT * FROM type_commercant WHERE type=:old');
    $stmt->execute(array(':old' => $old));
    $rows = $stmt->fetchAll(); 
    // Uncomment this to know what you get
    // var_dump($rows);
    foreach ( $rows as $row ) {
        $bdd->exec(
            "UPDATE commercant
                SET type_id = " . $bdd->quote($row['id']) .
            " WHERE id = " . $bdd->quote($id)
        );
    }
}

You can use this code for debugging.

var_dump( $bdd->errorInfo() );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM