0

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

警告:mysql_connect(): in 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 上运行:]

而不是我的<username here>,<server is here>,<password here>有真实代码:]

我会很感激任何答案:]

4

4 回答 4

1

您可以执行以下操作来查看错误:

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

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

使用 mysqli 或 pdo

于 2013-01-22T00:40:02.543 回答
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。顺便说一句,我使用的是 WHILE 而不是 FOR 循环,这只是个人喜好。

于 2013-01-22T01:11:15.377 回答
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> 
于 2013-01-22T02:32:43.010 回答
0

我建议你开始使用 PDO;

索引.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;
}    
?>
于 2013-01-22T00:44:33.860 回答