[英]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
.你混合
mysqli
和mysql
。
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.