简体   繁体   English

mysqli_connect() 没有选择数据库? 并且不能插入数据

[英]mysqli_connect() isn't selecting database? and can't insert data

I've got this code我有这个代码

<?php 
    $db_host="localhost";
    $db_username="root";
    $db_password="";
    $db_name="mydb";
    $db_connect=mysqli_connect($db_host, $db_username,$db_password, $db_name);
    //check connection
    if (mysqli_connect_error()){ 
        echo "Failed to connect to MySQL:" .mysqli_connect_error();
    } else{
        echo "Connection successful" ; 
    }
?>

When i run the code it shows "Connection successful" but when i input data into a table that is in the database it gives me an error "No database selected"当我运行代码时,它显示“连接成功”但是当我将数据输入到数据库中的表时,它给我一个错误“没有选择数据库”

I tried the code below and it seems to work, other than the fact that the Auto increment value (ID) isn't passed into the database so it gives me an error which means that "the data passed in row 1 doesn't match the datatype. "(Shouldn't it not be required to be entered, and with each insert query gets updated automatically?) I am passing values to the table using and insert statement and not in array form, maybe?我尝试了下面的代码,它似乎有效,除了自动增量值 (ID) 没有传递到数据库中的事实之外,它给了我一个错误,这意味着“第 1 行中传递的数据不匹配数据类型。“(不应该不需要输入,并且每个插入查询都会自动更新吗?)我正在使用和插入语句而不是数组形式将值传递给表,也许?

<?php
    error_reporting(1);
    $connect_error = 'Sorry, we\'re experiencing connection problems.';
    mysql_connect('localhost','root','') or die($connect_error);
    mysql_select_db('mydb') or die($connect_error);
?>

I was asked to provide the code i'm using for inserting data into the table.我被要求提供用于将数据插入表中的代码。

   <?php   
    require_once ('dbconnect.php');

    $title= "my title";

    if (isset($_POST['submit'])) {

    $username =    $_POST['username'];
    $password =    $_POST['password'];
    $class =       $_POST['class'];
    $type =        $_POST['type'];
    $description = $_POST ['description'];
    $date = date(d-m-y);

    $sql= "INSERT into mytable values ('username, password, class, type, description')" ;
    $qry= mysql_query($sql) ;  

    if (!$qry) {
      echo "Something went wrong: " . mysql_error();}

      else echo "Finished successfully";
}


    ?>

Are you mixing mysqli and mysql .你混合mysqlimysql

use below code to fire mysql query使用下面的代码来触发 mysql 查询

mysqli_query($db_connect,$sql);

replace this line替换这一行

$qry= mysql_query($sql) ;

to

$qry= mysqli_query($db_connect,$sql);

Latest Version of php uses mysqli object (mysql improved) insted of methods for accessing mysql database.最新版本的 php 使用 mysqli 对象(mysql 改进)插入了访问 mysql 数据库的方法。

    <?php 

    $db_host="localhost";
    $db_username="root";
    $db_password="";
    $db_name="mydb";

    $db_connect=new mysqli($db_host, $db_username,$db_password, $db_name);


    //check connection
    if ($db_connect -> connect_error)
    { 
    echo "Failed to connect to MySQL:".$db_connect->connect_error;
    }
    else{
    echo "Connection successful" ; }

    ?>

I had two mistakes, one was mixing mysqli in the dbconnect.php while using mysql_query in the other page.我有两个错误,一个是在 dbconnect.php 中混合了 mysqli,而在另一个页面中使用了 mysql_query。 And then I was confused about the second parameter to use with mysqli_query other than the insert statement, used the connection variable and it now works.然后我对与 mysqli_query 一起使用的第二个参数感到困惑,而不是插入语句,使用了连接变量,现在它可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP 无法使用 mysqli_connect 连接到数据库 - PHP can't connect to database with mysqli_connect 为什么mysqli_connect不工作但mysql_connect是? - Why isn't mysqli_connect working but mysql_connect is? 无法连接到数据库“致命错误:调用未定义函数mysqli_connect()” - Can't connect to database “Fatal error: Call to undefind function mysqli_connect()” 无法使用PHP连接到我的MySQL数据库mysqli_connect() - Can't connect to my MySQL database using PHP mysqli_connect() PHP 7.1.x-未定义mysqli_connect(扩展已打开) - PHP 7.1.x - mysqli_connect Isn't Defined (Extension is turned on) 为什么我的mysqli_connect没有连接? 它说没有指定主机名 - Why isn't my mysqli_connect connecting? It says no hostname was specified when one is 无法使用mysqli_connect连接,但mysql_connect有效..? - Can't connect using mysqli_connect, but mysql_connect works..? mysqli_connect 未连接到数据库 - mysqli_connect not connecting to database mysqli_connect():(HY000 / 2002):无法通过套接字连接到本地MySQL服务器 - mysqli_connect(): (HY000/2002): Can't connect to local MySQL server through socket mysqli_connect():(HY000 / 2003):无法连接到“域名”上的MySQL服务器(111) - mysqli_connect(): (HY000/2003): Can't connect to MySQL server on 'domain name' (111)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM