MYSQLI SELECT FROM

时间:2014-04-26 16:12:58

标签: php mysql

如何使用相同的php脚本向用户显示我的数据库? 在这里,在我从表单中获取$ _POST数据之后,我想向用户显示整个表格。我怎样才能实现这一目标?非常感谢GURUS! (:

<?php
$db_hostname = 'localhost';
$db_database = 'userdata';
$db_username = '';
$db_password = '';

$mysqli = new mysqli($db_hostname, $db_username, $db_password, $db_database);

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

function sanitizeString($var)
{
    $var = strip_tags($var);
    $var = stripslashes($var);
    $var = htmlentities($var);

    return mysql_real_escape_string($var);  
}

function queryMysql($query)
{
    $result = mysqli_query($query) or die(mysql_error());
    return $result; 
}


 $var1 = $_POST['VAR1'];
 $var2 = $_POST['VAR2'];
 $var3 = $_POST['VAR3'];



 $query = "INSERT INTO users (username, userpass, email)
 VALUES ('".$var1."','".$var2."' ,'".$var3."')";

 $result = mysqli_query($mysqli, $query); 

mysqli_close($mysqli);

echo $var1 ." ". $var2 ." ". $var3 ;

 $query = "SELECT FROM users";
 $result = mysqli_query($mysqli, $query);

?>

1 个答案:

答案 0 :(得分:1)

$result = mysqli_query($mysqli, $query);之后添加:

print '<table>'; // Up to you to put thead, tbody etc
while($row = $result->fetch_assoc()) {
    print '<tr>';
    print '<td>' . $row['username'] . '</td>'; 
    print '<td>' . $row['userpass'] . '</td>'; 
    print '<td>' . $row['email'] . '</td>'; 
    // And so on

    print '</tr>';
}
print '</table>';

但你应该考虑一下,如果把所有东西放在一个文件中是个好主意。最好将服务器代码与HTML-Part分开。

同样,MVC Approach(模型视图控制器)对于模板化系统也很有趣。

编辑

看看@Luke Madhanga的评论!!