douyan3478 2015-10-08 13:35
浏览 342
已采纳

非常奇怪的情况无法连接到本地MySQL服务器

I'm experiencing a strange situation, and it's the first time in 5 years...

my code:

<?php include("static/inc/settings.php"); ?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>News</title>
    <link rel="stylesheet" href="static/css/style.css" media="screen" />
    <script type="text/javascript" src="static/js/jquery.js"></script>
    <script type="text/javascript" src="static/js/tinymce.min.js"></script>
    <script type="text/javascript" src="static/js/script.js"></script>
</head>
<body>
    <div id="master">
        <?php if(isset($_POST["inserisci"])){
            $tipo=$_POST["tipo"];
            $titolo=$_POST["titolo"];
            $testo=$_POST["testo"];
            $link=$_POST["link"];
            $info=pathinfo($_FILES["immagine"]["name"]);
            $ext=$info["extension"];
            $newname=md5($titolo.time()).".".$ext;
            $target="static/uploads/".$newname;
            move_uploaded_file($_FILES['immagine']['tmp_name'],$target);
            mysql_query("INSERT INTO links(url) VALUES('".$link."');") or die(mysql_error()); ?>
            <div class="inserito scompari">La news &egrave; stata inserita</div>
        <?php } ?>
        <div id="header">
            <?php include("static/inc/menu.php"); ?>
        </div>
        <div id="main">
            <div class="block">
                <form enctype="multipart/form-data" id="inserisci_news" method="post">
                    <p>Tipo<br /><select name="tipo" id="tipo"><option value=""></option><option value="sinistra">sinistra</option><option value="sopra">sopra</option></select></p>
                    <p>Titolo<br /><input type="text" name="titolo" id="titolo" value="ciao" /></p>
                    <p>Testo<br /><textarea name="testo" id="testo">prova</textarea></p>
                    <p>Link<br /><input type="text" name="link" id="link" value="http://www.google.it" /></p>
                    <p>Immagine<br /><input type="file" name="immagine" value="" /></p>
                    <input type="submit" name="inserisci" value="Inserisci" />
                </form>
            </div>
        </div>
    </div>
</body>
</html>

basically, when I compile the form, I get an error in the query: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

but if I remove the if(isset($_POST["inserisci"])){ cycle.... It goes!!!

What the hell?

In settings.php there's the connection:

<?php $dbname="xxx";
$user="xxx";
$pswd="xxx";
$host="xxx";
if(!isset($link)){
$link=mysql_connect($host, $user, $pswd);
if(!$link){
    die('Could not connect: '.mysql_error());
}
mysql_set_charset('utf8', $link);
$db_selected=mysql_select_db($dbname, $link);
if(!$db_selected){
    die('Could not use db: '.mysql_error());
}
}
date_default_timezone_set('Europe/Rome'); ?>
  • 写回答

1条回答 默认 最新

  • dongtingrun4973 2015-10-08 14:02
    关注

    I think the problem is that you're doing everything with global variables and you've stomped on yourself.

    The relevant line in settings.php:

    $link=mysql_connect($host, $user, $pswd);
    

    The relevant line in your code:

    $link=$_POST["link"];
    

    What you have done is overwritten the variable containing the MySQL connection with something else. This is probably resulting in your query not having a valid connection object to use. I don't know why removing the isset() conditional appears to cause the error to disappear, but I would recommend changing the name of either of these variables and seeing what happens.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序