0

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?

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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";       

}

?>

Comments

0

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]);  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.