使用php没有echo输出

时间:2014-01-04 21:02:06

标签: php database insert

我将以下代码插入我的网络服务器上的mysql数据库中:

<?php 
$connect = mysql_connect("server", "user", "pwd"); 
$database = "dbname"; 
@mysql_select_db($database) or die( "Unable to seleact database"); 
echo 'test1'; 

if(mysql_connect_errno($connect)) 
{ 
echo 'test2'; 
echo "Failed to connect to MySQL: " . mysql_connect_error(); 
} 

else 
{ 
echo 'test3'; 

} 

$name= isset($_POST['name']) ? $_POST['name'] : ''; 
$email= isset($_POST['email']) ? $_POST['email'] : ''; 
echo 'hallo' . $name; 
$query = mysql_query($connect, "insert into jagdenwilli(name, email) values ('$name' ,'$email') "); 
mysql_close($connect); 

?>

问题是,'test1'之后没有输出了。写入数据库仍然无法正常工作。有人看到我的错误吗?

4 个答案:

答案 0 :(得分:2)

奖金提示:
您应该始终首先看到您的服务器错误日志。你一定会从那里得到一些线索
试试这个:

<?php 
    $connect = mysql_connect("server", "user", "pwd"); 
    $database = "dbname"; 
    @mysql_select_db($database) or die( "Unable to seleact database"); 
    echo 'test1'; 

    if(!$connect) 
    { 
    echo 'test2'; 
    echo "Failed to connect to MySQL: " .mysql_error(); 
    } 

    else 
    { 
    echo 'test3'; 

    } 

    $name= isset($_POST['name']) ? $_POST['name'] : ''; 
    $email= isset($_POST['email']) ? $_POST['email'] : ''; 
    echo 'hallo' . $name; 
    $query = mysql_query($connect, "insert into jagdenwilli(name, email) values ('$name' ,'$email') "); 
    mysql_close($connect); 

?>

答案 1 :(得分:2)

    The only problem with the previous code is syntax error.


    Try these one with connection update :


      <?php 

           $connect = mysql_connect("server", "user", "pwd"); 
           $database = "dbname"; 

           if(! $connect )
             {
               die('Could not connect: ' . mysql_error());
             }

           echo 'Connected successfully';            

           mysql_select_db($database);

           echo 'test1'; 

           $name= isset($_POST['name']) ? $_POST['name'] : ''; 
           $email= isset($_POST['email']) ? $_POST['email'] : ''; 

           echo 'hallo' . $name; 

           $query = mysql_query( "INSERT INTO jagdenwilli(name, email) 
                                  VALUES ('$name','$email') "); 


           mysql_close($connect);


       ?>

答案 2 :(得分:1)

在php中没有函数mysql_connect_errno(),mysqli有一个mysqli_connect_errno

答案 3 :(得分:1)

没有函数mysql_connect_errno()。

您可以检查if(!$connect)以确定连接是否成功。