Solved

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

Posted on 2013-06-08
3
1,366 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

864 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now