CentOS 6
PHP 5.2
MySQL 5.0.96
Having a problem with a mysql result resource and one column in one table.
I can query all columns in that table, except for one and except for *.
In each failed case, I get mysql_num_rows, but asking for mysql_fetch_array or mysql_fetch_assoc returns an error, ONLY when I include that column in the query.
NOTE: ALL of the following queries work perfectly from the command line.
In weird addition, subsequent queries that request data from this same table, the same column as the problem column described here, work perfectly.
When using an alias in the query, the error goes away ... unless I try to USE that alias in the result, in which case the resource again fails.
Weirdest thing I have ever seen. It MUST be a problem with PHP and the column or its data, but I can't find any way around it.
First, the table definition:
describe SHIP_OUT;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| SO_ID | int(11) | NO | PRI | NULL | auto_increment |
| SO_TKNM | varchar(64) | YES | | NULL | |
| SO_DATE | varchar(24) | YES | | NULL | |
| SO_BY | varchar(24) | YES | | NULL | |
| SO_PICKER | varchar(32) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
Then a simple query using one column (not the weird one):
query: select SO_PICKER from SHIP_OUT
mysql_num_rows for Resource id #18: 659
I can do that all day long with every column except the weird one, like so:
query: select SO_ID,SO_DATE,SO_BY,SO_PICKER from SHIP_OUT
mysql_num_rows for Resource id #18: 659
Let's add the problem column, SO_TKNM:
query: select SO_ID,SO_DATE,SO_BY,SO_PICKER,SO_TKNM from SHIP_OUT
mysql_num_rows for Resource id #18: 659
Warning: mysql_fetch_array(): 18 is not a valid MySQL result resource
Nope. Maybe we just grab everything?:
query: select * from SHIP_OUT
mysql_num_rows for Resource id #18: 659
Warning: mysql_fetch_array(): 18 is not a valid MySQL result resource
Nope. Let's use an alias for the problem column, instead:
query: select SO_TKNM as TKNM from SHIP_OUT
mysql_num_rows for Resource id #18: 659
No error! .. but ... trying to USE that result:
while($row=mysql_fetch_array($doq)) {
echo "TKNM: ".$row['TKNM'];
}
produces:
query: select SO_TKNM as TKNM from SHIP_OUT
mysql_num_rows for Resource id #18: 659
Warning: mysql_fetch_array(): 18 is not a valid MySQL result resource
Whaaaat?!?! Seriously. AFTER the query has run, and the array is assigned to a variable ...THEN it throws the error.
And just trying to grab that one column produces ... you guessed it:
query: select SO_TKNM from SHIP_OUT
mysql_num_rows for Resource id #18: 659
Warning: mysql_fetch_array(): 18 is not a valid MySQL result resource
- I have destroyed and rebuilt the table with no data and manually added simple data, with no joy.
- I have destroyed and rebuilt the table using the existing data ... in that column it's all just short strings of numbers and letters, nothing weird.
- I have tried quoting the column name (
SO_TKNM
) to prevent it's name from being treated as a command ...but no joy.
Any thoughts, however crazy or stupidly simple are welcome. This is driving me batty! TIA.
== EDIT: ADDING PHP CODE =====
Here's my PHP code, first the original:
$nomatchq = "select SO_TKNM from SHIP_OUT where SO_DATE >= '20140520' AND SO_DATE <= '20140521' order by SO_TKNM";
$doq = mysql($DBName,$nomatchq);
while($row=mysql_fetch_array($doq)) {
echo "TKNM: ".$row['SO_TKNM'];
}
Here's the way I generated the sample output, above:
$nomatchq = "select SO_TKNM from SHIP_OUT where SO_DATE >= '20140520' AND SO_DATE <= '20140521' order by SO_TKNM";
echo "query: ".$nomatchq."<br>";
$doq = mysql($DBName,$nomatchq);
echo "mysql_num_rows for ".$doq.": ".mysql_num_rows($doq)."<br>";
while($row=mysql_fetch_array($doq)) {
echo "TKNM:".$row['SO_TKNM'];
}
And here's the use of the alias:
$nomatchq = "select SO_TKNM as TKNM from SHIP_OUT where SO_DATE >= '20140520' AND SO_DATE <= '20140521' order by SO_TKNM";
echo "query: ".$nomatchq."<br>";
$doq = mysql($DBName,$nomatchq);
echo "mysql_num_rows for ".$doq.": ".mysql_num_rows($doq)."<br>";
while($row=mysql_fetch_array($doq)) {
echo "TKNM:".$row['TKNM'];
}
I'm sorry I didn't post the PHP code, earlier. There are a set of methods that make "mysql()" function as shown in dozens of places around the sites. For the way this server is set up, it's normal.
Does the error make sense to anyone?
Is there any reason why mysql_num_rows would return the correct result count but one line later that resource is not recognized as valid by mysql_fetch_array or mysql_fetch_assoc?
Please let me know if there is more info you need.
And I do appreciate the comment about pdo/mysqli/prepared statements.