The table itself

There is a bot table in phpMyAdmin. How to make a request for php that will extract the data in the money column of a certain user_id ?

For example, get the value of money for user_id = $id (the value of a column in a variable). After that, the value of money will need to be placed in the $money variable.

Is it possible for an example to solve this situation, so that later you can understand the remaining requests on a ready-made example?

  • And what is your difficulty? - ArchDemon
  • I do not understand how to correctly pull out the value of the column X of the row in which the value of the column Y = 123 - Andrey Lyovushkin
  • 3
    There is more information on the Internet than the number in your money field - n.osennij
  • use the condition WHERE - n.osennij
  • one
    @Andrey Lyovushkin, please indicate in your question (edit it) how you are trying to connect to the database (mysqli, PDO, something else), how you form and send a request for data sampling, and what problem you have with it. Without this information to answer your question is very difficult. - Miron

2 answers 2

The PDO class is used to work with the database in PHP.

DB connection

To connect to MySQL, you need to write such code (taken from here ),

 $host = '127.0.0.1'; $db = 'test'; $user = 'root'; $pass = ''; $charset = 'utf8mb4'; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; try { $pdo = new PDO($dsn, $user, $pass, $options); } catch (\PDOException $e) { throw new \PDOException($e->getMessage(), (int)$e->getCode()); } 

enter your own values ​​for the connection parameters and place in the db.php file.

After that in your script write include 'db.php'; and after that, the $pdo variable will be available in it, which will be used for all database queries.

Request execution

Performing a SELECT query involves 3 stages:

  • preparation of the request, with question marks being put in place of all variables. This creates the $ stmt variable.
  • execution of the request, in which all the variables involved in the request are sent separately, as an array, to the $stmt->execute() method.
  • getting the requested data. Data can be obtained in three different ways.
    • single line as array or object, $stmt->fetch()
    • an array of strings, if the query can return more than one string, $stmt->fetchAll() . After that, the resulting array can be displayed via foreach() .
    • the only value is if we request a single column of a single row, $stmt->fetchColumn()
    • in very rare cases, the acquisition can be performed in a fourth way - sampling one line at a time in the loop, but we will not consider it here.

This is a scheme for performing any queries in which variables are involved. In this case, the code will be as follows:

 include 'db.php'; $stmt = $pdo->prepare('SELECT money FROM bot WHERE user_id=?'); // подготовка $stmt->execute([$id]); // выполнение $money = $stmt->fetchColumn(); // получение 
     $query = 'SELECT * FROM `bot` WHERE `user_id` = "'.mysqli_real_escape_string($mysqli, $id).'"'; 

    The $ mysqli variable is your database connection, if you use a database connection, as in the example:

     $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db'); 

    But you, as a novice programmer, it is advisable to immediately use the connection to the database only through PDO - this is a safer option. In this case, the query syntax in $ query will have to be slightly edited.

    Please edit your question as I wrote in the comments, and then the Stackoverflow community will have more chances to help you.

    • one
      so why give an example of mysqli? - Jean-Claude
    • @ Jean-Claude Mysqli is infected with plague, does not work or do not like you personally? - Miron