Use mysql_fetch_row () function for each row of the result set acquires

In front of us the two ways of getting the result set of data, so we have today to introduce mysql_fetch_row () function to obtain the results of the third set, the function and in front of that function similar usage! Wuxi marble measurement platform

Also function syntax is as follows:

1

array mysql_fetch_row(resource result)

mysql_fetch_row () function associated with the specified from the identification result acquiring a row of data in the result set and returned as an array, this array variable $ row given row, each column stores the results in an array element, from below zero, that is to $ row [0] in the form of access to the first array element (only one is true when the elements), the first call to mysql_fetch_row () function returns the next row in the result set, know no more rows it will return false.

Note: This function returns field names case sensitive!

The following examples of the two preceding articles in the function is the same, except that the following examples are set each record acquisition result by mysql_fetch_row () function is progressive. Then echo statement from the array output result set book information corresponding to each field.

The development of specific steps are as follows:

1. Create a project, add form, connect to the database, the database and selection process to achieve the same with the previous article, here is not repeated, the next is not clear a small partner can see, we have a connection to.

And 2. the previous article, except that the present example using mysql_fetch_row () function progressive obtain the results set record, the core code is as follows:

1

2

3

4

5

6

7

8

9

<?php

$sql = mysql_query("select * from tb_book");

$row = mysql_fetch_row($sql);

if ($_POST["Submit"]=="查询"){

    $txt_book = $_POST["txt_book"];

    $sql = mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'"); //执行模糊查询

    $row = mysql_fetch_row($sql);  //逐行获取查询结果,返回值为数组

}

?>

3. Use the conditional statement if the result set variable $ row judge, if the value is false, then the output you retrieve information does not exist, otherwise do ... while loop information by way of the output of the array of focus, the core code show as below:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<?php

if ($row == false){

    echo "<p align='center' style='color: #FF0000;font-size: 12px'>对不起,你要查询的信息不存在</p>";

}

?>

<?php

do {

    ?>

    <table>

        <tr align="left" bgcolor="#FFFFFF">

            <td height="20" align="center"><?php echo $row[0] ?></td>

            <td height="20" align="center"><?php echo $row[1] ?></td>

            <td height="20" align="center"><?php echo $row[2] ?></td>

            <td height="20" align="center"><?php echo $row[3] ?></td>

            <td height="20" align="center"><?php echo $row[4] ?></td>

            <td height="20" align="center"><?php echo $row[5] ?></td>

        </tr>

    </table>

    <?php

}while ($row = mysql_fetch_row($sql));

?>

Operating results and the results of the previous two articles of the above examples is the same run, the chart pasted here, little friends can try it yourself at the local!

Guess you like

Origin www.cnblogs.com/furuihua/p/12172423.html
Row