Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the first row of the mysql resource string?

Here is my problem. I need more than one row from the database, and i need the first row for certain task and then go through all the list again to create a record set.

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = //extract first row from database
//add display some field from it


while($row = mysql_fetch_assoc($result)) {
   //display all of them
}

Now, how to extract just the first row?

like image 727
mrN Avatar asked Jan 06 '11 11:01

mrN


People also ask

How do you get a portion of a string using MySQL?

SUBSTRING() function in MySQL function in MySQL is used to derive substring from any given string . It extracts a string with a specified length, starting from a given location in an input string. The purpose of substring is to return a specific portion of the string.


1 Answers

you can use Object oriented style :

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

if ( $row = $result->fetch_assoc()){
    $firstRow = $row;
    mysql_data_seek($result, 0);

    while( $row = $result->fetch_assoc()) {
       //display all of them
    }
}
like image 193
Anouar khaldi Avatar answered Sep 20 '22 18:09

Anouar khaldi