Problem with fetching values...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

I have to code a compatibility  layer  under  PHP5  for  PostgreSQl  and
MySQL, which mean, I have to code something like db_query()  which  then
can be used with mysql_query() and pg_query().

But now I have hit an weird error, since in PostgreSQL it is wotking but
in MySQL it get the right number of values but NO the value itself.

I use:

----[ '~/.tdphp-vserver/includes/database_mysql.inc' ]------------------
<snip>
} elseif (DB_TYPE == 'mysql') {

  function db_connect($host=DB_HOST, $port=DB_PORT, $database=DB_NAME, $user=DB_USER, $pass=DB_PASS, $link='db_link') {
    global $$link;
    $$link=mysql_connect($host . ':' . $port, $user, $pass);
    if (!$$link) { db_error($query, '', mysql_error()); }
    $tmp=mysql_select_db($database, $$link);
      if (!$tmp) { db_error($query, '', mysql_error()); }
    return $$link;
  }

  function db_close($link='db_link') {
    global $$link;
    return mysql_close($link);
  }

  function db_error($query, $errno, $error) {
    die("<FONT size=\"+2\" color=\"red\"><B>Error $errno</B></FONT><HR size=\"+3\" noshade>$error<P>$query");
  }

  function db_query($query) {
    global $$link;
    $result=mysql_query($query) or db_error($query, '', mysql_error($$link));
    return $result;
  }

  function db_fetch_array($db_query) {
    return mysql_fetch_array($db_query, MYSQL_NUM);
  }

  function db_free_result($db_query) {
    return mysql_free_result($db_query);
  }

  function db_fetch_fields($db_query) {
    return mysql_fetch_fields($db_query);
  }

  function db_input($string) {
    return mysql_real_escape_string($string);
  }
------------------------------------------------------------------------

and the code which produce the error is:

----[ '~/.tdphp-vserver/includes/10_electronica_admin.inc' ]------------
<snip>
function setupOverviewProjects($type='projects') {
  if ($type == 'sub_projects') {
    $VAL1="Current Sub-Projects";
    $VAL2="sub_projects";
  } else {
    $VAL1="Current Main-Projects";
    $VAL2="projects";
  }

  $output  = "<div class=\"setupOverviewBorder\">\n";
  $output .= "<div class=\"setupOverviewTitle\">" . T_gettext($VAL1) . "</div>\n";
  $output .= "<br />\n";
  $output .= "  <ol class=\"setupOverviewNameList\">\n";

  $DB_connect=db_connect()
    or die('Could not connect: ' . db_error());

  $DB_query='SELECT serno,name,desc_short FROM ' . $VAL2 . ' ORDER BY serno';
  $DB_result=db_query($DB_query, $DB_connect)
    or die('Query failed: ' . db_error());

  while ($DB_list=db_fetch_array($DB_result)) {
    $output .= "    <li><a href=\"/?what=admin&where=" . $VAL2 . "&action=edit&serno=$DB_list[serno]&referer=" . urlencode("http://"; . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']) . "\"><span class=\"setupOverviewName\">Argh... " . $DB_list['name'] . "</span></a>";
    if ($_COOKIE[$VAL2 . '_desc_short'] == 'show') {
      $output .= "<dt />" . $DB_list['desc_short'];
    }
    $output .= "</li>\n";
  }

  db_free_result($DB_result);
  db_close($DB_connect);
------------------------------------------------------------------------

and then I get in the Webpage something like:

----[ STDIN ]-----------------------------------------------------------
Current Main-Projects

   1. Argh...
   2. Argh...
   3. Argh...
   4. Argh...
   5. Argh...
   6. Argh...
   7. Argh...
   8. Argh...
   9. Argh...
  10. Argh...

Add new Project | Show short describtion
------------------------------------------------------------------------

which mean, the db_fetch_array() give the right number of  entries  back
since it count up to 10, but it can not get the value $DB_list['name'].

The SQL data for the this part is:

----[ '~/BACKUPS/electronica.sql']--------------------------------------
<snip>
--
-- Tabellenstruktur fÃŒr Tabelle `projects`
--

CREATE TABLE `projects` (
  `serno` bigint(20) unsigned NOT NULL auto_increment,
  `active` int(11) NOT NULL default '0',
  `name` varchar(50) collate latin1_german2_ci NOT NULL,
  `desc_short` varchar(250) collate latin1_german2_ci NOT NULL,
  `desc_long` varchar(16000) collate latin1_german2_ci NOT NULL,
  `sub_projects` varchar(1000) collate latin1_german2_ci default NULL,
  `photos` varchar(1000) collate latin1_german2_ci default NULL,
  PRIMARY KEY  (`serno`),
  UNIQUE KEY `serno` (`serno`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=11 ;

--
-- Daten fÃŒr Tabelle `projects`
--

INSERT INTO `projects` VALUES (1, 0, '24V DC Modular ATX PSU', 'foo', '', '', '');
INSERT INTO `projects` VALUES (2, 0, '24V DC Modular Solar-Wind-Charger and Distributor', 'foo', '', '', '');
INSERT INTO `projects` VALUES (3, 0, '24V DC Multichemistry Charger', 'foo', '', '', '');
INSERT INTO `projects` VALUES (4, 0, 'GSM Modem', 'foo', '', '', '');
INSERT INTO `projects` VALUES (5, 0, 'GSM Router', 'foo', '', '', '');
INSERT INTO `projects` VALUES (6, 0, 'Outdoor HandBag TablePC', 'foo', '', '', '');
INSERT INTO `projects` VALUES (7, 0, 'Industrial TablePC', 'foo', '', '', '');
INSERT INTO `projects` VALUES (8, 0, 'MicroPC', 'foo', '', '', '');
INSERT INTO `projects` VALUES (9, 0, 'MicroServer', 'foo', '', '', '');
INSERT INTO `projects` VALUES (10, 0, 'USB Gadget', 'foo', '', '', '');
<snip>
------------------------------------------------------------------------

Now while checking with:

----[ '~/.tdphp-vserver/includes/10_electronica_admin.inc' ]------------
<snip>
#  while ($DB_list=db_fetch_array($DB_result)) {
#    $output .= "    <li><a href=\"/?what=admin&where=" . $VAL2 . "&action=edit&serno=$DB_list[serno]&referer=" . urlencode("http://"; . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']) . "\"><span class=\"setupOverviewName\">Argh... " . $DB_list['name'] . "</span></a>";
#    if ($_COOKIE[$VAL2 . '_desc_short'] == 'show') {
#      $output .= "<dt />" . $DB_list['desc_short'];
#    }
#    $output .= "</li>\n";
#  }
  echo "<pre>\n";
  while ($DB_list=db_fetch_array($DB_result)) {
    print_r ($DB_list);
  }
  echo "</pre>\n";
  exit();
<snip>
------------------------------------------------------------------------

I have gotten something this:

----[ STDIN ]-----------------------------------------------------------
Array
(
    [0] => 1
    [1] => 24V DC Modular ATX PSU
    [2] => foo
)
Array
(
    [0] => 2
    [1] => 24V DC Modular Solar-Wind-Charger and Distributor
    [2] => foo
)
Array
(
    [0] => 3
    [1] => 24V DC Multichemistry Charger
<snip>
------------------------------------------------------------------------

So, PostgreSQL catch the array by "name"

    pg_fetch_array($db_query, null, PGSQL_ASSOC)

and MySQL use the "position"

    mysql_fetch_array($db_query, MYSQL_NUM)

But positional values do not work, since the sequence can change...

How can I get the db_fetch_array($DB_result) to give me the name instead
the numerical position?

I need:

Array
(
    [serno] => 1
    [name] => 24V DC Modular ATX PSU
    [desc_short] => foo
)

Can someone tell me what I have over-seen?

Note:   The "database_mysql.inc" is a derived work
        of my working (!!!) "database_pgsql.inc".

Thanks, Greetings and nice Day/Evening
    Michelle Konzack
    Systemadministrator
    24V Electronic Engineer
    Tamay Dogan Network
    Debian GNU/Linux Consultant


-- 
Linux-User #280138 with the Linux Counter, http://counter.li.org/
##################### Debian GNU/Linux Consultant #####################
<http://www.tamay-dogan.net/>               <http://www.can4linux.org/>
Michelle Konzack   Apt. 917                  ICQ #328449886
+49/177/9351947    50, rue de Soultz         MSN LinuxMichi
+33/6/61925193     67100 Strasbourg/France   IRC #Debian (irc.icq.com)

<<attachment: signature.pgp>>


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux