MySQL数据库显示所有数据问题

时间:2013-09-01 23:44:06

标签: php mysqli

我试图在我的数据库中显示特定表中的所有数据。我在尝试这样做时遇到了这个错误。

    Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in      /home/content/59/11513559/html/bg/showscore.php on line 23

这是我的php文件:

   <?php
    session_start();
    //connect to db
    $con = mysqli_connect("localhost","placeholder","placeholder","placeholder");


     $table=$_SESSION['gamecode'];

     mysqli_select_db( $con,'dbname') or die("Database selection failed: " .      mysql_error());
     $result = mysqli_query($con,"SELECT * FROM $table");


     function tableme($result){
     $header='';
     $rows='';
     while ($row = mysql_fetch_array($result)) 
 if (!$result) { // add this check.
     die('Invalid query: ' . mysql_error()); 
     if($header==''){
        $header.='<th>'; 
        $rows.='<tr>'; 
        foreach($row as $key => $value){ 
            $header.='<td>'.$key.'</td>'; 
            $rows.='<td>'.$value.'</td>'; 
        } 
        $header.='</th>'; 
        $rows.='</tr>'; 
     }else{
        $rows.='<tr>'; 
        foreach($row as $value){ 
            echo "<td>".$value."</td>"; 
        } 
        $rows.='</tr>'; 
      }
     } 
      return '<table>'.$header.$rows.'</table>';
     }
      echo tableme($result);

这是第23行: mysqli_select_db(" . $table . ",$con) or die(mysql_error());

我不确定我还需要添加到这一行。或者它会导致这个错误的错误。另外我知道我的会话工作正常我已经调用它并在其他页面上显示它以添加/删除表中的数据。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

正如警告所说,第一个参数应该是mysqli资源

 mysqli_select_db(  $con,$table) or die(mysql_error());

答案 1 :(得分:0)

正如 php文档所说:

bool mysqli_select_db ( mysqli $link , string $dbname )

所以这一行:

 mysqli_select_db(" . $table . ",$con) or die(mysql_error());

应该是:

 mysqli_select_db($con," . $table . ") or die(mysql_error());

AND:" . $table . "应该是您的数据库的名称(不是表格的名称)和没有 spaces.