Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Jak sprawdzić, czy zapytania do bazy się udały?
Forum PHP.pl > Forum > PHP
Asmox
W książce pt. "PHP5. Zaawansowanie programowanie" doszukałem się błędu. Mianowicie przy tworzeniu "Warstwy abstrakcji dla bazy danych" metoda Insert kończy się w taki sposób:
  1. <?php
  2. $query = mysql_query($sql, $this->link);
  3.    if(! is_resource($query)) {
  4.      $error = mysql_errno($this->link) . ": " . mysql_error($this->link);
  5.      throw new exception($error);
  6.    }
  7.    
  8.    return mysql_affected_rows($query);
  9. ?>

To chyba wiadome, że instrukcja Insert nie zwraca żadnej kotwicy do danych (w przeciwieństwie do funkcji Select). Testując kod zawsze rzuca mi on wyjątek o kodzie błędu 0 (czyli że go nie ma).

Chciałbym się dowiedzieć, jak sprawdzać, czy zapytania Insert, Update i Delete się powiodły. Czy coś takiego będzie w porządku:
  1. <?php
  2. $query = mysql_query($sql, $this->link);
  3.    if(! mysql_affected_rows($query)) {
  4.      $error = mysql_errno($this->link) . ": " . mysql_error($this->link);
  5.      throw new exception($error);
  6.    }
  7.    
  8.    return mysql_affected_rows($query);
  9. ?>


@EDIT: Już sprawdziłem, coś kurde nie działa, bo wywala mi taki błąd:
Cytat
Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in C:\Asmox\Programy\WebServ\httpd\KPiR\components\class.Database.php on line 58

Czyli jednak zwraca jakąś kotwicę. Wygląda na to, że to ja coś zwaliłem. Tylko co?
skowron-line
Odpowiedz kryje się w manualu ( jak zawsze ) smile.gif
http://pl.php.net/manual/en/function.mysql-affected-rows.php
Cytat
Parameters

link_identifier

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level error is generated.


http://pl.php.net/manual/en/function.mysql-query.php

Cytat
Return Values

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.
Asmox
to mam robić coś w tym stylu if(! mysql_query($sql)) throw new exception
skowron-line
Cytat(Asmox @ 19.06.2009, 17:23:51 ) *
to mam robić coś w tym stylu if(! mysql_query($sql)) throw new exception

No raczej.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.