Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP][PHP/SQL] Use mysqli or PDO
123KOMORNIK321
post 1.03.2021, 18:42:37
Post #1





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 1.03.2021

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


Cześć, z góry muszę wam powiedzieć że jeszcze nie potrafię pisać w php
Mam taki kod od znaj i spytał się czy mogę mu go przepisać aby nie wyświetlał się błąd

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO

To jest kod

  1. $root = (empty($_SERVER['DOCUMENT_ROOT']) ? getenv("DOCUMENT_ROOT") : $_SERVER['DOCUMENT_ROOT']);
  2. require_once $root.'/config.inc.php';
  3. class Database {
  4.  
  5. var $server = "localhost"; //database server
  6. var $user = "root"; //database login name
  7. var $pass = ""; //database login password
  8. var $database = ""; //database name
  9. var $pre = ""; //table prefix
  10. var $showerror = false;
  11. #######################
  12. //internal info
  13. var $error = "";
  14. var $errno = 0;
  15. //number of rows affected by SQL query
  16. var $affected_rows = 0;
  17. var $link_id = 0;
  18. var $query_id = 0;
  19.  
  20. #-#############################################
  21. # desc: constructor
  22.  
  23. function Database($database) {
  24. $root = (empty($_SERVER['DOCUMENT_ROOT']) ? getenv("DOCUMENT_ROOT") : $_SERVER['DOCUMENT_ROOT']);
  25. require_once $root.'/config.inc.php';
  26. if (isset($database) && ($database == "FORUMS")) {
  27. $this->server = DB_FORUMS_SERVER;
  28. $this->user = DB_FORUMS_USER;
  29. $this->pass = DB_FORUMS_PASS;
  30. $this->database = DB_FORUMS_DATABASE;
  31. $this->pre = DB_FORUMS_PREFIX;
  32. } elseif (isset($database) && $database == "LOGS") {
  33. $this->server = DB_LOGS_SERVER;
  34. $this->user = DB_LOGS_USER;
  35. $this->pass = DB_LOGS_PASS;
  36. $this->database = DB_LOGS_DATABASE;
  37. $this->pre = DB_LOGS_PREFIX;
  38. } elseif (isset($database) && $database == "MTA") {
  39. $this->server = DB_SERVER;
  40. $this->user = DB_USER;
  41. $this->pass = DB_PASS;
  42. $this->database = DB_DATABASE;
  43. $this->pre = DB_PREFIX;
  44. }
  45. $this->showerror = DB_SHOW_ERROR;
  46. }
  47.  
  48. #-#constructor()
  49. #-#############################################
  50. # desc: connect and select database using vars above
  51. # Param: $new_link can force connect() to open a new link, even if mysql_connect() was called before with the same parameters
  52.  
  53. function affected_rows() {
  54. return $this->affected_rows;
  55. }



  1. function connect($new_link = false) {
  2. // var_dump($this->server);die;
  3. $this->link_id = mysql_connect($this->server, $this->user, $this->pass, $new_link);
  4.  
  5. if (!$this->link_id) {//open failed
  6. $this->oops("Could not connect to server: <b>$this->server</b>.");
  7. }
  8.  
  9. if (!@mysql_select_db($this->database, $this->link_id)) {//no database
  10. $this->oops("Could not open database: <b>$this->database</b>.");
  11. }
  12.  
  13. // Deprecated but will get us rolling with mysql 5.7 for now.
  14.  
  15. $this->query("SET SESSION sql_mode = ''");
  16.  
  17. // unset the data so it can't be dumped
  18. $this->server = 'localhost - host name';
  19. $this->user = 'root - user name';
  20. $this->pass = 'pass';
  21. $this->database = 'db - name';
  22. }


Czytałem trochę o mysqli i PDO ale nie jestem w stanie mu tego zrobić i mam prośbę do was
Czy ktoś z was może mi to przepisać ?

PS: Jeśli chodzi o przepisywanie takiego kodu na przyszłość stosować PDO czy mysqli questionmark.gif

Ten post edytował 123KOMORNIK321 1.03.2021, 18:43:42
Go to the top of the page
+Quote Post
viking
post 1.03.2021, 18:55:09
Post #2





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Dział zleceń jest niżej.


--------------------
Go to the top of the page
+Quote Post
vokiel
post 2.03.2021, 12:15:24
Post #3





Grupa: Zarejestrowani
Postów: 2 592
Pomógł: 445
Dołączył: 12.03.2007

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


PDO to wrapper na wiele różnych baz, mysqli to tylko do MySQL/MariaDB.


--------------------
Go to the top of the page
+Quote Post
LowiczakPL
post 2.03.2021, 14:16:36
Post #4





Grupa: Zarejestrowani
Postów: 531
Pomógł: 55
Dołączył: 3.01.2016
Skąd: Łowicz

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


http://www.w3big.com/pl/php/php-mysql-connect.html




--------------------
Szukam zleceń Symfony, Laravel, Back-End, Front-End, PHP, MySQL ...
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: 16.04.2024 - 12:20