Solved

php get value of specific row and column from array of db

Posted on 2013-06-08
3
1,382 Views
Last Modified: 2013-06-08
say i retrive a table select * from xxxxx

if there any simple way that say

$value = $dataRecord...; ( which i can direct specific a row and column )

like row 0 column 1 or row 0 column "title"

expert please help
0
Comment
Question by:john80988
3 Comments
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 500 total points
ID: 39232420
You could simply loop through all the rows, fetching each row as an associative array, and storing that in a bigger array.

For example:

$result = mysql_query("select * from xxxxx");
$index = 0
while ($row = mysql_fetch_assoc($result)) {
    $table[$index] = $row;
    $index++;
}

Open in new window


You could then just access it using $table[<<row number>>]["column_title"].

I wouldn't recommend this if your table is very big, however.  This is a big drain on resources.

If your table is big, then I would recommend a better solution is to simply locate the element you want by modifying the query with the appropriate WHERE clause.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 39232433
hello, yes you can use the  PH{  mysql_data_seek( )  function to set the mySQL Result to any specific numbered Row , you should read this in the manual -
http://us2.php.net/manual/en/function.mysql-data-seek.php

some code might be like -
$result = mysql_query("");
$query = 'SELECT * FROM xxxxx';
$result = mysql_query($query);
if (mysql_data_seek($result, 4)) {// go to row 4
    $row = mysql_fetch_assoc($result),
    }
    else // if the seek Fails then there is NO row 4
    {
    echo "Cannot seek to row 4: " . mysql_error() . "\n";
    }

Open in new window

but you can get different amount of rows from a select, You may need to use the   mysql_num_rows($result) to get the Range of the amount of rows
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39232464
Because MySQL will 're-use' empty space in it's tables, the order of rows is only predictable if you use an ORDER BY clause.  ORDER BY tells MySQL to sort the results by a particular column and return them in that order.  If your table is having additions and deletions, the data you are looking for will not necessarily be in the same numerical row each time.

It is usually best to find a different way to identify the data you want to retrieve.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question