N
N
Nikita Filatov2015-07-29 08:15:59
PHP
Nikita Filatov, 2015-07-29 08:15:59

How to use class field in SQL query?

Created a class "DataBase", in its constructor I get the name of the table. This name must be used in functions that execute SQL queries, but for some reason these queries are not executed correctly, that is, they do not receive the value of the field containing the value of the table. Here is the code for the class itself:

class DataBase {

    private static $database_name;
    private static $mysqli;

    public function __construct($database_name = '')
  {
    $hostname_connect = 'localhost';
    $database_conntect = 'webdb';
    $username_connect = 'root';
    $password_connect = 'root';
    $this->database_name = $database_name;

    $this->mysqli = new mysqli(
      $hostname_connect,
      $username_connect,
      $password_connect,
      $database_conntect
      );



    @mysql_query("set_character_set_client='utf8'");
    @mysql_query("set_character_set_results='utf8'");
    @mysql_query("set collation_connection='utf8_unicode_ci'");
  }


  public function add_user($login='', $email='', $password=''){
    $this->mysqli->query("INSERT INTO '$this->database_name' (login, email, password) VALUES ('$login', '$email', '$password')");
  }

  public function user_is_exist($email= ''){
    $result = $this->mysqli->query("SELECT * FROM '$this->database_name' WHERE email LIKE '$email' "); 
    $num_rows = mysqli_num_rows($result);
    if($num_rows == false) {
      return false;
    }
    else return true;
  }

  }

In the case of manual entry of the table name, everything works fine.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Ivanov, 2015-07-29
@M0NSTERC4T

<?php

class DataBase {

    private static $database_name;
    private static $mysqli;

    public function __construct($database_name = '')
  {
    $hostname_connect = 'localhost';
    $database_conntect = 'webdb';
    $username_connect = 'root';
    $password_connect = 'root';
    $this->database_name = $database_name;

    $this->mysqli = new mysqli(
      $hostname_connect,
      $username_connect,
      $password_connect,
      $database_conntect
      );



    @mysql_query("set_character_set_client='utf8'");
    @mysql_query("set_character_set_results='utf8'");
    @mysql_query("set collation_connection='utf8_unicode_ci'");
  }


  public function add_user($login='', $email='', $password=''){


   // DEBUG
    echo "INSERT INTO '$this->database_name' (login, email, password) VALUES ('$login', '$email', '$password')";
    $this->mysqli->query("INSERT INTO '$this->database_name' (login, email, password) VALUES ('$login', '$email', '$password')");
  }

  public function user_is_exist($email= ''){
    $result = $this->mysqli->query("SELECT * FROM '$this->database_name' WHERE email LIKE '$email' "); 
    $num_rows = mysqli_num_rows($result);
    if($num_rows == false) {
      return false;
    }
    else return true;
  }

  }
  
  
$a = new DataBase('test');
$a->add_user('test' , 'test' , 'test');

// INSERT INTO 'test' (login, email, password) VALUES ('test', 'test', 'test')

everything works correctly,
see what the line will give you
better show how you use it

D
Dmitry Entelis, 2015-07-29
@DmitriyEntelis

Oh yoyo.
1) Why are you creating a class for working with a database that is clearly tied to 1 table?
Create a common class for working with the base, inherit the model class from it for specific functionality.
I advise you to read about MVC.
In general, the very idea of ​​\u200b\u200btransferring the name of the table is somehow strange. At you it is a lot of tables in which users lie?
2) Never use the error suppression operator "@"
Code

@mysql_query("set_character_set_client='utf8'");
    @mysql_query("set_character_set_results='utf8'");
    @mysql_query("set collation_connection='utf8_unicode_ci'");
absolutely meaningless, it is not executed - suddenly mysql, and there is no connection in mysql, connection in mysql i
3) Never transfer data to the database without processing. All data must be at least wrapped in mysqli_real_escape_string. Well, or use PDO

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question