Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP-Newbie Frage bzgl. IF-Anweisung in SQL-Abfrage

Empfohlene Antworten

Veröffentlicht

Hallo,

ich beschäftige mich erst seit ein paar Tagen mit PHP und habe mal eine Frage bezüglich einer SQL-Anweisung.

Vorweg, die Datenbankverbindung steht, mit einfachen SQL-Anweisungen gibt es keine Probleme. Füge ich jedoch dem SQL-Query 'If-Anweisungen' hinzu, murrt mich PHP ständig an.

$month2 = 11;

$year2 = 2002;

$day2 = 27;

$rst = mssql_query("SELECT YEAR(datetime)AS year, MONTH(datetime) AS month

if (month2 = 00)

{

,1 as day;

else

{

,DAY(datetime) AS day;

}

,COUNT(DISTINCT c_ip) AS besucher, COUNT(c_ip) AS zugriffe

FROM vwn_statistik.dbo.tblStatistik

WHERE ActiveId = 3

GROUP BY MONTH(datetime), YEAR(datetime)

if (month2 != 00)

{

,DAY(datetime);

}

HAVING (if month2 != 00)

{

month(datetime) = month2; And

}

year(datetime)= year2

ORDER BY YEAR(datetime) DESC, MONTH(datetime) DESC

if (month2 != 00)

{

,day(datetime) DESC;

}");

while($vwn_statistik = mssql_fetch_array ($rst))

{

echo $vwn_statistik ["zugriffe"] ;

}

Fehlermeldungen:

Warning: mssql_query() [function.mssql-query]: message: Line 3: Incorrect syntax near '{'. (severity 15) in D:\wwwroot\vwn-intranet\test-ulf\test.php on line 141

Warning: mssql_query() [function.mssql-query]: message: Line 15: Incorrect syntax near '{'. (severity 15) in D:\wwwroot\vwn-intranet\test-ulf\test.php on line 141

Warning: mssql_query() [function.mssql-query]: message: Line 18: Incorrect syntax near ')'. (severity 15) in D:\wwwroot\vwn-intranet\test-ulf\test.php on line 141

Warning: mssql_query() [function.mssql-query]: message: Line 25: Incorrect syntax near '{'. (severity 15) in D:\wwwroot\vwn-intranet\test-ulf\test.php on line 141

Warning: mssql_query() [function.mssql-query]: Query failed in D:\wwwroot\vwn-intranet\test-ulf\test.php on line 141

Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in D:\wwwroot\vwn-intranet\test-ulf\test.php on line 143

Kurz gesagt:

fang mit: $rst = "SELECT BLABLABLA";

if(Anweisung) {

$rst.="SQLStatementerweiterung";

}

.

.

.

usw.

Dann dürften keine Fehler mehr auftreten, und sauberer ist der Code dann allemal.

Am Fehlerunanfälligsten (und vermutlich am saubersten) ist das Ganze, wenn du die Abfragen bei jeder Bedingung komplett zusammensetzt.

MfG

Sauber jetzt klappt's. Werde den richtige Syntax mal posten:


<?
$day2=27;
$month2=11;
$year2=2002;
$activeId=0;
$sql = " SELECT YEAR(datetime)AS year,MONTH(datetime) AS month, COUNT(DISTINCT c_ip) AS besucher,
COUNT(c_ip) AS zugriffe";

if ($month2 == "00"){
$sql .= ", 1 as day";
}else{
$sql .= ", DAY(datetime) AS day";
}
$sql .= ", COUNT(DISTINCT c_ip) AS besucher, COUNT(c_ip) AS zugriffe FROM vwn_statistik.dbo.tblStatistik
WHERE ActiveId= 3 GROUP BY MONTH(datetime), YEAR(datetime)";

if ($month2 <> "00"){
$sql .= ", DAY(datetime)";
}

$sql .= " HAVING year(datetime)= year2";

if ($month2 <> "00"){
$sql .= " AND month(datetime) = month2";
}
$sql .= " ORDER BY YEAR(datetime) DESC, MONTH(datetime) DESC";

if ($month2 <> "00"){
$sql .= " , day(datetime)";
}


echo $sql;
?>

[/PHP]

Wunderbar, jetzt kann es weitergehen:marine

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.