Counter funktioniert nicht.

user2016

Grünschnabel
Hallo,

ich bekomme das einfach nicht hin.

Meine DB-Struktur sieht wie folgt aus:
SQL:
CREATE TABLE IF NOT EXISTS `options` (
  `option_name` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `option_value` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SQL:
INSERT INTO `options` (`option_name`, `option_value`) VALUES
('counter_digits', '0');

Counter:
PHP:
$pdo = new PDO( $db_server, $db_user, $db_password, $db_options );
$counting = $pdo->prepare("UPDATE options SET option_value = option_value + 1 WHERE counter_digits = ?");
$counting->execute(array(counter_digits));

Wo steckt da der Fehler drin?
Vielen Dank im Voraus :)
 
Hi

was ist denn überhaupt das Problem?
Passiert einfach nichts oder...?

Was ist counter_digits?

Was geben alle drei PHP-Zeilen für Werte zurück?
 
Hi Sheel,

das Problem ist, dass ich das ganze nicht so recht verstehe, bzw. warum counter_digits nicht hochzählt.
 
PDO-Error-Funktion
Gibt nichts zurück.

Ich hab es nun so gelöst:
PHP:
$pdo = new PDO( $db_server, $db_user, $db_password, $db_options );

$sth = $pdo->prepare( "SELECT option_value FROM options WHERE option_id = 5" );
$sth->execute();
$count = $sth->fetch();

if( !isset( $_SESSION['visitor'] ) )
{

    $sth = $pdo->prepare( "UPDATE options SET option_value =" . $count['option_value'] . " + 1 WHERE option_id = 5" );
    $sth->execute();

    $_SESSION['visitor'] = TRUE;

}

$pdo = null;

Und einen String in der DB um 1 zu erhöhen dürfte nicht so toll sein.
Warum, wenn ich fragen darf?
 
Warum, wenn ich fragen darf?
Viele DB-Systeme können das einfach nicht.

Zu deiner jetztigen Lösung, damit hast du jetzt ein Concurreny-Problem.
Wenn 2 (oder mehr) Prozesse da ziemlich gleichzeitig zu dem Code kommen...
A: Abfrage, Wert ist 10
B: Abfrage, Wert ist 10
A. 11 einfügen
B: 11 einfügen
...und in der DB ist 11, nicht 12.

Man könnte zwar mit Transaktions kommen usw.usw., aber das einfache Update zu reparieren ist deutlich einfacher.
 
$counting->execute(array(counter_digits));

counter_digits ist keine Variable. Da fehlt ein Dollarzeichen.

Das heißt, da wird ein leerer Wert in deine Query gesetzt, und sie wählt einfach keine Zeile aus.

Dreh mal das Error-Reporting auf, dann beschwert sich PHP über so was. Am Scriptbeginn:

PHP:
error_reporting(E_ALL);
ini_set('display_errors', 1);

PS: Wenn du viel Code schreibst, würde ich mir auch einen Editor zulegen, der derlei Probleme findet. Ich weiß nicht, ob es Editoren wie Atom oder Sublime auch können (denke schon), aber etwa NetBeans (Free Software) und PhpStorm (diverse Lizenzen, aber prinzipiell Bezahlsoftware) können es in jedem Fall.
 
Zuletzt bearbeitet:
Hallo Mermshaus,

da ich von Sheel nichts mehr gelesen habe und ich es derweil schon selbst gelöst hatte (ob gut, oder nicht) ist dieses Thema wohl gelöst.
Danke für deine Anregung und Vorschläge, NetBeans ist mir durchaus bekannt. :)
 
Zurück