Good day.

The question is related to the output of data in the database table.

Suppose there is a table of the following type:

id title 1 первая строка 2 вторая строка 3 третья строка

Let's call this table table1. Execute SQL query:

 SELECT title FROM table1 WHERE id 

After that in PHP after connecting to the database I am writing the following query to the database:

  $db = mysqli_connect("HOST","name","passowrd"); mysqli_select_db($db, "name_db"); $title = "SELECT title FROM table1 WHERE id"; $result = mysqli_query($db, $title); $titlerows = mysqli_fetch_row($result); 

Is done.
After that I try to pull the rows in the title column.
echo $titlerows[0];
Show the contents of the first title:
первая строка
However, the following query:
echo $titlerows[1];
Nothing derives. Although I expected the following: вторая строка
Help with the management of a specific table column, with the ability to display data on id indices. Thank you in advance.

  • In general, what does it mean "WHERE id" what is this unfinished condition .. - Denis Kotlyarov
  • In this case, I think the condition can be omitted. Because If you apply a search for a specific line WHERE id = 1, then to search for the next line you need a new query. - Dmitriy Doronin
  • + If a specific string is necessary, "WHERE id = 1 Limit 1;" so that muscle after finding line 1 did not climb and did not look for another line with id 1. - Denis Kotlyarov

1 answer 1

The point is that the mysqli_fetch_row() function for one call retrieves only one row from the resulting table. To pull out the second line, you must call the function again. Often, to go through all the tables, arrange a loop with while

 $db = mysqli_connect("HOST","name","passowrd"); mysqli_select_db($db, "name_db"); $title = "SELECT title FROM table1"; $result = mysqli_query($db, $title); while($titlerows = mysqli_fetch_row($result)) { echo $titlerows[0].'<br />'; } 

Using the indices in the $titlerows , you can refer to column numbers, but not rows.

PS In addition, pay attention to the WHILE id - there is no syntax error here, but there is not much point in this condition either. In the current version, you can simply omit it and extract all the rows.

  • Thank. Led the entire list. Actually the most important question is this. How to display not the entire list, but the line of interest. Most likely the id is needed here. Those. inside the function, there must be a condition by which it will search for all strings and if it finds id = 2, for example, it will only output this string. - Dmitriy Doronin
  • @DmitriyDoronin If you need a separate line SELECT title FROM table1 WHERE id = 2 - just execute the query and call mysqli_fetch_row ($ result) once, moreover, you can add LIMIT 1 even at the end of the query to limit the output to only one line. If you expect several rows, it is better to always go over the resulting table in a loop and form the resulting array. Further work with the resulting array. Those. in the while () loop, we do not output echo data, but store the necessary elements into a separate array, say $ result. - cheops
  • @DmitriyDoronin if you need only one value at all, instead of the function mysqli_fetch_row (), it is better to use the function mysqli_result () - which retrieves from the resulting table, not an array, but one value. - cheops
  • Good. If we omit the WHERE clause, it turns out we are unloading all the title values ​​from the table. When calling a while loop, all values ​​are unloaded. Is it possible to select rows by index of the resulting array? - Dmitriy Doronin
  • one
    @DmitriyDoronin In this case, you must create your own array $ results = array (); while ($ titlerows = mysqli_fetch_row ($ result)) {$ results [] = $ titlerows [0]; } and then, yes, you can access it by the $ results [0] index, $ results [1] index, $ results [2] index, etc. This index will denote exactly the rows in your result table. - cheops