specifying that fields are arrays
I read
http://php.net/manual/en/function.mysql-field-table.php
and now am very confused.

I have 2 tables I'm working with which I'd like to include in a number of functions.

posts
('post.id', 'title', 'body')

users
('user_id', 'username', 'password')

I wish get the fields directly from the tables and specify that their fields are arrays.

your input is appreciated.

    I'm thinking something like this

    function Tbl_fields_Array(){
    
    $connection = db_connect();
    
    $TABLE_NAME = '';
    
    $result = mysql_query("SHOW FIELDS FROM $TABLE_NAME, $TABLE_NAME");
    
    $i = 0;
    
    while ($row = mysql_fetch_array($result)) {
      echo $row['Field'] . ' ' . $row['Type'];
    }
    }
    

      [man]mysql_fetch_assoc[/man] or [man]mysql_fetch_array[/man] should be helpful for doing that. You might try using the [man]print_r[/man] function inside your loop to see what your query is returning.

      As for your problem, it's not really clear what you are trying to do.

        5 days later
        sneakyimp;10967344 wrote:

        [man]mysql_fetch_assoc[/man] or [man]mysql_fetch_array[/man] should be helpful for doing that. You might try using the [man]print_r[/man] function inside your loop to see what your query is returning.

        As for your problem, it's not really clear what you are trying to do.

        Hey sneakyimp,

        Thanks for the reply

        I have 2 tables.
        With PHP I want to build an array of their column names.

        This is working in Mysql and the result is
        column_name
        id
        title
        body
        created
        modified
        created_at
        user_id
        username
        password
        email

        using SQL code

        SELECT column_name
        FROM information_schema.columns
        WHERE table_name = 'posts'
        UNION
        SELECT column_name
        FROM information_schema.columns
        WHERE table_name = 'users'
        

          I thought print_r($array); would help me troubleshoot this but it prints nothing.

          function Tbl_fields_Array()
          {
          $connection = db_connect();
          
          
          $query ="SELECT column_name FROM information_schema.columns WHERE table_name = 'posts' 
          union 
          SELECT column_name FROM information_schema.columns WHERE table_name = 'users'" or die(mysql_error()); 
          
          $result = mysql_query($query);
          
          if($result == false) 
          {
             user_error(mysql_error()); // get some debug info
          }
          $result = result_to_array($result);
          
          while ($count = 0; $row = mysql_fetch_array($result); $count++) 
          {
            echo $row['column_name'].'<br/>' . ' ' . $row['Type'].'<br/>';
          
          } 
          }
              echo "The name is " . $array[0];
          	print_r($array);
          
          Tbl_fields_Array();	
          
          
          

            The reason it prints nothing is because you have not defined $array to contain any values. $try it on $row. I still have no idea what you are trying to do.

              sneakyimp;10967782 wrote:

              The reason it prints nothing is because you have not defined $array to contain any values. $try it on $row. I still have no idea what you are trying to do.

              Hey sneakyimp,

              Thanks for the reply.

              I was off on a tangent yesterday and forgot to include my function to array code 🙂

              What I am attempting to do is basically create a function that can do selects and join with out all the entry data.

              I am expanding upon this code so I can do join/union.

              SO I need the column_name of all the tables I wish to work with ... so at this point ... data =' Input' ... and I NEED data = 'Input' join/union 'Input' ... which can be a simple or complex join with out all the SQL syntax code.

              
              function selectQuery($table, $cond="", $fields="",  $order="", $sort="asc", $limit="") 
                  { 
                      $cond = ($cond=="")?"":"where $cond"; 
                      $fields = ($fields=="")?"*":$fields; 
                      $order = ($order=="")?"":"order by $order $sort"; 
                      $limit = ($limit=="")?"":"limit $limit"; 
                      $query = "select $fields from $table $cond $order $limit"; 
                      $result = mysql_query($query) or die(mysql_error()); 
                      $data = Array(); 
                      if(mysql_num_rows($result) > 0) 
                          while($row=mysql_fetch_array($result)) 
                              $data[] = $row; 
                      return $data; 
                  } 
              

              I am basically having issues retrieving the column name from the database in an array so I can create a join tlb function that I can implement into the above code.

              I have not built many function codes so I thought this forum would be the best place to ask.

                Write a Reply...