Probleem met SQL-query

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Jo Immanuel

Jo Immanuel

23/10/2013 22:40:31
Quote Anchor link
Ik heb de volgende sql-query

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
SELECT rugnummer, achternaam, tbl_kaarten.ikoon, CASE
WHEN aantaldoelpunten = 1 THEN '<img src="1bal.png" alt="icon" />'
WHEN aantaldoelpunten = 2 THEN '<img src="2bal.png" alt="icon" />'
WHEN aantaldoelpunten = 3 THEN '<img src="3bal.png" alt="icon" />'
WHEN aantaldoelpunten = 4 THEN '<img src="4bal.png" alt="icon" />'
WHEN aantaldoelpunten = 5 THEN '<img src="5bal.png" alt="icon" />'
WHEN aantaldoelpunten = 6 THEN '<img src="6bal.png" alt="icon" />'
WHEN aantaldoelpunten = 7 THEN '<img src="7bal.png" alt="icon" />'
WHEN aantaldoelpunten = 8 THEN '<img src="8bal.png" alt="icon" />'
WHEN aantaldoelpunten = 9 THEN '<img src="9bal.png" alt="icon" />'
END AS aantaldoelpunten FROM tbl_opstellingn  
LEFT JOIN tbl_spelers ON tbl_opstellingn.spelerID = tbl_spelers.spelerID  
LEFT JOIN ktbl_club_speler ON tbl_opstellingn.spelerID = ktbl_club_speler.spelerID  
LEFT JOIN tbl_kaarten ON tbl_opstellingn.spelerID = tbl_kaarten.spelerID  
LEFT JOIN
( SELECT COUNT(*) AS aantaldoelpunten, tbl_doelpunten.spelerID
FROM tbl_doelpunten  
LEFT JOIN tbl_opstellingn ON tbl_doelpunten.spelerID = tbl_opstellingn.spelerID
WHERE thuis_uit = 'thuis' AND tbl_doelpunten.wedstrijdID = '109'
GROUP BY tbl_opstellingn.opstellingpos ) a
ON tbl_spelers.spelerID = a.spelerID WHERE tbl_opstellingn.wedstrijdID = '109' AND thuis_uit = 'thuis' AND clubID = '11' limit 0, 11


Wanneer ik deze query uitvoer in MySQL gaat alles prima. Maar zodra ik hem op mn site laat zien krijg ik de volgende error:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
MySQL Error#: 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 ') f ON tbl_spelers.spelerID = f.spelerID WHERE tbl_opstellingn.wedstrijdID = '10' at line 2


Ik begrijp er niets meer van. Weet iemand wat er aan de hand is?

Toevoeging op 23/10/2013 23:14:03:

Ik heb nu de spatie tussen "(" en "select count" weggehaald. Nu krijg ik geen 1064-error maar. MAar bij het testen van de pagina krijg ik nu de volgende error:

"Parse error: syntax error, unexpected '1' (T_LNUMBER) in C:\xampp\htdocs\................php on line 261"

line 261 is overigens bij bovenstaande sql-query.
 
PHP hulp

PHP hulp

29/04/2024 18:44:25
 
Erwin H

Erwin H

23/10/2013 23:26:09
Quote Anchor link
Ik kan beide vragen/problemen niet helemaal plaatsen. De tekst die in de foutmelding van je query krijgt staat niet in de query. Dus daar kunnen we verder niets mee (ofwel: plaats de goede code).
De tweede foutmelding is ook niets over te zeggen omdat dat een php probleem betreft (waarschijnlijk een quote teveel of te weinig), maar je laat geen php code zien.
 
Jo Immanuel

Jo Immanuel

23/10/2013 23:34:46
Quote Anchor link
He Erwin,

PHP is (nog) niet mijn sterkste kant (doe daarom ook veel met dreamweaver, excuses daarvoor)

Bedoel je dit?

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
mysql_select_db($database_hallo, $hallo);
$query_RSopstellingplusTHUIS = "SELECT rugnummer, achternaam, tbl_kaarten.icon, CASE WHEN aantaldoelpunten = 1 THEN '<img src="1bal.png" alt="icon" />' WHEN aantaldoelpunten = 2 THEN '<img src="2bal.png" alt="icon" />' WHEN aantaldoelpunten = 3 THEN '<img src="3bal.png" alt="icon" />' WHEN aantaldoelpunten = 4 THEN '<img src="4bal.png" alt="icon" />' WHEN aantaldoelpunten = 5 THEN '<img src="5bal.png" alt="icon" />' WHEN aantaldoelpunten = 6 THEN '<img src="6bal.png" alt="icon" />' WHEN aantaldoelpunten = 7 THEN '<img src="7bal.png" alt="icon" />' WHEN aantaldoelpunten = 8 THEN '<img src="8bal.png" alt="icon" />' WHEN aantaldoelpunten = 9 THEN '<img src="9bal.png" alt="icon" />' END AS aantaldoelpunten FROM tbl_opstellingn LEFT JOIN tbl_spelers ON tbl_opstellingn.spelerID = tbl_spelers.spelerID LEFT JOIN ktbl_club_speler ON tbl_opstellingn.spelerID = ktbl_club_speler.spelerID LEFT JOIN tbl_kaarten ON tbl_opstellingn.spelerID = tbl_kaarten.spelerID LEFT JOIN (SELECT COUNT( * ) AS aantaldoelpunten, tbl_doelpunten.spelerID FROM tbl_doelpunten LEFT JOIN tbl_opstellingn ON tbl_doelpunten.spelerID = tbl_opstellingn.spelerID WHERE thuis_uit = 'thuis' AND tbl_doelpunten.wedstrijdID = '109' GROUP BY tbl_opstellingn.opstellingpos )a ON tbl_spelers.spelerID = a.spelerID WHERE tbl_opstellingn.wedstrijdID = '109' AND thuis_uit = 'thuis' AND clubID = '11'   ";
$query_limit_RSopstellingplusTHUIS = sprintf("%s LIMIT %d, %d", $query_RSopstellingplusTHUIS, $startRow_RSopstellingplusTHUIS, $maxRows_RSopstellingplusTHUIS);
$RSopstellingplusTHUIS = mysql_query($query_limit_RSopstellingplusTHUIS, $hallo) or die(mysql_error());
$row_RSopstellingplusTHUIS = mysql_fetch_assoc($RSopstellingplusTHUIS);

if (isset($_GET['totalRows_RSopstellingplusTHUIS'])) {
  $totalRows_RSopstellingplusTHUIS = $_GET['totalRows_RSopstellingplusTHUIS'];
} else {
  $all_RSopstellingplusTHUIS = mysql_query($query_RSopstellingplusTHUIS);
  $totalRows_RSopstellingplusTHUIS = mysql_num_rows($all_RSopstellingplusTHUIS);
}
$totalPages_RSopstellingplusTHUIS = ceil($totalRows_RSopstellingplusTHUIS/$maxRows_RSopstellingplusTHUIS)-1;
 
Erwin H

Erwin H

23/10/2013 23:45:39
Quote Anchor link
Kijk, nu wordt het duidelijk waar het probleem zit. Je gebruikt allerlei quotes door elkaar en dat gaat zo op een gegeven moment fout. Kijk naar de kleuren en je ziet het probleem:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
$query_RSopstellingplusTHUIS
= "SELECT rugnummer, achternaam, tbl_kaarten.icon,
  CASE WHEN aantaldoelpunten = 1 THEN '<img src="
1bal.png" alt="icon" />'
       WHEN aantaldoelpunten = 2 THEN '<img src="
2bal.png" alt="icon" />'
       WHEN aantaldoelpunten = 3 THEN '<img src="
3bal.png" alt="icon" />'
       WHEN aantaldoelpunten = 4 THEN '<img src="
4bal.png" alt="icon" />'
       WHEN aantaldoelpunten = 5 THEN '<img src="
5bal.png" alt="icon" />'
       WHEN aantaldoelpunten = 6 THEN '<img src="
6bal.png" alt="icon" />'
       WHEN aantaldoelpunten = 7 THEN '<img src="
7bal.png" alt="icon" />'
       WHEN aantaldoelpunten = 8 THEN '<img src="
8bal.png" alt="icon" />'
       WHEN aantaldoelpunten = 9 THEN '<img src="
9bal.png" alt="icon" />' END AS aantaldoelpunten
  FROM tbl_opstellingn
  LEFT JOIN tbl_spelers ON tbl_opstellingn.spelerID = tbl_spelers.spelerID
  LEFT JOIN ktbl_club_speler ON tbl_opstellingn.spelerID = ktbl_club_speler.spelerID
  LEFT JOIN tbl_kaarten ON tbl_opstellingn.spelerID = tbl_kaarten.spelerID
  LEFT JOIN (
    SELECT COUNT( * ) AS aantaldoelpunten, tbl_doelpunten.spelerID
    FROM tbl_doelpunten
    LEFT JOIN tbl_opstellingn ON tbl_doelpunten.spelerID = tbl_opstellingn.spelerID
    WHERE thuis_uit = 'thuis' AND tbl_doelpunten.wedstrijdID = '109'
    GROUP BY tbl_opstellingn.opstellingpos
  )a ON tbl_spelers.spelerID = a.spelerID
  WHERE tbl_opstellingn.wedstrijdID = '109'
    AND thuis_uit = 'thuis'
    AND clubID = '11'   "
