开发者

Learning Php MySql queries, How to show the result in PHP?

im just need the easiest way in php to navigate in this result:

开发者_运维技巧

$datos = mysql_query("SELECT * FROM usuarios LIMIT 0, 30 ");

I mean, how do i do an echo from $datos in each element of the table?

Ex. Table:

ID Name      LastName
1  Domingo   Sarmiento
2  Juan      Lopez

How can i read for example the second last name?


mysql_query, when used for SELECT statements, returns a resource which you can use to return the found rows:

$result = mysql_query("SELECT * FROM usuarios LIMIT 0, 30 ");

while ($row = mysql_fetch_assoc($result)) {
    echo $row['ID'];
    echo $row['Name'];
    echo $row['LastName'];
}

Refer to the linked documentation for additional uses and functions.

Since you are learning PHP/MySQL it would behoove me to point out the preferred method of interacting with databases: PDO. PDO affords a consistent interface to all supported databases with added benefits such as prepared statements and lowered injection risk, to name a couple.


You could try mysql_data_seek

Here's one of the examples from the site:

<?php

$query="SELECT * FROM `team` ORDER BY `team`.`id` ASC";
$result=mysql_query($query);
$last_row = mysql_num_rows($result) - 1;
if (mysql_data_seek($result, $last_row)) { //Set Pointer To LAST ROW in TEAM table.

     $row = mysql_fetch_row($result); //Get LAST RECORD in TEAM table
     $id = $row[0] + 1; //New Team ID Value
     // more code here ...

} else { //Data Seek Error

    echo "Cannot seek to row $last_row: " . mysql_error() . "\n";       

}

?>


The PHP manual is a good start to use mysql_* functions.

What you are trying to do is loop through your result.

$datos = mysql_query("SELECT * FROM usuarios LIMIT 0, 30 ");
while ($row = mysql_fetch_array($datos, MYSQL_BOTH)) {
    printf("ID: %s  Name: %s LastName: %s", $row[0], $row[1], $row[2]);  
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