警告:mysql_select_db()期望参数1为字符串,

时间:2016-07-22 08:36:53

标签: php mysqli

我收到此错误:

  

警告:mysql_select_db()期望参数1为str​​ing,object   在第11行的C:\ xampp \ htdocs \ apply \ insert.php中给出       未选择数据库未插入

<?php

$con = mysqli_connect('localhost','root','');

if (!$con)
{
    echo 'Not connected to server';
}

if (!mysql_select_db($con,'staff'))
{
    echo "Database not selected";
}

$Name = $_POST['username'];
$Email = $_POST['email'];

$sql = "INSERT INTO person (Name, Email) VALUES ('$Name','$Email')";

if(!mysqli_query($con,$sql))
{
    echo 'Not inserted';
}
else {
    echo 'Inserted';
}

header("refresh:2; url=index.html");

?>

3 个答案:

答案 0 :(得分:1)

您正在将mysql_mysqli_个功能混合使用。

if (!mysql_select_db($con,'staff'))更改为if (!mysqli_select_db($con,'staff'))

答案 1 :(得分:0)

编写如下代码:

<?php


$con = mysqli_connect('localhost','root','');

if (!$con)
{
    echo 'Not connected to server';
}

if (!mysqli_select_db($con,'staff'))
{
    echo "Database not selected";
}

$Name = $_POST['username'];
$Email = $_POST['email'];

$sql = "INSERT INTO person (Name, Email) VALUES ('$Name','$Email')";

if(!mysqli_query($con,$sql))
{
    echo 'Not inserted';
}
else {
    echo 'Inserted';
}

答案 2 :(得分:0)

像这样编写你的代码

<?php
$con = mysqli_connect('localhost','root','','staff');
if (!$con)
{
echo 'connection not established';
}

$Name = $_POST['username'];
$Email = $_POST['email'];

$sql = "INSERT INTO person (Name, Email) VALUES ('$Name','$Email')";

if(!mysqli_query($con,$sql))

{
echo 'Not inserted';
}

else {
echo 'Inserted';
}