Hello. There is a MySQL
table containing two fields - id
and text
. The id
field for every 5-6 elements is one, text
different. You need to take the first element with the desired id
and paste it into a specific piece of code, and then take the second element with the same id
and paste it into another piece of code. I pulled out the first two elements, then did the following: $row_images=mysql_fetch_array($result_images)
. It is not possible to get the first and second elements using the $row_images['text'][1]
or ['text'][2]
method, and it is also impossible to do the same through while($row_images=mysql_fetch_array($result_images)){}
. In general, two elements with one string need to be placed in different variables. How can this be done? Thanks in advance for the answers.
- Why not? Give exactly your code. - Oleg Arkhipov
- 2depricated This extension is deprecated since PHP 5.5.0 and will be removed in the future. Use MySQLi or PDO_MySQL instead. See also the MySQL instruction: API selection and the corresponding FAQ for more details. Alternatives to this function: mysqli_connect () PDO :: __ construct () - zb '24
- have already deleted) - Pavel Dura
|
1 answer
The mysql extension is obsolete and excluded from the new version of PHP 7. It is best to use the PDO extension
<?php try { $pdo = new PDO( 'mysql:host=localhost;dbname=test', 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); $query = "SELECT `text` FROM tbl WHERE id = :id"; $tbl = $pdo->prepare($query); $tbl->execute(['id' => 3]); $row_images = $tbl->fetchAll(PDO::FETCH_COLUMN); echo "<pre>"; print_r($row_images); echo "</pre>"; } catch (PDOException $e) { echo "Ошибка выполнения запроса: " . $e->getMessage(); }
Using the fetchAll()
method, you can retrieve all the records returned by the query, specifying the PDO::FETCH_COLUMN
constant as the first parameter, you will simplify the resulting array to the state
Array ( [0] => Первая запись [1] => Вторая запись ... [5] => Шестая запись )
Then you can replace the values, focusing on the order in the array. If necessary, elements can be sorted at the SQL query level using the ORDER BY
.
|