简体   繁体   English

无法连接到本地 MySQL 服务器

[英]Can't connect to local MySQL server

I have a account page configured like the following;我有一个如下配置的帐户页面;

if(!isset($_SESSION["myusername"])){ 

then show some login form然后显示一些登录表单

else connect to database and show the logged user's details.否则连接到数据库并显示登录用户的详细信息。 The page displayes the form but with some mysql errors at the bottom.该页面显示表单,但底部有一些 mysql 错误。

mysql错误

the issue is that at this point the condition is just to show the form, not connect to mysql.问题是此时条件只是显示表格,而不是连接到 mysql。

Now enter username: demo@demo.com / password: demo (the font is white, i haven't fixed that yet.) the website is http://auto-sal.es/account.php现在输入用户名:demo@demo.com / 密码:demo(字体是白色的,我还没修好)网址是http://auto-sal.es/account.php

and you will see that everything works okay.你会发现一切正常。登录

Can anyone please help me find out what the issue is.谁能帮我找出问题所在。

here is the code for the account page.这是帐户页面的代码。

<?php include("header.php"); ?>
<body id="account">
<div id="wholewrap">
<div id="bodywrap">
<div id="content">

<?php
session_start();
if(!isset($_SESSION["myusername"])){
$form = '
<h1>Members section</h1>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>';
echo $form;
}
else
include("conn.php");
$strSQL = "SELECT * FROM customer WHERE cus_id = '$cus_id';";
$rs = mysql_query($strSQL);
mysql_error();
while($row = mysql_fetch_array($rs)) {
$layout='
<h1>Your details</h1>
<table border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td width="308" valign="top"><p>Name:</p></td>
    <td width="308" valign="top"><p>' . $row['name'] . '</p></td>
  </tr>
  <tr>
    <td width="308" valign="top"><p>DOB:</p></td>
    <td width="308" valign="top"><p>' . $row['dob'] . '</p></td>
  </tr>
  <tr>
    <td width="308" valign="top"><p>Address:</p></td>
    <td width="308" valign="top"><p>' . $row['address'] . '</p></td>
  </tr>
  <tr>
    <td width="308" valign="top"><p>Email:</p></td>
    <td width="308" valign="top"><p>' . $row['email'] . '</p></td>
  </tr>
  <tr>
    <td width="308" valign="top"><p>Telephone:</p></td>
    <td width="308" valign="top"><p>' . $row['telephone'] . '</p></td>
  </tr>
  <tr>
    <td width="308" valign="top"><p>Join date:</p></td>
    <td width="308" valign="top"><p>' . $row['join_date'] . '</p></td>
  </tr>
  <tr>
    <td width="308" valign="top"><p>Last access:</p></td>
    <td width="308" valign="top"><p>' . $row['last_access'] . '</p></td>
  </tr>
</table>';
echo $layout;
}
mysql_close();
    ?>
</div>  <!-- content #end -->
<div id="sidebar">
<h1>Sidebar</h1>
</div>  <!-- sidebar #end -->
</div>  <!-- bodywrap #end -->
</div>  <!-- wholewrap #end -->
<?php include("footer.php");?>

connection contents连接内容

<?php
$host="xxx"; // Host name 
$username="xxxx"; // Mysql username 
$password="xxx"; // Mysql password 
$db_name="xxx"; // Database name 
$name=$_SESSION['name'];
$cus_id=$_SESSION['cus_id'];

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
mysql_set_charset('utf8');
?>

Look at these lines of your code:查看您的代码的这些行:

else
   include("conn.php");
$strSQL = "SELECT * FROM customer WHERE cus_id = '$cus_id';";
$rs = mysql_query($strSQL);

Your code will only include the connection file (conn.php) if $_SESSION["myusername"] is set.如果设置了 $_SESSION["myusername"],您的代码将只包含连接文件 (conn.php)。 you need to wrap all the query code in the else section between {... }您需要将所有查询代码包装在{... }之间的 else 部分

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

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