无法通过 PHP 连接到 MYSQL [英] Can't connect to MYSQL through PHP

查看:52
本文介绍了无法通过 PHP 连接到 MYSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个简单的问题,我深表歉意,但我对编码真的很陌生.我一直试图通过我自己的研究来解决这个问题,但我无法弄清楚出了什么问题.

I apologize if this is a simple question but I'm really really new at coding. I've been trying to figure this out through my own research but I can't figure out what's wrong.

我在连接 MYSQL 时遇到问题,即使我可以使用相同的数据通过 phpAdmin 进行连接.知道发生了什么吗?

I'm having a problem connecting to MYSQL even though I can connect through my phpAdmin using the same data. Any idea what's going on?

我不确定您需要哪些信息来帮助我解决此问题,因此我包含了用于构建代码的文件.我非常感谢您能提供的任何帮助.

I'm not sure what info you'll need to help me solve this so I included the files that I used to build my code. I really appreciate any help you can provide.

我收到的错误是抱歉,无法连接到数据库

这段代码出现在 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;

}

推荐答案

尝试将您的 MySQL 转换为 MySQLi.

Try converting your MySQL to 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;

}

这篇关于无法通过 PHP 连接到 MYSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