简体   繁体   English

MySql Select命令返回空值

[英]MySql Select Command returning null

I have a simple php code which enters value into MySql database and then retrieves it and displays it. 我有一个简单的php代码,它将值输入MySql数据库,然后检索它并显示它。 However when retrieving it always return null and Empty Set is echoed everytime. 但是,检索时始终返回null,并且每次都会回显Empty Set。 Can someone please help. 有人可以帮忙吗?

I am using WAMP Server. 我正在使用WAMP Server。

Database name is trial and name of table is People. 数据库名称为试验,表名称为人员。 It has 2 attributes: name and email id 它具有2个属性:名称和电子邮件ID

Following is my code: 以下是我的代码:

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

if (mysqli_connect_errno())

       echo "Failed to connect to MySQL: " . mysqli_connect_error();

mysqli_query($con,"INSERT INTO People VALUES ('xyz', 'abc@zzz.com')");

echo "Insertion Success";

$result = mysqli_query($con,"SELECT * FROM People");

if($result == null)

    echo "Empty Set";

else

   while($row = mysqli_fetch_array($result)) 
   {
       echo $row['name'] . " " . $row['emailid'];
       echo "<br>";
   }

   mysqli_close($con);
?>

You should select a database after using mysqli_connect but before any queries are done: 您应该在使用mysqli_connect但在完成任何查询之前选择一个数据库:

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

if (mysqli_connect_errno())

       echo "Failed to connect to MySQL: " . mysqli_connect_error();

mysqli_select_db($con, "databasename");

//query processing here..

You should check if record is inserted change your code to 您应该检查是否插入了记录,将代码更改为

if( mysqli_query($con,"INSERT INTO People VALUES ('xyz', 'abc@zzz.com')") === FALSE){
    echo mysqli_error();
}

first always check if you query executed write by executing it inside if(): 首先,总是通过在if()内部执行查询来查询是否查询了执行的write:

if(!mysqli_query("your query"))
   {
     mysqli_error();
   }

second i think your query is not executing because you cant name you table with a capital letter so it should be "people" not "People" 第二,我认为您的查询未执行,因为您不能用大写字母命名表格,因此应该是“人物”而不是“人物”

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM