你如何为mysqli_connect定义一个变量?

时间:2016-04-20 12:06:29

标签: php mysql

我在W3C上关注使用PHP和MYSQL进行简单数据库连接的示例但是它出现了两个我不理解的错误,因为在W3C上他们是如何做到的并且他们没有错误。

错误1

注意:未定义的变量:第3行的C:\ xampp \ htdocs \ ShoutIt \ database.php中的mysqli_connect

错误2

致命错误:未捕获错误:函数名必须是C:\ xampp \ htdocs \ ShoutIt \ database.php中的字符串:3堆栈跟踪:#0 C:\ xampp \ htdocs \ ShoutIt \ index.php(1) :在第3行的C:\ xampp \ htdocs \ ShoutIt \ database.php中抛出include()#1 {main}

index.php文件

<?php include 'database.php'; ?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Shout It !</title>
        <link rel="stylesheet" href="CSS/style.css" type="text/css"/>
    </head>
    <body>
        <div id="container">
            <header>
                <h1>SHOUT IT! Shoutbox</h1>
            </header>
            <div id="shouts">
                <ul>
                    <li class="shout"><span>10:15PM - </span>Brad : Hey What Are you guys up to.</li>
                    <li class="shout"><span>10:15PM - </span>Brad : Hey What Are you guys up to.</li>
                    <li class="shout"><span>10:15PM - </span>Brad : Hey What Are you guys up to.</li>
                    <li class="shout"><span>10:15PM - </span>Brad : Hey What Are you guys up to.</li>
                    <li class="shout"><span>10:15PM - </span>Brad : Hey What Are you guys up to.</li>
                </ul>
            </div>
            <div id="input">
                <form method="post" action="process.php">
                    <input type="text" name="user" placeholder="Enter Name" />
                    <input type="text" name="message" placeholder="Message" />
                    <br/>
                    <input class="shout-btn" type="submit" name="submit"value="Shout It Out !" />
                </form>
            </div>
        </div>
    </body>
</html>

database.php文件

> <?php //Connect to MySQL $con =
> $mysqli_connect("localhost","root","Passwordaaa","shoutit");
> 
> //Test Connection if(mysqli_connect_errno()){     echo 'Failed to connect
> to MySQL: '.mysqli_connect_error(); }

4 个答案:

答案 0 :(得分:5)

您正在使用的程序样式

<?php
$con = mysqli_connect("localhost","root","Passwordaaa","shoutit");

if (!$con) {
    die('Connect Error: ' . mysqli_connect_errno());
}
?>

如果你想使用面向对象的风格

<?php
    $mysqli = mysqli_connect("localhost","root","Passwordaaa","shoutit");

    if ($mysqli->connect_errno) {
        die('Connect Error: ' . $mysqli->connect_errno);
    }
    ?>

有关详细信息,请阅读PHP手册http://php.net/manual/en/mysqli.connect-errno.php

答案 1 :(得分:2)

也许只是更正您的代码? (删除V形符号,将$mysqli_connect替换为mysqli_connect等)

看看PHP syntax documentation

<?php
//Connect to MySQL
$con = mysqli_connect("localhost","root","Passwordaaa","shoutit");

//Test Connection
if(mysqli_connect_errno()){
    echo 'Failed to connect to MySQL: '.mysqli_connect_error();
}

答案 2 :(得分:0)

替换:

 $mysqli_connect("localhost","root","Passwordaaa","shoutit");

人:

 mysqli_connect("localhost","root","Passwordaaa","shoutit");

答案 3 :(得分:0)

浏览http://www.w3schools.com/php/func_mysqli_connect.asp

    <?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>