似乎无法连接到mysql,localhost?

时间:2013-01-22 00:36:12

标签: php mysql sql localhost mysql-connect

我似乎无法在本地主机下运行此代码,我的目标是制作一个应该在网站上显示的表但是当我尝试连接时我得到错误:

  

警告:mysql_connect():在C:\ xampp \ htdocs \ PhpProject2 \ hent.php上   第5行无法连接

my site name: http://localhost/PhpProject2/hent.php

代码:

<html>
<body>
<?php 

mysql_connect('<server is here>','<my username here>','<password here>') 
or die('can not connect' );
mysql_select_db('<my username here>') or die ('can not connect to <username here>');  

$sql = "Select * from Customer";
$result = mysql_query($sql);
$number= mysql_num_rows($result);

for($i=0; $i < $number; $i++)
{
    $table = mysql_fetch_row($result);
    echo  $table[0], $table[1];
    echo '<br>';
}
?>
    </body>
</html> 

我正在使用xampp,MySQL正在端口3306上运行:]

而不是我的&lt;这里的用户名&gt;,&lt;服务器在这里&gt;,&lt;密码在这里&gt;有真实的代码:]

我会感谢任何答案:]

4 个答案:

答案 0 :(得分:1)

您可以通过以下方式查看错误:

mysql_connect('<server is here>','<my username here>','<password here>') 
or die('Error: '.mysql_error() );

尽量避免使用以mysql_*开头的所有函数。他们目前正在被剥夺。

使用mysqli或pdo

答案 1 :(得分:1)

试试这个:

<?php
  $host = "hostname";
  $user = "username";
  $password = "password";
  $database = "database";

  $link = mysqli_connect($host, $user, $password, $database);
  If (!$link){
      echo ("Unable to connect to database!");
  }
  else {
  $query = "SELECT * FROM Customer";    
  $result = mysqli_query($link,$query);

  while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
   echo  $row['<insert column name>']. "<br>";
  }
}
mysqli_close($link);
?>

我在此代码中使用了MYSQL库。您应该检查mysql中的列是否被称为0和1. B.T.W.我正在使用WHILE而不是FOR循环,这只是个人偏好。

答案 2 :(得分:0)

我建议你开始使用PDO;

<强>的index.php

<?php 
require "dbc.php";

$getList = $db->getAllData(25);

foreach ($getList as $key=> $row) {
         echo $row['columnName'] .' key: '. $key;
    }
?>

<强> dbc.php

<?php
class dbc {
public $dbserver = 'server';
public $dbusername = 'user';
public $dbpassword = 'pass';
public $dbname = 'db';

function openDb() {    
    try {
        $db = new PDO('mysql:host=' . $this->dbserver . ';dbname=' . $this->dbname . ';charset=utf8', '' . $this->dbusername . '', '' . $this->dbpassword . '');
    } catch (PDOException $e) {
        die("error, please try again " . $e);
    }        
    return $db;
}

function getAllData($qty) {
    //prepared query to prevent SQL injections
    $query = "Select * from Customer where zip= ?";
    $stmt = $this->openDb()->prepare($query);
    $stmt->bindValue(1, $qty, PDO::PARAM_INT);
    $stmt->execute();
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $rows;
}    
?>

答案 3 :(得分:0)

这就是我通常连接到MySQL数据库的方式。 我有config.php

<?php

function db_connect(){
   $conn = new mysqli('localhost', 'root', 'leave_black_if_no_password_set', 'database_name');
   if (!$conn) {
      return false;
   }
   $conn->autocommit(TRUE);  
    return $conn;

}

?>

//结束配置

现在,在你的另一个文件中只需调用config.php:`include'config.php';

    <html>
    <body>
    <?php 
    include 'php/config.php';
    $dbCon = db_connect();


    $sql = "Select * from Customer";
    $result = mysqli_query($dbCon, $sql);
    $number= mysqli_num_rows($result);

   while($row=mysqli_fetch_array($result)){
    {
        echo  $row['column_name'];
        echo '<br>';
    }
//close conn
 mysqli_close($dbCon);
    ?>
        </body>
    </html>