Hello phpers!
I have a short PHP code not detecting a key word (eg. “PRI”) in a result set from a php:mysql query.
Would greatly appreciate some help …
Mysql Query return dataset is here that below PHP scripts returns.
mysql> show columns from PRODUCT_IMAGES;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| IMAGE_ID | int(50) | NO | PRI | NULL | auto_increment |
| BC_ID | varchar(11) | YES | | NULL | |
| PHOTO_NAME | varchar(50) | NO | | NULL | |
| STATUS | int(50) | NO | | NULL | |
+------------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
The PHP code below is included in one or our websites as a template from a 3PA that is no-longer supported.
It’s supposed to detect that the above IMAGE_ID Column is the (PRI)mary key of the table and then “return $primary” . However, it seems to be not working. I’m new to php (after a couple of years plonking away at it) and i can’t decipher how this array() is being manipulate and why its not working, ie. it is not saying that $primary > 0…
I can easily handle this sort of thing in ksh/bash(awk/grep/sed etc), PL/SQL and SQL… etc but not in PHP. So any help would be greatly appreciated.
Thanks!
K
Some info about the calls and vars set in the following script:
database.php = handles the connect string to the database -- works fine for all our PHP pages.
$tbl = is passed in (and confirmed) as PRODUCT_IMAGES
[php]
<?php require_once("database.php"); function get_primary($tbl) { global $Fields; $q1 = "show columns from `$tbl`"; $r1 = mysql_query($q1); while($ARR = mysql_fetch_array($r1)) { if ($ARR[3]== "PRI"&in_array($ARR[0],$Fields)) { $primary[] = $ARR[0] ; } } return $primary; } ?>[/php]
I have tested the $result with the following query (removed the database.php to harcode the connection) here’s the code and results:
[php]
<?php //--- SCRIPT: test_cols.php $connect = mysql_connect('localhost', 'username', 'password'); mysql_select_db('DATABASENAME', $connect); $result= mysql_query("show columns from PRODUCT_IMAGES"); while ($row = mysql_fetch_assoc($result) ) { $Field = $row['Field']; $Type = $row['Type']; $Null = $row['Null']; $Key = $row['Key']; echo $Field."|"; echo $Type."|"; echo $Null."|"; echo $Key."|\n"; }; ?>[/php]
Here’s the output:
$ php -f test_cols.php
IMAGE_ID|int(50)|NO|PRI|
BC_ID|varchar(11)|YES||
PHOTO_NAME|varchar(50)|NO||
STATUS|int(50)|NO||
$