1

Ok so I am still kinda new to this I have a field called fields that when a new field is created that store the value in the database. What I want to do is bring the data back and name session arrays the name of the fields so that they auto generate.

Here's my code

session_start(); 
$_SESSION['fields'][$y]=$row['fields']; 
echo $_SESSION['fields'][$y]; 
echo""; 
$f= $_SESSION['fields'][$y]; 
echo $f; 
echo""; 
$_SESSION[$f][$y]=$row[$f]; 
echo $_SESSION[$f][1]; ; $y++;
4
  • 1
    When I echo $f it echos the field names that I want however it wont fetch the row Commented Feb 17, 2014 at 1:16
  • 1
    Where is the "MySQL"? Commented Feb 17, 2014 at 1:19
  • if (mysqli_connect_errno()) { echo 'Failed to connect to MySQL: ' . mysqli_connect_error(); } $result = mysqli_query($con,'SELECT * FROM customers'); while($row = mysqli_fetch_array($result)) { while($row = mysqli_fetch_array($result)) { Commented Feb 17, 2014 at 1:21
  • Are you wanting to use the database in general to store session data? Take a look at session_set_save_handler Commented Feb 17, 2014 at 1:35

2 Answers 2

1

Hi I have the same problem .. I got solve it by :

using wordpress build in functions wpdb dont use session_start since wordpress trigger session_start();

any question contact me : [email protected]

global $wpdb;
$table_name = $wpdb->prefix . "table_name";
$result = $wpdb->get_results ( "SELECT * FROM $table_name" );

foreach ( $result as $print )   {
    echo $print->id;
    echo $print->fields1;
    echo $print->fields2;
}
Sign up to request clarification or add additional context in comments.

Comments

0

If I understood you correctly, you just want to return the row from the database into a session, so you can use it later to construct HTML elements:

session_start();

$mysqli = new mysqli('host', 'user', 'pass', 'database');
if($stmt = $mysqli->prepare('SELECT * from FIELDS where 1')):
    $stmt->execute();
    $fields = $stmt->get_result(); //so we can use fetch array and simplify this process
    while($row = $stmt->fetch_array(MYSQLI_ASSOC)):  //foreach result set, return an associative array
        $_SESSION['fields'][] = $row; //push the array into your fields session
    endwhile;
    $stmt->close(); //close your statement
endif;
$myslqi->close(); //drop the mysqli connection, we're done

print_r($_SESSION['fields']); //lets see what fields looks like now

$ret = '';
foreach($_SESSION['fields'] as $row): //foreach row in sessions
    $ret .= '<div>'; //parent container for all fields in this row
    foreach($row as $idx => $field): //foreach field in the row## Heading ##
        $ret  .= '<span>'. $field . '</span>'; //create a div element to contain the field
    endforeach;
    $ret . = '</div>'; //close the parent container
endforeach;

echo $ret; //returned the constructed HTML for the fields

1 Comment

what I am trying to do is us the data from the field row to select data from other rows ie.... $fieldname=$row['fieldnames']; and then I want to call more data with something like $row=['$fieldname'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.