Errors while querying a table and using <option></option>...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pplers
    New Member
    • Apr 2007
    • 60

    Errors while querying a table and using <option></option>...

    Here is config.php:
    [PHP]<?php //The vars are all ok.
    $dbhost = 'localhost';
    $dbname = 'forum';
    $dbuser = 'toor';
    $dbpass = '';
    ?>[/PHP]

    Here is a part of functions.php:
    [PHP]<? require "config.php "; //It is in the same directory.
    ...
    function num(){
    $start = mysql_connect($ dbhost, $dbuser);
    mysql_select_db ($dbname);
    $d = mysql_query("SE LECT id FROM atable;");
    $c = mysql_num_rows( $d);
    mysql_close($st art);
    return $c;
    }

    function id($aa){
    $start = mysql_connect($ dbhost, $dbuser);
    mysql_select_db ($dbname);
    $d = mysql_query("SE LECT id FROM atable;");
    $zxc = mysql_fetch_row ($d);
    mysql_close($st art);
    return $zxc[$aa];
    }
    ...
    ?>[/PHP]

    Part of index.php:
    [PHP]<?
    ...
    $cx = num();
    for($zp=0;$zp<= $cx;$zp++){
    $office = id($zp);

    echo "<option>";
    echo $office;
    echo "</option>";
    }
    ...
    ?>[/PHP]

    There are 13 id in atable.

    The problems:
    1- When i use the variables from config.php, both functions return null values

    2- When i do not use tha vars from config.php, the num() function returns the correct value. But id() only returns the first id from atable.

    Can anyone helpme ?????
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, pplers.

    When you attempt to connect to MySQL without entering any credentials, PHP will attempt to use the default login info in your php.ini file. Since this is usually also blank, this means that MySQL is configured on your machine to let just about anybody log in without a password!

    Have a look at this article.

    In terms of the 1 id instead of 13, you're only fetching one row in functions.php.

    Try doing this instead:
    [code=php]
    $zxc = array();
    while($row = mysql_fetch_row ($d))
    {
    $zxc[] = $row;
    }
    [/code]

    Comment

    • vituko
      New Member
      • Dec 2006
      • 48

      #3
      And the 1 question :

      Inside the function body you have to "import" the global variables (included in your file : in the global scope) into the local scope to get access :

      global $variable ;

      It's a strange behavior of php...

      Comment

      • pplers
        New Member
        • Apr 2007
        • 60

        #4
        Thanks the query went great after modifiying something else. But the other problem still remains. When i change the user and/or the dbname for the vars in config.php then, null values are returned... Why is this ????


        p.s: Still happening after doing: global $dbuser; etc, etc.

        p.s2: global $whatever has to be used inside every function right ??? Isnt there a way to use it at the begining of the code ???

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya pplers.

          Try this instead:
          config.php:
          [code=php]
          if(! ($GLOBALS['db'] = mysql_connect( ... )))
          {
          throw new Exception('Unab le to connect to database server.');
          }

          if(! (mysql_select_d b(' ... ', $GLOBALS['db'])))
          {
          throw new Exception('Unab le to select database.');
          }
          [/code]

          In other words, actually connect to the database in your config.php file. It's generally the sort of thing that you only need to do once per page load anyway.


          The way the 'global' keyword works:
          It's actually an alias for $var =& $GLOBALS['var'];

          So this would work:
          [code=php]
          $GLOBALS['db'] = mysql_connect( ... );

          function doSomething()
          {
          global $db; // $db =& $GLOBALS['db'];
          }
          [/code]

          Whereas this only works sometimes (depends on the value of the register_global s directive):
          [code=php]
          $db = mysql_connect( ... );

          function doSomething()
          {
          global $db; // $db =& ...?
          }
          [/code]

          Comment

          Working...