SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: trouble creating array of field names

  1. #1
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    trouble creating array of field names

    I am simply trying to get a list of fields from a table. I am getting an: "Undefined variable: current_array" error. Here's my code:

    function get_fields($user)
    {
    $conn = db_connect();
    $sql = "show fields from current";
    $result = $conn->query($sql);
    $totalFields = mysqli_num_fields($result);
    for ( $i = 0; $i < $totalFields; ++$i )
    {
    $current_array[$i] = mysqli_fetch_array($result);
    }
    return $curent_array;
    }

    Any ideas? And is there a simpler way to create an array out of the field names for a table?

  2. #2
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sql'DESCRIBE current';
    $results mysql_query($sql) or die('Query error: ' mysql_error());

    $field_array = array();
    while(
    $row mysql_fetch_array($results)) {
        
    $field_array[] = $row['Field'];


  4. #4
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried both techniques to no avail. Let's see if I executed correctly:

    Catweasel, I tried to use mysqli_fetch_fields with this code:

    $sql = "select * from current";
    $result = $conn->query($sql);
    $current = mysqli_fetch_fields($results) ;
    return $current;

    I get nothing

    Danebrood, I tried your code word for word and I got a "Query error: No database selected" error. I added the databasename to the table name (so describe sickel.current) and I got "Query error: Access denied for user ''@'localhost' to database 'sickel' ". This should not happen since I used the db_connect function that has my password and user name and which never fails.

    Any ideas?

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yry to print the variable which has the username and password, I belive that you are using the global varible which holds username and password inside the function. If I am right you need to declare the variables as global inside the function too.

  6. #6
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wasn't sure what you meant by make the variables global within the function. What I just did was substitute "$conn = mysqli_connect('localhost', 'myusername', 'mypassword', 'mydatabase);" in place of "$conn = db_connect();" I still got the "Query error: Access denied for user ''@'localhost' to database 'mydatabase' " Was that what you were suggesting?

  7. #7
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the example I gave, you'd want something like the following to occur prior:

    PHP Code:
    mysql_connect('localhost''root''') or die('Connect error: ' mysql_error());
    mysql_select_db('sickel') or die('Database selection error: ' mysql_error()); 
    Without the mysql_select_db() function, you would get the error you received.

  8. #8
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you tell me how to include "mysql_select_db('sickel') or die('Database selection error: ' . mysql_error()); " ?

    Would it be a variable like $conn = mysql_connect('localhost', 'root', '') or die('Connect error: ' . mysql_error()); ?

  9. #9
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when you call that function, it just sets the current mysql connection as using that database; no other steps are required.

    If you use my two examples together, using your own connection variables (i.e. your own host, username, password, and database), it should all work out as is.


    Note that my examples use the mysql functions, rather than the mysqli.

  10. #10
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still getting the "Database selection error: Access denied". Here's my code:

    function get_fields($valid_user)
    {
    $conn = mysqli_connect('localhost', 'myusername', 'mypassword', 'mydatabase');
    mysql_select_db('mymovief_RobSickel') or die('Database selection error: ' . mysql_error());
    $sql= "describe mydatabase.current";
    $result = $conn->query($sql);
    $field_array = array();
    while($row = mysql_fetch_array($results))
    {
    $field_array[] = $row['Field'];
    }
    return $field_array;
    var_dump($field_array);
    }

    Any ideas?

  11. #11
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sickel2 View Post
    Any ideas?
    try this way -
    PHP Code:
    $mysqli = new mysqli("localhost""myusername""mypassword""mydatabase");

    if (
    mysqli_connect_errno()) {
        
    printf("Connect failed.\n");
        exit();
    }

    function 
    get_fields($user) {
        global 
    $mysqli;
        
    $query "SELECT * from current";
        if (
    $result $mysqli->query($query)) {
            
    $finfo $result->fetch_fields();
            foreach (
    $finfo as $val) {
                
    printf("Name:     %s\n"$val->name);
                
    printf("Table:    %s\n"$val->table);
                
    printf("max. Len: %d\n"$val->max_length);
                
    printf("Flags:    %d\n"$val->flags);
                
    printf("Type:     %d\n\n"$val->type);
            }    
        
    $result->close();
        }
        return 
    $finfo;


  12. #12
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the code but I'ver got the "Call to a member function query() on a non-object" error.

  13. #13
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your mixing mysqli (note the i) and mysql. And referring to two databases ("mydatabase" and "mymovief_RobSickel"). My example is strictly using mysql (no i on the end). Using only the mysql functions:

    PHP Code:
    mysql_connect(HOSTUSERNAMEPASSWORD) or die('Connect error: ' mysql_error());
    mysql_select_db(DATABASENAME) or die('Database selection error: ' mysql_error()); 

    $sql"describe TABLENAME";
    $result mysql_query($sql);
    $field_array = array();
    while(
    $row mysql_fetch_array($results))
    {
      
    $field_array[] = $row['Field'];
    }
    return 
    $field_array;
    var_dump($field_array); 
    Change HOST, USERNAME, PASSWORD, DATABASENAME, and TABLENAME to the correct strings.

    Using mysqli is probably similiar using the object oriented syntax, but you don't want to mix it with the mysql functions. They work differently:

    MySQL
    mysqli

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •