[英]MySQL query fails silently in php
This is my PHP code written for website. 这是我为网站编写的PHP代码。 When I executed this, the query doesn't execute and doesn't show any error.
当我执行此操作时,查询不会执行,也不会显示任何错误。 I also checked data types of values that are to be inserted.
我还检查了要插入的值的数据类型。 The database username and password and all credentials are correct.
数据库用户名和密码以及所有凭据均正确。 What could be the problem?
可能是什么问题呢?
<?php
$password ='abcdef';
$host="localhost"; // Host name
$username="futureti_dsatya"; // Mysql username
$password="D2e3e4v1i"; // Mysql password
$db_name="futureti_db"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select databse.
$con = mysqli_connect($host, $username, $password,$db_name);
if(!$con)
{
die('Could not connect: '. mysql_error());
}
else
{
$res = mysqli_query($con,"insert into users values(55555623,'saran1','satya_saran',$password)");
if($res){
print("i am ok");
}
else{
print("bad");
}
}
?>
you dont get any error because you are making using mysql. 您没有得到任何错误,因为您正在使用mysql。 not mysqli.
不是mysqli。
your code is wroking just wrap password . 您的代码混乱了,只包装了密码。 i guess the connection is not connecting.
我想连接没有连接。
replace this: 替换为:
die('Could not connect: '. mysql_error());
to 至
die('Could not connect: '. mysqli_error()); //to see the error
Wrap $pass
in quotes (55555623,'saran1','satya_saran','$pass')
as shown below with an explanation about " $password
", and change mysql_error());
如下所示将
$pass
括在引号中(55555623,'saran1','satya_saran','$pass')
,并带有有关“ $password
”的说明,并更改mysql_error());
to mysqli_error());
到
mysqli_error());
those two functions don't mix and that is why you did not get the proper error messages to show up. 这两个函数不能混合使用,这就是为什么您没有得到正确的错误消息来显示的原因。
As already stated, you're using $password
twice; 如前所述,您使用
$password
两次; change one of the variables to something else. 将其中一个变量更改为其他变量。
What you're presently doing is overwriting your $password
variable. 您当前正在做的是覆盖
$password
变量。
I am assuming you want to enter abcdef
into your DB. 我假设您想在数据库中输入
abcdef
。 If so, then do this instead: 如果是这样,请改为执行以下操作:
<?php
$pass ='abcdef';
$host = "localhost"; // Host name
$username = "futureti_dsatya"; // Mysql username
$password = "D2e3e4v1i"; // Mysql password
$db_name = "futureti_db"; // Database name
$tbl_name = "users"; // Table name
// Connect to server and select databse.
$con = mysqli_connect($host, $username, $password,$db_name);
if ( !$con ) {
die('Could not connect: '. mysqli_error());
} else {
$res = mysqli_query($con, "insert into users values (55555623,'saran1','satya_saran','$pass' )");
if( $res ) {
print("i am ok");
} else {
print("bad");
}
}
?>
Also, inserting data into a table without telling it which columns to use is not a recommended method. 另外,也不建议将数据插入表中而不告知要使用的列。
Use something to the effect of: 使用以下效果:
($con, "insert into users (column1, column2, column3, column4) values (55555623,'saran1','satya_saran','$pass' )
Sidenote: If the column for your first value isn't an (int)
you will need to wrap that in quotes as well. 旁注:如果第一个值的列不是
(int)
,则也需要将其用引号引起来。
Also, if your first column is an AUTO_INCREMENT
, you will need to remove the AUTO_INCREMENT
from the column's type. 同样,如果您的第一列是
AUTO_INCREMENT
,则需要从列的类型中删除AUTO_INCREMENT
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.