Php MySQL数据库显示所有数据问题
我试图显示数据库中特定表中的所有数据。我在尝试这样做时犯了这个错误Php MySQL数据库显示所有数据问题,php,mysqli,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 $
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);
正如警告所说,第一个参数应该是mysqli resource
mysqli_select_db( $con,$table) or die(mysql_error());
正如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());
和:
“$table.”
应该是数据库的名称(而不是表的名称),并且不带空格
和定义$table?
更改参数的顺序:将$con放在第一位。另外,不要混合使用mysql
和mysqli
调用-这不会起作用。最后,根本不要使用mysql
:它已经被弃用了。使用mysqli
或PDOI在“$\u会话”行中定义的“$table”。。。感谢您,我确实更改了它。我还确保我将“mysql”更改为“mysqli”。我注意到我将“$table”变量赋值错误,并将其更改为“$table=$\u SESSION['gamecode'];”我现在得到一个错误,说数据库选择失败了…做一个var\u转储($table)分配给$table
后,请在该行中选择code>,以确保它获得所需数据库的名称。@st15jap-您没有在$\u会话
行中设置$table
。您正在将该行中的$\u SESSION
设置为$table
Answer deleted sir!;)哇,你完全正确。它现在连接到正确的数据库。因此,要连接到表,我应该使用$result=mysqli_query($con,“SELECT*FROM.$table”)代码>如下:$result=mysqli_query($con,“SELECT*FROM$table”);好的,我做了这些改变。我更新了上面的代码以反映应该正确的内容。我没有收到任何错误消息,但是我没有像它应该显示的那样显示该表。您是否碰巧看到任何其他会导致此操作不起作用的内容?我感谢你为回答我最初的问题所做的一切