Results 1 to 10 of 10
Back Story:
Hired to do PHP/MYSQL work on a race website.
Need to dump MYSQL database into Windoze spreadsheet (though that part works just fine)
This is what works:
PHP ...
- 12-21-2011 #1Just Joined!
- Join Date
- Nov 2010
- Location
- Alaska
- Posts
- 55
PHP/MYSQL: 2 similar codes, one fails
Back Story:
Hired to do PHP/MYSQL work on a race website.
Need to dump MYSQL database into Windoze spreadsheet (though that part works just fine)
This is what works:
This dumps a spreadsheet of 2011's racers. Now, I need a dump of all racers ever. When I try replacing $sql with "SELECT * FROM PARTICIPANTS", it doesn't work for an unknown reason. I know for a fact that it's not a server or Apache problem.PHP Code:
$sql = "SELECT * FROM participants AS T1, race2011 AS T2 WHERE T1.id = T2.racerid";
$result = mysql_query($sql);
while($cols = mysql_fetch_assoc($result) ){
if( !isset($columnLabels) ){
foreach($cols AS $key => $value){
$columnLabels[] = ucwords( str_replace("_", " ", $key) );
}
$row[] = $columnLabels;
}
$row[] = $cols;
}
I @THINK@ that the search results aren't being stored to $row properly, but I have no idea. Can someone please help me? I've been working on this all day and can't manage to figure this one out. Thanks in advance.Last edited by MisterDood; 12-21-2011 at 12:16 AM. Reason: Forgot to change something in code
- 12-21-2011 #2
"it doesn't work" is not very informative.
Does $result get populated? Does $cols work? Do the loops happen? Add print statements to the various parts of the code so you can see what the status of the variables are. And when you say "it doesn't work", what do you mean? $row doesn't get set correctly?DISTRO=Arch
Registered Linux User #388732
- 12-21-2011 #3
In mysql tables are files so if this is running on a Linux server, PARTICIPANTS is not the same as participants. This is a common gotcha when developing on a Windows box and deploying to Linux!
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 12-21-2011 #4
SQL is generally case-insensitive. Here is an example from sqlite:
Code:sqlite> INSERT INTO test.test VALUES ("alex"); sqlite> SELECT * FROM test.test; alex sqlite> SELECT * FROM test.TEST; alex sqlite> SELECT * FROM TEST.TEST; alexDISTRO=Arch
Registered Linux User #388732
- 12-21-2011 #5
Agreed but in MySQL, MYISAM table names certainly are case sensitive if the underlying file system is case sensitive. A MYISAM table is stored as three files
TableName.frm which is the structure of the data
TableName.myd which is the actual data
TableName.myi which is your indexes.
To be honest, I can't remember if the engines like INNODB are are also case sensitive on the table names but with MySQL, it is safer to assume that it is!If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 12-21-2011 #6Just Joined!
- Join Date
- Nov 2010
- Location
- Alaska
- Posts
- 55
Sorry for not being informative; I figured it would be kind of obvious since I've only been using PHP/MYSQL for about 4 days. I also had a really bad headache from this. Regardless, I'm going to run through this one more time, and post my test results:
That displays "Resource id #9". This is good so far.PHP Code:$sql = "SELECT * FROM participants"
$result = mysql_query($sql);
echo $result;
This says "Array" a lot. Probably once for every row in the table (about 6,000).PHP Code:<...>
while($cols = mysql_fetch_assoc($result) ){
echo $cols;
}
Displays the name of each column name from the MYSQL databasePHP Code:while($cols = mysql_fetch_assoc($result) ){
if( !isset($columnLabels) ){
foreach($cols AS $key => $value){
$columnLabels = ucwords( str_replace("_", " ", $key) );
echo $columnLabels;
}
}
}
When I replace $columnLabels in the line "$columnLabels = ucwords( str_replace("_", " ", $key) );" with $columnLabels[], it says Array once for each element in the array (which makes sense).
This says "Array" once, which makes sense. echo var_dump($row); tells me that the array $row is holding a single array ($columnLabels). That array holds the column names from the MYSQL database (as expected).PHP Code:while($cols = mysql_fetch_assoc($result) ){
if( !isset($columnLabels) ){
foreach($cols AS $key => $value){
$columnLabels[] = ucwords( str_replace("_", " ", $key) );
}
$row[] = $columnLabels;
}
}
echo $row;
It wont display t, but when I comment out the line "$row[]=$cols", it displays t. As far as I know, that means the line "$row[] = $cols;" is my problem. I can think of a few things this could mean, but I'm sure it's not a server issue since it worked before the switch.PHP Code:while($cols = mysql_fetch_assoc($result) ){
if( !isset($columnLabels) ){
foreach($cols AS $key => $value){
$columnLabels[] = ucwords( str_replace("_", " ", $key) );
}
$row[] = $columnLabels;
}
$row[] = $cols;
}
echo t; // t doesn't need to be quoted
Also, sorry for a long post, I tried my best to not repeat stuff. Is there a way to do a hide/show box or something?Last edited by MisterDood; 12-21-2011 at 09:56 PM. Reason: Replacing a pronoun to avoid confusion
- 12-23-2011 #7
You're probably wasting a lot of memory there. Try to limit the output data to a few records:
Code:$sql = "SELECT * FROM participants LIMIT 10"
- 12-23-2011 #8Just Joined!
- Join Date
- Nov 2010
- Location
- Alaska
- Posts
- 55
First of all, I need the entire table to be in the document. If you're saying I should loop the function you listed and append everything to my document, I'm not sure if that would work. I'd have to use this general idea:
Would that not waste a lot of memory?PHP Code:$len = /*Dynamic way to find last ID*/
$i;
while $i<$len{
foreach($i AS $i){
<file append function with row[$i] and file>
}
}
generate_file();
- 12-23-2011 #9
Instead of echo $row; use var_dump($row); and you will see the contents of the array. If you do it inside the loop, you will see a lot of data. It may shed some light.
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 12-28-2011 #10
I just wanted to point out that developing can be done with a small subset of the original data set. That should speed up some things during the development phase. Once your code works as you expect, you can still do optimizations if needed. That said, taking everything that comes from a database and storing it in memory is most often a bad idea. If the set becomes bigger and bigger, consider to use temporary files.


Reply With Quote
