Nombre de résultat par SELECT {PDO}

Zayer -  
 Zayer -
Bonjour,

J'ai une requête PDO avec MySQL et je dois effectuer un SELECT sur une base de donnée pour voir si la valeur (ici artiste) existe déjà, si tel est le cas, ne rien faire, sinon, l'insérer et garder l'id en variable...

J'ai chercher partout sur internet et je ne trouve rien qui fonctionne correctement...

Voici mon code :
-----------------------------------------------------------
$sg_artist = "Alexendre Poulin";

////Looking if the artist exist
$req = $bdd->query("SELECT count(*) FROM artists WHERE name=\"$sg_artist\"");

//Save the count (0 or 1) in $count
$count = $req->rowCount();

//If 1, exist
if($count == 1) {

//Save the id to $id_artist
while($data = $req->fetch()) { $id_artist = $data['id']; }

/*****/ echo "[!] Already exist: ID $id_artist";
}

//Else create the artist

else {
$req = $bdd->exec("INSERT INTO artists (id ,name) VALUES(\"\", \"$sg_artist\")") or die(print_r($bdd->errorInfo()));
//Save id in id_artist
$id_artist = $bdd->lastInsertId();

/*****/ echo "Created: ID $id_artist";
}
-----------------------------------------------------------
Quelqu'un sait comment procéder SVP?
Grandement apprécié,
Merci :)
A voir également:

1 réponse

Zayer
 
Il n'y a aucune solution ou alternative? :(
0