;
?>

En ook een beetje handiger op geschreven want bovenstaande query op 1 regel zetten is natuurlijk complete waanzin.
 
Jo Immanuel

Jo Immanuel

23/10/2013 23:54:05
Quote Anchor link
Hmm, ik kijk naar de kleuren, maar kan het probleem niet vinden.
 
Erwin H

Erwin H

23/10/2013 23:56:13
Quote Anchor link
Je ziet niet dat de query geopend wordt door een dubbele quote en dat de dubbele quote in de case statement het weer afsluit? Die dubbele quote tussen src= en 1bal.png.
 
Jo Immanuel

Jo Immanuel

24/10/2013 00:09:05
Quote Anchor link
OK, dus ik mag tussen src= en 1 bal.png geen dubbele quotes gebruiken en die moet ik vervangen door enkele?

Toevoeging op 24/10/2013 00:15:39:

Ik heb nu alle dubbele quotes weggehaald bij de case statement. Gevolg is geen rode tekst meer in de code. Echter nog wel een error bij testen. Namelijk:

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 '1bal.png' alt='icon' />' WHEN aantaldoelpunten = 2 THEN '

Toevoeging op 24/10/2013 00:24:41:

Nou, volgens mij is het gelukt.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
"SELECT rugnummer, achternaam, tbl_kaarten.icon,  CASE WHEN aantaldoelpunten = 1 THEN '<img src=1bal.png alt=icon />'


Helemaal geen quotes (dubbele of enkele) meer om 1bal.png en icon heen.

Ik ga morgen verder kijken. Erwin. hartstikke bedankt!
 
Ger van Steenderen
Tutorial mod

Ger van Steenderen

24/10/2013 00:36:38
Quote Anchor link
Beter nog is om heel die CASE statement weg te laten:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
<?php
$result
= $mysqli->query("SELECT aantaldoelpunten FROM"; // .... en het ressie;
while ($row = $result->fetch_assoc()) {
echo '<img src="' . $row['aantaldoelpunten'] . 'bal.png" alt="icon" />';
}

?>
Gewijzigd op 24/10/2013 00:37:54 door Ger van Steenderen
 
Jo Immanuel

Jo Immanuel

24/10/2013 00:53:34
Quote Anchor link
He Ger,

Krijg je dan ook meerdere ballen achter elkaar bij een speler als die meerdere keren heeft gescoord?

PS: Waarom is het beter? Omdat op mijn manier er een probleem is wanneer 1 speler meer dan 9x scoort, of is er een andere reden?
Gewijzigd op 24/10/2013 01:20:18 door Jo Immanuel
 
Ger van Steenderen
Tutorial mod

Ger van Steenderen

24/10/2013 08:41:57
Quote Anchor link
Het uiteindelijke resultaat is hetzelfde, nl. een plaatje met het aantal ballen.

De voornaamste reden is dat je SQL geen HTML moet laten produceren, daar is PHP voor.
 
Jo Immanuel

Jo Immanuel

24/10/2013 23:52:21
Quote Anchor link
Ger,

Ik kom er echt niet meer uit.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php require_once('Connections/hallo.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function
GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{

  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }


  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case
"text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case
"long":
    case
"int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case
"double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case
"date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case
"defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }

  return $theValue;
}
}


