dqq3623 2013-01-22 00:36
浏览 64
已采纳

似乎无法连接到mysql,localhost?

i can't seem to run this code under local host, my goal is to make a table that is supposed to show in a website but when i try to connect i get the error:

Warning: mysql_connect(): in C:\xampp\htdocs\PhpProject2\hent.php on line 5 can not connect

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

the code:

<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> 

i'm using xampp and MySQL is running on port 3306:]

instead of my < username here >, < server is here >, < password here > there is real code :]

i would Appreciate any answer :]

  • 写回答

4条回答 默认 最新

  • dougu2006 2013-01-22 01:11
    关注

    Try this:

    <?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);
    ?>
    

    I have used the MYSQL library in this code. You should check if you column in mysql is called 0 and 1. B.T.W. I am using WHILE instead of FOR loop that is just a personal preference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程