1

I am attempting to create a function that will take a query in mysql and output it with mysql_fetch_assoc.

The issue is when I return the output inside the loop it only outputs the first field. Likewise, if I return the output outside of the loop I only get the last value.

Is there anyway to get around this?

Here is the main page:

<?php
$dbhost = "localhost";
$dbuser = "widget_cms";
$dbpass = "cjclone123";
$dbdata = "widget_corp";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbdata);

if(mysqli_connect_errno()) {
    die("database connection failed: " .
        mysqli_connect_error() .
            " (" . mysqli_connect_errno() . ")"
    );
} else {
    echo "success!";
} 

Insert_Line_Into_subjects();

    $table = "subjects";

    $result = Output_Table($table);

    var_dump($result);

    echo (Display_Table($result));

    mysqli_free_result($result); 


?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
 <head>
  <title>phpMysql</title>
 </head>
 <body>

    <ul>
     <form action="phpMysql.php" method="post">
     Menu Name: <input type="text" name="menuName" value=""><br>
     Position: <input type="text" name="pos" value=""><br>
     Visibility: <input type="text" name="vis" value=""><br>
     <input type="submit" name="submit" value="Create account"><br>
     </form>
    </ul> 

 </body>
</html>

<?php mysqli_close($connection); ?>

Here are all the functions that I used:

function Insert_Line_Into_subjects(){

if (isset($_POST["submit"])) {
    global $connection; 

    $menu_name = ($_POST["menuName"]);
    $position = ($_POST["pos"]);
    $visibility = ($_POST["vis"]);

    $query1 = "INSERT INTO subjects ";
    $query1 .= "(menu_name, position, visible) ";
    $query1 .= "VALUES ('$menu_name', '$position', '$visibility')";
    $result = mysqli_query($connection, $query1);
    Check_SQL_Execution($result);
    return $result;
    }
}

    function Check_SQL_Execution($result) {
        global $connection;

    if (!$result) {
        die("<br>Database query failed: " . mysqli_error($connection));
    }
    }

    function Output_Table ($table) {
    global $connection; 

    $query1 = "SELECT * ";
    $query1 .= "FROM {$table}" ;
    $result1 = mysqli_query($connection, $query1);
    Check_SQL_Execution($result1);
    return $result1; 

}

    function Display_Table($result) {

        //if (isset($_POST["submit"])){
        $output = '';

        while ($row = mysqli_fetch_assoc($result)) {
        $output = $row["menu_name"];
        $output .= " ("; 
        $output .= $row["id"];
        $output .= ")";
    }
    return $output;
 }

What did I do wrong? Thanks!

3
  • 2
    The return line should be outside the loop. Commented Jun 1, 2016 at 13:34
  • what is you expected output? Commented Jun 1, 2016 at 13:36
  • I expected the entire table to output. Each menu name and id. Commented Jun 1, 2016 at 13:45

1 Answer 1

4
function Display_Table($result) {
    $output = '';
    while ($row = mysqli_fetch_assoc($result)) {
        $output .= $row["menu_name"];
        $output .= " ("; 
        $output .= $row["id"];
        $output .= ")";
    }
    return $output;
}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this and it only outputted the last value in the table.
show me the proof. show me full fragment of your code (where and how you use this function), query and sample of data you execute your query against
@zach, the subtle difference here with Alex's snippet is that $output is appended to on each loop, whereas you were clobbering it each time.
That was it! Thank you.

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.