MYSQL - select a specific value from the selected array

I have a little problem, and since I am very new to all of this, I have not been successful when searching on Google because I do not know the exact definitions for what I am looking for.

I have a very simple database and I get all the rows:

while($row = mysql_fetch_array($result)){ echo $row['id']. " - ". $row['name']; echo "<br />"; } 

Now, my question is: how do I filter the second result? I thought something like this might work, but it is not:

 $name2= $row['name'][2]; 

Is it possible? Or do I need to write another mysql query (something like SELECT .. WHERE id = "2" ) to get the name value in the second line?

I am doing my best:

-get all data from the database (with a "while" loop), but individually display specific results on my page. For example echo("name in second row") and echo("id of first row") , etc.

+4
source share
9 answers

If you prefer to work with a full set of results instead of iterating over them only once, you can put the entire array of results in an array:

 $row = array(); while( $row[] = mysql_fetch_array( $result ) ); 

Now you can access individual records using the first index, for example, the name field of the second line is in $row[ 2 ][ 'name' ] .

+12
source
 $result = mysql_query("SELECT * FROM ... WHERE 1=1"); while($row = mysql_fetch_array($result)){ /*This will loop arround all the Table*/ if($row['id'] == 2){ /*You can filtere here*/ } echo $row['id']. " - ". $row['name']; echo "<br />"; } 
+2
source
 $counter = 0; while($row = mysql_fetch_array($result)){ $counter++; if($counter == 2){ echo $row['id']. " - ". $row['name']; echo "<br />"; } } 
0
source

Yes, ideally you need to write another sql query to filter your results. If you have:

 SELECT * FROM Employes 

then you can filter it with:

 SELECT * FROM Employes WHERE Name="Paul"; 

if you want all names starting with P can be achieved with:

 SELECT * FROM Employes WHERE Name LIKE "P%"; 

The main reason for using an SQL query to filter your data is that database manager systems such as MySQL / MSSQL / Oracle / etc. are highly optimized and faster than the server-side condition block in PHP.

0
source

This While loop will automatically retrieve all records from the database. If you want to get any other field, you will only need to use this.

0
source

Depends on what you want to do. mysql_fetch_array () retrieves the current row pointed to by the resource pointer right now. This means that you do not have $row['name'][2]; . At each iteration of the while loop, you have all the columns from your query in the $ row array, you do not immediately get all the rows from the query in the array. If you need only one row, then yes - add a WHERE to the query, do not extract other rows if you do not need them. If you need all the lines, but you want to do something special when you get the second line, then you need to add a counter that checks which line you are currently working on. I.e:.

 $count = 0; while($row = mysql_fetch_array($result)){ if(++$count == 2) { //do stuff } } 
0
source

If you want to be able to use two consecutive results in one cycle, you can save the results of the first cycle and then execute the cycle.

 $initial = true; $storedId = ''; while($row = mysql_fetch_array($result)) { $storedId = $row['id']; if($initial) { $initial = false; continue; } echo $storedId . $row['name']; } 

This only works for sequential things. Please excuse the syntax errors, I have not programmed in PHP for a very long time ...

0
source

If you always want a second row, no matter how many rows you have in the database, you should change your query this way:

 SELECT * FROM theTable LIMIT 1, 1; 

See: http://dev.mysql.com/doc/refman/5.5/en/select.html

0
source

I used the code from the answer and changed it a bit. I think I would share.

 $result = mysql_query( "SELECT name FROM category;", db_connect() ); $myrow = array(); while ($myrow[] = mysql_fetch_array( $result, MYSQLI_ASSOC )) {} $num = mysql_num_rows($result); 

Usage example

 echo "You're viewing " . $myrow[$view_cat]['name'] . "from a total of " . $num; 
0
source

All Articles