Je ne comprend pas ou est mon erreur sql ?
Résolu
Utilisateur anonyme
-
Utilisateur anonyme -
Utilisateur anonyme -
Bonjour, j'ai fais un code mais il ne fonctionne pas le voici :
Et voici la structure de ma base
db=ied -> structure des tables :
# Nom Type
1 id(Primaire) int(11)
2 montant int(11)
Nom des tables : safege2014, ld2014, ld2015
Ma requête est concaténé mais c'était pour les tests je la préparerai plus tard.
Et voici l'erreur que sa affiche :
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ld2015'' at line 1' in C:\xampp\htdocs\ied\graph.php:13
Cdlt Théo B
Théo B
<!DOCTYPE html>
<html> <!-- oncontextmenu="return false" ondragstart="return false" onselectstart="return false" onclick="return false" -->
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="../base.css" />
<link rel="stylesheet" href="../header.css" />
<link rel="stylesheet" href="ied.css">
<link href="../favicon.ico" rel="shortcut icon" type="image/x-icon">
<title></title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script src="/js/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php
require_once '../header.html';
?>
<div id="formulaire">
<form method="post" action="graph.php">
<select name="annee" id="annee">
<optgroup label="2014">
<option value="safege2014">12014</option>
<option value="ld2014">22014</option>
</optgroup>
<optgroup label="2015">
<option value="ld2015" selected>12015</option>
</optgroup>
</select>
<input type="submit" value="Envoyer" />
</form>
</div>
</body>
</html>
<?php
require_once 'phplot/phplot.php';
try
{
$bdd = new PDO('mysql:host=localhost;dbname=ied;charset=utf8', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
$reponse = $bdd->query('SELECT * FROM possesseur = \'' . $_POST['annee'] . '\'');
$datas = array_map(function($data){
return array('', $data['id'], $data['montant']);
}, $response->fetchAll());
$response->closeCursor();
$plot = new PHPlot(800, 600);
$plot->SetFailureImage(False);
$plot->SetPrintImage(False);
$plot->SetPlotType('lines');
$plot->SetDataType('data-data');
$plot->SetDataValues($datas);
$plot->DrawGraph();
?>
<img id="graph" src="<?php echo $plot->EncodeImage();?>" alt="Image">
Et voici la structure de ma base
db=ied -> structure des tables :
# Nom Type
1 id(Primaire) int(11)
2 montant int(11)
Nom des tables : safege2014, ld2014, ld2015
Ma requête est concaténé mais c'était pour les tests je la préparerai plus tard.
$response = $bdd -> prepare("SELECT * FROM annee = ?");
$response -> execute(array($_POST['annee']));
Et voici l'erreur que sa affiche :
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ld2015'' at line 1' in C:\xampp\htdocs\ied\graph.php:13
Cdlt Théo B
Théo B
A voir également:
- Je ne comprend pas ou est mon erreur sql ?
- Instagram une erreur s'est produite - Forum Instagram
- Erreur t32 ✓ - Forum Livebox
- Erreur lors de l'enregistrement du contact - Forum Xiaomi
- Erreur 0x80070643 - Accueil - Windows
- Iptv erreur de lecture - Forum TV & Vidéo
3 réponses
Bonjour
Ta requête est incorrecte.
Ta requête est incorrecte.
SELECT * FROM annee = ?ne veut rien dire. Une requête ressemble plutôt à
SELECT * FROM matable WHERE annee = ?.
Utilisateur anonyme
Ah d'accord mais moi je voulais faire varier justement la table c'est impossible de le faire donc ?
Non, tu peux très bien le faire :
$matable="nom de ma table"; $requete='SELECT * FROM '.$matable.' WHERE annee = ?';
<?php
require_once 'phplot/phplot.php';
try
{
$bdd = new PDO('mysql:host=localhost;dbname=ied;charset=utf8', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
$reponse = $bdd->query("SELECT * FROM ".$_POST['annee']."")
$datas = array_map(function($data){
return array('', $data['id'], $data['montant']);
}, $response->fetchAll());
$response->closeCursor();
$plot = new PHPlot(800, 600);
$plot->SetFailureImage(False);
$plot->SetPrintImage(False);
$plot->SetPlotType('lines');
$plot->SetDataType('data-data');
$plot->SetDataValues($datas);
$plot->DrawGraph();
?>
<img id="graph" src="<?php echo $plot->EncodeImage();?>" alt="Image"> J'ai compris mon erreur de syntaxe grâce a ce forum : ( https://openclassrooms.com/forum/sujet/affectation-d039-une-varaible-au-nom-de-la-table-29775 ) mais j'ai toujours une erreur : Parse error: syntax error, unexpected '$datas' (T_VARIABLE) in C:\xampp\htdocs\ied\graph.php on line 15