-1

I have this warning in my page "mysql_fetch_assoc() expects parameter 1 to be resource null given in table.php on line 199" I just want when I post from my form, that post to get posted/showed into the table below: Username, Password, eMail and eMail password If you can help me to fix it and where is the problem because I can't understand where is, and I'm not so good in php language :/

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Table</title>
            <style type="text/css">

        Table Style - This is what you want
        ------------------------------------------------------------------ */
        table a:link {
            color: #666;
            font-weight: bold;
            text-decoration: none;
        }
        table a:visited {
            color: #999999;
            font-weight: bold;
            text-decoration: none;
        }
        table a:active,
        table a:hover {
            color: #bd5a35;
            text-decoration: underline;
        }
        table {
            font-family: Arial, Helvetica, sans-serif;
            color: #666;
            font-size: 12px;
            text-shadow: 1px 1px 0px #fff;
            background: #eaebec;
            margin: 20px;
            border: #ccc 1px solid;

            -webkit-border-radius: 3px;
            border-radius: 3px;

            -webkit-box-shadow: 0 1px 2px #d1d1d1;
            box-shadow: 0 1px 2px #d1d1d1;
        }
        table > thead th {
            padding: 21px 25px 22px 25px;
            border-top: 1px solid #fafafa;
            border-bottom: 1px solid #e0e0e0;

            background: #ededed; /* Old browsers */
            background: -moz-linear-gradient(top,  #ededed 0%, #ebebeb 100%); /* FF3.6+ */
            background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ededed), color-stop(100%,#ebebeb)); /* Chrome,Safari4+ */
            background: -webkit-linear-gradient(top,  #ededed 0%,#ebebeb 100%); /* Chrome10+,Safari5.1+ */
            background: -o-linear-gradient(top,  #ededed 0%,#ebebeb 100%); /* Opera 11.10+ */
            background: -ms-linear-gradient(top,  #ededed 0%,#ebebeb 100%); /* IE10+ */
            background: linear-gradient(to bottom,  #ededed 0%,#ebebeb 100%); /* W3C */
            filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ededed', endColorstr='#ebebeb',GradientType=0 ); /* IE6-9 */
        }
        table > thead th:first-child{
            text-align: left;
            padding-left: 20px;
        }
        table > thead > tr:first-child > th:first-child {
            -webkit-border-top-left-radius: 3px;
            border-top-left-radius: 3px;
        }
        table > thead > tr:first-child > th:last-child {
            -webkit-border-top-right-radius: 3px;
            border-top-right-radius: 3px;
        }
        table > tbody > tr {
            text-align: center;
            padding-left: 20px;
        }
        table > tbody > tr > td:first-child {
            text-align: left;
            padding-left: 20px;
            border-left: 0;
        }
        table > tbody > tr > td {
            padding:18px;
            border-top: 1px solid #ffffff;
            border-bottom: 1px solid #e0e0e0;
            border-left: 1px solid #e0e0e0;

            background: #fbfbfb; /* Old browsers */
            background: -moz-linear-gradient(top,  #fbfbfb 0%, #fafafa 100%); /* FF3.6+ */
            background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#fbfbfb), color-stop(100%,#fafafa)); /* Chrome,Safari4+ */
            background: -webkit-linear-gradient(top,  #fbfbfb 0%,#fafafa 100%); /* Chrome10+,Safari5.1+ */
            background: -o-linear-gradient(top,  #fbfbfb 0%,#fafafa 100%); /* Opera 11.10+ */
            background: -ms-linear-gradient(top,  #fbfbfb 0%,#fafafa 100%); /* IE10+ */
            background: linear-gradient(to bottom,  #fbfbfb 0%,#fafafa 100%); /* W3C */
            filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fbfbfb', endColorstr='#fafafa',GradientType=0 ); /* IE6-9 */
        }
        table > tbody > tr:nth-child(even) > td{
            background: #f8f8f8; /* Old browsers */
            background: -moz-linear-gradient(top,  #f8f8f8 0%, #f6f6f6 100%); /* FF3.6+ */
            background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f8f8f8), color-stop(100%,#f6f6f6)); /* Chrome,Safari4+ */
            background: -webkit-linear-gradient(top,  #f8f8f8 0%,#f6f6f6 100%); /* Chrome10+,Safari5.1+ */
            background: -o-linear-gradient(top,  #f8f8f8 0%,#f6f6f6 100%); /* Opera 11.10+ */
            background: -ms-linear-gradient(top,  #f8f8f8 0%,#f6f6f6 100%); /* IE10+ */
            background: linear-gradient(to bottom,  #f8f8f8 0%,#f6f6f6 100%); /* W3C */
            filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f8f8f8', endColorstr='#f6f6f6',GradientType=0 ); /* IE6-9 */
        }
        table > tbody > tr:last-child > td{
            border-bottom: 0;
        }
        table > tbody > tr:last-child > td:first-child {
            -webkit-border-bottom-left-radius: 3px;
            border-bottom-left-radius: 3px;
        }
        table > tbody > tr:last-child > td:last-child {
            -webkit-border-bottom-right-radius: 3px;
            border-bottom-right-radius: 3px;
        }
        table > tbody > tr:hover > td {
            background: #f2f2f2; /* Old browsers */
            background: -moz-linear-gradient(top,  #f2f2f2 0%, #f0f0f0 100%); /* FF3.6+ */
            background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f2f2f2), color-stop(100%,#f0f0f0)); /* Chrome,Safari4+ */
            background: -webkit-linear-gradient(top,  #f2f2f2 0%,#f0f0f0 100%); /* Chrome10+,Safari5.1+ */
            background: -o-linear-gradient(top,  #f2f2f2 0%,#f0f0f0 100%); /* Opera 11.10+ */
            background: -ms-linear-gradient(top,  #f2f2f2 0%,#f0f0f0 100%); /* IE10+ */
            background: linear-gradient(to bottom,  #f2f2f2 0%,#f0f0f0 100%); /* W3C */
            filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f2f2f2', endColorstr='#f0f0f0',GradientType=0 ); /* IE6-9 */
        }

        </style>
</head>
<body>
<?php
mysql_connect("", "", "");
mysql_select_db("xeongame05755");

$dbLink = mysql_connect("", "", "");
    mysql_query("SET character_set_client=utf8", $dbLink);
    mysql_query("SET character_set_connection=utf8", $dbLink);

function cleanInput($input) {

  $search = array(
    '@<script[^>]*?>.*?</script>@si',   // Strip out javascript
    '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
    '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
    '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
  );

  $output = preg_replace($search, '', $input);
  return $output;
}

function sanitize($input) {
    if (is_array($input)) {
        foreach($input as $var=>$val) {
            $output[$var] = sanitize($val);
        }
    }
    else {
        if (get_magic_quotes_gpc()) {
            $input = stripslashes($input);
        }
        $input  = cleanInput($input);
        $output = mysql_real_escape_string($input);
    }
    return $output;
}

$Username = sanitize($_POST['Username']);
$Password = sanitize($_POST['Password']);
$eMail = sanitize($_POST['eMail']);
$eMailPw = sanitize($_POST['eMailPw']);
$submit = sanitize($_POST['submit']);

if($submit) {
    if(
        $Username &&
        $Password &&
        $eMail &&
        $eMailPw
    ) {
        $insert=mysql_query("INSERT INTO commenttable (Username,Password,eMail,eMailPw) VALUES ('$Username','$Password','$eMail','$eMailPw') ");
        echo "<meta HTTP-EQUIV='REFRESH' content='0; url=TEST2.php'>";
    }

}
?>
<div align="#">
<?php

$dbLink = mysql_connect("", "", "");
    mysql_query("SET character_set_results=utf8", $dbLink);
    mb_language('uni');
    mb_internal_encoding('UTF-8');
$result = mysql_query("SELECT Username, Password, eMail, eMailPw FROM {$commenttable} ORDER BY userid DESC"); 
    echo "<div align='center'><table width='20%' cellspacing='0'>";
    echo '  <thead>
            <tr>
            <th>Username</th> 
            <th>Password</th>
            <th>eMail</th>
            <th>eMail Password</th>
        </tr>
            </thead>' ;
            // keeps getting the next row until there are no more to get
    while($rows=mysql_fetch_assoc($getquery))
    {
    // Print out the contents of each row into a table
    echo '<tbody>
            <tr>
            <td>'; 
    echo $row['Username'];
    echo '</td>
            <td>'; 
    echo $row['Password'];
    echo '</td>
            <td>'; 
    echo $row['eMail'];
    echo '</td>
            <td>'; 
    echo $row['eMailPw'];
    echo '</td>
            </tr>
            </tbody>';  
    } 
    echo "</table></div> ";

?>
</div>




</body>
</html>
Deyan Nik
  • 17
  • 3
  • In addition to the duplicate, please note the `mysql_` constructor is [**deprecated as of PHP 5.5**](https://wiki.php.net/rfc/mysql_deprecation), and is [**removed in PHP 7**](https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7#extmysql). Please consider switching to either [**MySQLi**](http://php.net/manual/en/book.mysqli.php) or [**PDO**](http://php.net/manual/en/book.pdo.php), ensuring that you also use [**prepared statements**](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) to prevent [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) :) – Obsidian Age Dec 04 '17 at 23:32

1 Answers1

0

Replace $getquery with $result

while($row=mysql_fetch_assoc($result))

Edit: For reference see more here : http://php.net/manual/en/function.mysql-fetch-assoc.php

Please do use the new PHP Data Objects or the very least mysql improved PHP mysqli library instead as the mysql methods are prone to security risks

:

Robert I
  • 1,524
  • 2
  • 9
  • 18