Forum Moderators: coopster
while ($record = mysql_fetch_array($result) ) {
$records[] = $record;
}
Two questions.
1. Should I use serialize/unserialize or simply save $records as a session var. I ultimately use the same page to output the data but I allow the user to sort the data by different columns and I allow them to reduce the result set by selective criteria (like 'county', 'state', 'color').
2. I'm tripping over outputting the array. The code I'm using is:
foreach ($records as $row) {
echo "<p>".$row[column1]."</p>";
}
The script outputs the letter 'A' once for each record and then outputs the column data as I expected it to. I'm feeling a bit dense but at the risk of embarrasment, where's the bloodly 'A' coming from!?
I guess I should explain my thought process here. In addition to storing the data in the associative indices of the result array, mysql_fetch_array also stores the data in numeric indices. It may be where your "A" is coming from...you're probably getting the "A" in "Array".
[edited by: coopster at 5:56 pm (utc) on Oct. 2, 2003]
This leads me to the first of my questions - serialize versus sessions?
Which would lead to question number 2 once again. And I don't have an answer or even a clue as to what is happening there...yet.
I will execute a new query if need be. Sorting the data isn't a problem as much as getting the data initially.
Let's back up a bit and let me fill you in - you see I have three tables.
[T1]
records about businesses: biz name, owner name, address, phone, etc...
[T2]
business types - admin can add/edit/delete these at any time: engineering firm, interior decorator, seo experts
[T3]
biz2type matching table: ids from both T1 and T2 are paired here to give me a picture of what the business does
So when a query for the businesses is run (lets say all of them) the script queries T1 for everyone.
$query = "SELECT *
From biz";
$result = mysql_query($query);
$num_all = mysql_num_rows($result);
Then on the output, I had used a 'for' loop to output the query results like so:
for($i=0;$i<$num_all;$i++) {
$business = mysql_fetch_array($result);
output something
}
But in order to determine the 'type' of business I resorted to running another query in that for loop to search for the business type associated with the business. On 300 records the pause was noticeable.
I'm playing with a complex join to avoid the extra query but haven't been able to get the syntax right - if it's even possible. So that led me to the idea of caching the initial query and then doing the same with the second query. It's a stop gap method and I'd gladly drop it if I can get the join to work. My thinking is that the JOIN statement should eliminate all hoops I'm jumping through.
Still in the bunker ;) -- coopster
biz name - biz type 1
biz name - biz type 2 etc..
and should look like
biz name - biz type 1, biz type 2
I've been trying to condition it within the context of my SELECT query. In that thread you're filtering the result output through PHP conditional statements. If that's correct - it's been a long day and I'm not thinking clearly - then I should be able to handle that!
I tried a bunch of stuff, including:
$_SESSION['result'] = serialize($result);
I'm checking the $_SESSION array right before my 1st page quits, and what gets stored in there is
i:0;
..which appears to be the representation for the integer 0. When I unserialize() it it gives me 0.
Has anyone run into this before?
Am I better off doing this some other way?
Thanks-
>> Am I better off doing this some other way?
Depends upon what you're trying to do. As you can see above what I started out trying to do was the wrong way to go about it and I ultimately changed course (thanks to a dope slap from coopster) and used the method that better suited what I was trying to accomplish.
SO, what is it you're trying to do!?
What I would like to do is have the results page store the MySQL result resource in a session variable so that if the user clicks a column heading to do a sort, then PHP can do this without issuing a new MySQL query.
I'm beginning to think I'd be better off just chunking through the rows and storing them in the session instead of the MySQL result resource. Ideas? Thanks in advance.
Other thoughts. You could build a temporary table and place the results in that and then sort accordingly.
Alternately you could stuff everything into an array as I was trying to do and then save and sort the array.
How many records are we talking about searching through?
I will try stuffing the rows into an array. I suspect that will work fine. Right now I'm working with just a handful of records (less than a hundred), but I want to build my system to be scalable to much larger datasets.
Thanks again-
I tried a bunch of stuff, including:$_SESSION['result'] = serialize($result);
I'm checking the $_SESSION array right before my 1st page quits, and what gets stored in there is
i:0;
..which appears to be the representation for the integer 0. When I unserialize() it it gives me 0.