如果这是一个简单的问题我很抱歉,但我真的很擅长编码。我一直试图通过自己的研究来解决这个问题,但我无法弄清楚出了什么问题。
我在连接到MYSQL时遇到问题,即使我可以使用相同的数据通过我的phpAdmin连接。知道发生了什么事吗?
我不确定您需要哪些信息来帮我解决这个问题所以我包含了用于构建代码的文件。我非常感谢您提供的任何帮助。
我得到的错误抱歉,无法连接到数据库
此代码显示在header.php文件
中$SERVER = 'localhost';
$USER = 'useradmin';
$PASS = 'password';
$DATABASE = 'mydatabase';
if (!($mylink = mysql_connect( $SERVER, $USER, $PASS))){
echo "<h3>Sorry, could not connect to database.</h3><br/>
Please contact your system's admin for more help\n";
exit;
}
mysql_select_db( $DATABASE )
此代码出现在我的index.php文件
中<?php
session_start();
include_once('header.php');
include_once('function.php');
$_SESSION['userid'] = 1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<?php
if (isset($_SESSION['message'])) {
echo "<b>". $_SESSION['message']. "</b>";
unset($_SESSION['message']);
}
?>
<form method='post' action='add.php'>
<p>Your Status:</p>
<textarea name='body' rows='5' cols='40' wrap=VIRTUAL></textarea>
<p><input type='submit' value='submit'/></p>
</form>
<?php
$posts = show_posts($_SESSION['userid']);
if (count($posts)) {
?>
<table border='1' cellspacing='0' cellpadding='5' width='500'>
<?php
foreach ($posts as $key => $list) {
echo "<tr valign='top'>\n";
echo "<td>".$list['userid'];
echo "<td>".$list['body'] ."<br/>\n";
echo "<small>".$list['stamp'] ."</small></td>\n";
echo "</tr>\n";
}
?>
</table>
<?php
}else{
?>
<p><b>You haven't posted anything yet!</b></p>
<?php
}
?>
</body>
</html>
这是我的function.php文件
function add_post($userid, $body) {
$sql = "insert into posts (user_id, body, stamp)
values ($userid, '". mysql_real_escape_string($body)."', now())";
$result = mysql_query($sql);
function show_posts($userid) {
$posts = array();
$sql = "select body, stamp from posts
where user_id ='$userid' order by stamp desc";
$result = mysql_query($sql);
while($data = mysql_fetch_object($result)) {
$posts[] = array( 'stamp' => $data->stamp,
'userid => $userid
'body' => $data->body
);
}
return $posts;
}
答案 0 :(得分:1)
您的第一个函数似乎没有关闭大括号}
,加上缺少的引号和逗号。
'userid => $userid
应为'userid' => $userid,
mysql_select_db( $DATABASE )
中缺少关闭的分号也会更改为mysql_select_db($DATABASE);
(function.php)
function add_post($userid, $body) {
$sql = "insert into posts (user_id, body, stamp)
values ($userid, '". mysql_real_escape_string($body)."', now())";
$result = mysql_query($sql);
} // <- missing closing brace
function show_posts($userid) {
$posts = array();
$sql = "select body, stamp from posts
where user_id ='$userid' order by stamp desc";
$result = mysql_query($sql);
while($data = mysql_fetch_object($result)) {
$posts[] = array('stamp' => $data->stamp,
'userid' => $userid,
'body' => $data->body
);
}
return $posts;
}
(header.php文件)
$SERVER = 'localhost';
$USER = 'useradmin';
$PASS = 'password';
$DATABASE = 'mydatabase';
if (!($mylink = mysql_connect($SERVER, $USER, $PASS))){
echo "<h3>Sorry, could not connect to database.</h3><br/>
Please contact your system's admin for more help\n";
exit;
}
mysql_select_db($DATABASE);
答案 1 :(得分:0)
尝试将MySQL转换为MySQLi。
<强>的header.php 强>:
<?php
$con=mysqli_connect("localhost","useradmin","password","mydatabase");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
?>
<强> function.php 强>:
function add_post($userid, $body) {
mysqli_query($con,"INSERT INTO posts (user_id, body, stamp)
VALUES ('$userid', '". mysqli_real_escape_string($body)."', now())");
function show_posts($userid) {
$posts = array();
$result = mysqli_query($con,"SELECT body, stamp FROM posts WHERE user_id='$userid' ORDER BY stamp DESC");
while($data = mysqli_fetch_object($result)) {
$posts[] = array( 'stamp' => $data->stamp,
'userid' => $userid
'body' => $data->body);
}
return $posts;
}
答案 2 :(得分:-1)
使用msqli_connect
代替mysql_connect
。