A
A
anonim6662015-07-23 12:37:32
PHP
anonim666, 2015-07-23 12:37:32

How to output a specific row from a MySQL database?

Good day !
I recently started learning MySQL and PHP, so I'm still stupid in these languages.
Let's say there is a database with a table and the following lines: id, author, text_name
For example:

id auther text_name
one pupkin news 1
2 Fedorov news 2
3 Sidorov news 3
4 Ivanov news 4

On the page there is a query to the database to display the table:
$db = mysql_connect ("сервер","название","пароль");
mysql_select_db ("название",$db);
mysql_set_charset (utf8);
$result = mysql_query ("SELECT author FROM full_text",$db);
$myrow = mysql_fetch_array ($result);

The following code is usually used to display on the page: The
<? echo $myrow ['author']; ?>
request goes to all lines from the author clause. To display a specific name, you need to specify either a specific id or a specific author name. And here the question itself is: is it possible to make a request for all names from the author item from the table, but so that each name can be displayed on the page in different places in the code? And if so, how?
Thanks in advance )

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Inchin ☢, 2015-07-23
@In4in

Use mysqli , not mysql!

$db = new mysqli("сервер","название","пароль", "БД");
   $result = $db->query ("SELECT author FROM full_text", MYSQLI_USE_RESULT);
   while($row = $db->fetch_row()){
       //Что-то делаем с $row - каждой строкой из таблицы
   }

A
Andrey Pavlenko, 2015-07-23
@Akdmeh

Wow, read further tutorials, you have a complete misunderstanding of the principles of obtaining data.
In order to get all the data, you need to run mysql_fetch_array in a loop:

while(($row=mysql_fetch_array($result))!==false)
{
echo $row['author'];
}

This will print all the names in turn. If you need later in another place, you can do something like this inside the loop:
$data_arr=array();
while(($row=mysql_fetch_array($result))!==false)
{
$data_arr[]=$row['author'];
}

And only then use the data with $data_arr.
Also:
1) mysql_set_charset('utf8'); (it should be a string)
2) decide if the field is called author or auther
3) stop using the mysql library and switch to PDO or at least mysqli. This library is outdated, and it does not work in new versions of php.
www.proft.com.ua/2008/11/28/primery-ispolzovaniya-pdo here is a short article about PDO, otherwise just google it.

A
Alexander Kleoshin, 2015-07-23
@mxSandr

This

$result = mysql_query ("SELECT author FROM full_text",$db);

and there is a request for all names from the "author" column;
Next
With each call to the mysql_fetch_array() function, the next row from the selection is placed in $myrow, i.e. the first time you call echo $myrow ['author'] will display "Pupkin", then you request the next row from the selection
. Now, when you call echo $myrow ['author'] will display the next value, i.e. "Fedorov", etc.
I have explained how it works in general.
In order for you to have an array, $authors, for example, to get names from this array in order, as they go in the database, that is, $authors[0] => "Pupkin", ... , $authors[3] => " Ivanov", you need to do the following:
$result = mysql_query ("SELECT author FROM full_text",$db);

$authors = array();

while ($row = mysql_fetch_array ($result))
{
  $authors[] = $row['author']
}

Now the $authors array contains all the names of the authors from the table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question