Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Jak sprawdzić, czy zapytania do bazy się udały?
Asmox
post 19.06.2009, 14:07:48
Post #1





Grupa: Zarejestrowani
Postów: 359
Pomógł: 12
Dołączył: 16.01.2009

Ostrzeżenie: (0%)
-----


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?

Ten post edytował Asmox 19.06.2009, 14:12:33


--------------------
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
skowron-line
post 19.06.2009, 14:51:04
Post #2





Grupa: Zarejestrowani
Postów: 4 340
Pomógł: 542
Dołączył: 15.01.2006
Skąd: Olsztyn/Warszawa

Ostrzeżenie: (0%)
-----


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.


Ten post edytował skowron-line 19.06.2009, 14:52:53


--------------------
I'm so fast that last night I turned off the light switch in my hotel room and was in bed before the room was dark - Muhammad Ali.
Peg jeżeli chcesz uprawiać sex to dzieci muszą wyjść, a jeżeli chcesz żeby był dobry ty też musisz wyjść - Al Bundy.

QueryBuilder, Mootools.net, bbcradio1::MistaJam
http://www.phpbench.com/
Go to the top of the page
+Quote Post
Asmox
post 19.06.2009, 18:23:51
Post #3





Grupa: Zarejestrowani
Postów: 359
Pomógł: 12
Dołączył: 16.01.2009

Ostrzeżenie: (0%)
-----


to mam robić coś w tym stylu if(! mysql_query($sql)) throw new exception


--------------------
Go to the top of the page
+Quote Post
skowron-line
post 19.06.2009, 20:00:48
Post #4





Grupa: Zarejestrowani
Postów: 4 340
Pomógł: 542
Dołączył: 15.01.2006
Skąd: Olsztyn/Warszawa

Ostrzeżenie: (0%)
-----


Cytat(Asmox @ 19.06.2009, 17:23:51 ) *
to mam robić coś w tym stylu if(! mysql_query($sql)) throw new exception

No raczej.


--------------------
I'm so fast that last night I turned off the light switch in my hotel room and was in bed before the room was dark - Muhammad Ali.
Peg jeżeli chcesz uprawiać sex to dzieci muszą wyjść, a jeżeli chcesz żeby był dobry ty też musisz wyjść - Al Bundy.

QueryBuilder, Mootools.net, bbcradio1::MistaJam
http://www.phpbench.com/
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 24.07.2025 - 23:32