mysql_select_db($database_hallo, $hallo);
$query_RSTHUISopstelling = "
SELECT rugnummer, achternaam, tbl_kaarten.icon,  aantaldoelpunten  
FROM tbl_opstellingn   LEFT JOIN tbl_spelers ON tbl_opstellingn.spelerID = tbl_spelers.spelerID  
LEFT JOIN ktbl_club_speler ON tbl_opstellingn.spelerID = ktbl_club_speler.spelerID  
LEFT JOIN tbl_kaarten ON tbl_opstellingn.spelerID = tbl_kaarten.spelerID  
LEFT JOIN  (
SELECT COUNT(*) AS aantaldoelpunten, tbl_doelpunten.spelerID FROM tbl_doelpunten  
LEFT JOIN tbl_opstellingn ON tbl_doelpunten.spelerID = tbl_opstellingn.spelerID  
WHERE thuis_uit = 'thuis'  
AND tbl_doelpunten.wedstrijdID = '109'  
GROUP BY tbl_opstellingn.opstellingpos ) f ON tbl_spelers.spelerID = f.spelerID  
WHERE tbl_opstellingn.wedstrijdID = '109'  AND thuis_uit = 'thuis'  
AND clubID = '11' limit 0, 11"
;
$RSTHUISopstelling = mysql_query($query_RSTHUISopstelling, $hallo) or die(mysql_error());
$row_RSTHUISopstelling = mysql_fetch_assoc($RSTHUISopstelling);
$totalRows_RSTHUISopstelling = mysql_num_rows($RSTHUISopstelling);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
#apDiv1 {
    position: absolute;
    width: 250px;
    height: 350px;
    z-index: 1;
    left: 11px;
    top: 16px;
}
</style>
</head>

<body>
<div id="apDiv1">
  <table border="0" cellpadding="0" cellspacing="0">
    <?php do { ?>
      <tr>
        <td width="25" align="center"><?php echo $row_RSTHUISopstelling['rugnummer']; ?></td>
        <td width="125" align="left"><?php echo $row_RSTHUISopstelling['achternaam']; ?></td>
        <td width="50" align="right"><?php echo $row_RSTHUISopstelling['icon']; ?></td>
        <td width="50" align="right"><?php echo $row_RSTHUISopstelling['aantaldoelpunten']; ?></td>
      </tr>
      <?php } while ($row_RSTHUISopstelling = mysql_fetch_assoc($RSTHUISopstelling)); ?>
  </table>
</div>
</body>
</html>
<?php
mysql_free_result($RSTHUISopstelling);
?>


Bovenstaande is mijn oefenpagina.
Ik heb de CASE-statement uit mijn SQL-query gesloopt. Nu geeft kolom aantaldoelpunten gewoon het numerieke aantal doelpunten dat een speler gescoord heeft.

Waar moet jouw code om middels php tekst aan de numerieke waarde te plakken dan komen te staan?

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
<?php
$result
= $mysqli->query("SELECT aantaldoelpunten FROM"; // .... en het ressie;
while ($row = $result->fetch_assoc()) {
echo '<img src="' . $row['aantaldoelpunten'] . 'bal.png" alt="icon" />';
}

?>


En wat bedoel je met ".... en het ressie"? moet ik daar de hele sql-query weer zetten?
Gewijzigd op 24/10/2013 23:54:55 door - Ariën -
 
- Ariën  -
Beheerder

- Ariën -

24/10/2013 23:56:08
Quote Anchor link
restje.... ;-)

Wat er nog mist is de tabelnaam, een eventuele WHERE-voorwaarde en de rest van de query.
Waarom een hele lap query van tiental regels opnieuw plaatsen, terwijl je hem ook even op het forum kan inkorten.
Gewijzigd op 24/10/2013 23:58:15 door - Ariën -
 
Jo Immanuel

Jo Immanuel

25/10/2013 00:04:52
Quote Anchor link
OK. Dat wordt dus in mijn geval:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
$result
= $mysqli->query("SELECT aantaldoelpunten FROM tbl_opstellingn  
 LEFT JOIN tbl_spelers ON tbl_opstellingn.spelerID = tbl_spelers.spelerID  
LEFT JOIN ktbl_club_speler ON tbl_opstellingn.spelerID = ktbl_club_speler.spelerID  
LEFT JOIN tbl_kaarten ON tbl_opstellingn.spelerID = tbl_kaarten.spelerID  
LEFT JOIN  (
SELECT COUNT(*) AS aantaldoelpunten, tbl_doelpunten.spelerID FROM tbl_doelpunten  
LEFT JOIN tbl_opstellingn ON tbl_doelpunten.spelerID = tbl_opstellingn.spelerID  
WHERE thuis_uit = 'thuis'  
AND tbl_doelpunten.wedstrijdID = '109'  
GROUP BY tbl_opstellingn.opstellingpos ) f ON tbl_spelers.spelerID = f.spelerID  
WHERE tbl_opstellingn.wedstrijdID = '109'  AND thuis_uit = 'thuis'  
AND clubID = '11' limit 0, 11"

while ($row = $result->fetch_assoc()) {
echo '<img src=' . $row['aantaldoelpunten'] . 'bal.png alt=icon />';
}

?>


En waar zet ik dat dan in mijn code neer?
Gewijzigd op 25/10/2013 00:21:04 door Jo Immanuel
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.