连接MySQL时,我没有正确得到结果

时间:2014-07-10 10:26:57

标签: php mysqli

对于下面添加的代码,我没有打印任何结果。

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

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query="SELECT * FROM `login`";
echo $query;
$result=@mysqli_query($query) or die(mysql_error());
while($row=mysqli_fetch_array($result))
{
    echo $row["username"];
}

5 个答案:

答案 0 :(得分:1)

尝试下面的代码

 //conection: 
$con = mysqli_connect("localhost","root","","temp") or die("Error " . mysqli_error($con)); 

//consultation: 

$query = "SELECT * FROM login" or die("Error in the consult.." . mysqli_error($con)); 

//execute the query. 

$result = $con->query($query); 

//display information: 

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

答案 1 :(得分:0)

<?php
$con=mysqli_connect("localhost","root","","temp");
// Here localhost is host name, root is username, password is empty and temp is database name.
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Perform queries
$result = mysqli_query($con,"SELECT * FROM login");

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

mysqli_close($con);
?> 

使用它。它可以解决你的问题。

答案 2 :(得分:0)

按原样使用此代码。

$con=mysqli_connect("localhost","root","","temp");
        $result = mysqli_query($con,"SELECT * FROM login");
         while($row = mysqli_fetch_array($result))
        {
              echo $row["username"];
        }

答案 3 :(得分:0)

// use this code  and plz check your db name
$host='localhost';
$user='root';
$pass='';
$db_name='temp';
$con=mysqli_connect($host,$user,$pass,$db_name);
if($con)
{
echo "db connect succecssfully";
}

$slt="select * from login";
$query=mysqli_query($slt,$con);
while($row=mysqli_fetch_array($query))
{
    echo $row["username"];
}

答案 4 :(得分:-1)

//connection 
$con = mysqli_connect("localhost","root","","my_db");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}