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); ?>
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;
}
function Display_Table($result) {
$output = '';
while ($row = mysqli_fetch_assoc($result)) {
$output .= $row["menu_name"];
$output .= " (";
$output .= $row["id"];
$output .= ")";
}
return $output;
}