简体   繁体   中英

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

the issue is that at this point the condition is just to show the form, not connect to 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

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. you need to wrap all the query code in the else section between {... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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