Jump to content

Trouble printing table data with foreach loop


babymac

Recommended Posts

I'm struggling to print table data.  I have a simple address book in mysql and want to loop through each item and print.  I've read everything I can get my hands on and nothing is showing.  Any thoughts are greatly appreciated! Thanks for your time!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML  1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;  charset=utf-8"/>
<title>Creating Directory</title>
</head>
<body>
    
    <?php
    
    //Label variables
    $dbhost = "localhost";
    $dbuser = "xxxxxx";
    $dbpword = "xxxxxx";
    $dbname = "xxxxxx";
    $dbtable = "Address_Book";
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $address = $_POST['address'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $bday = $_POST['bday'];

    //Initiate connection
    $con=mysqli_connect($dbhost, $dbuser, $dbpword, $dbname);

    // Check connection
    if (mysqli_connect_errno($con))
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    
    $result = mysqli_query( "SELECT * FROM $dbtable")  
    or die("SELECT Error: ".mysqli_error());
    
    while($data = mysqli_fetch_array($result, MYSQL_ASSOC)){
    foreach($data as $key => $value){
    echo $key;
    echo $value;
    } 
    }
    mysqli_close($con);
    
    ?>

    
    
   
</body>
</html>
Link to comment
Share on other sites

mysqli_query() and mysqli_error() both require the $con mysqli connection as a parameter and would be producing php error messages.

 

this indicates that you don't have php's error_reporting/display_errors turned on. don't wasted any more of your time trying to program in php until you have turned on these settings as you are just blindly spinning your wheels getting nowhere without them being on.

 

your first step will be to set php's error_reporting to E_ALL and display_errors to ON in the master php.ini on your development system. restart your web server to get any changes made to the master php.ini and make sure that you are getting php error messages being output from your code.

 

your second step would be to read the php.net documentation for the mysqli_query() and mysqli_error() functions so that you know the proper usage of them. it's impossible to program without knowing what parameters the functions you are using require.

Link to comment
Share on other sites

I really appreciate your comment and want to learn.  I am taking a beginning php class and have not learned anything about error reporting.  Perhaps this is a problem with the curriculum, but for now I am trying to get this code to function.  I have spent nearly 2 days reading and working on this simple assignment.  I'm finally getting an output, but it's doubling the info and I also need to put it into a table.  I will attach the new code - if anyone can give me some insight, I'd be so grateful.  I will look up error reporting now! Thanks again!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML  1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;  charset=utf-8"/>
<title>Creating Directory</title>
</head>
<body>
    
    <?php
    
    //Label variables
    $dbhost = "localhost";
    $dbuser = "xxxxxx";
    $dbpword = "xxxxxx";
    $dbname = "xxxxxx";
    $dbtable = "Address_Book";
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $address = $_POST['address'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $bday = $_POST['bday'];

    //Initiate connection
    $con=mysqli_connect($dbhost, $dbuser, $dbpword, $dbname);

    // Check connection
    if (mysqli_connect_errno($con))
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    
    $query = "SELECT * FROM $dbtable";
    $result = mysqli_query($con, $query);
      
    while($row = mysqli_fetch_array($result)) {
    foreach($row as $key => $value){
    echo $key;
    echo $value;
    }
    }
    mysqli_close($con);
    
    ?>

</body>
</html>
Link to comment
Share on other sites

okay, your next step will be to read the php.net documentation for what data a function returns.

 

mysqli_fetch_array() returns both a numerical and an associative array, so looping over the data it returns will duplicate the output. you will generally use mysqli_fetch_assoc() if you want to reference data by the the associative column names.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.