MrWeiser MrWeiser - 7 months ago 46
SQL Question

How do you define a variable for mysqli_connect?

I am following an example on W3C for a simple database connection using PHP and MYSQL however it is coming up with two errors which I don't understand due to on W3C that is how they do it and theirs do not error.

Error 1

Notice: Undefined variable: mysqli_connect in C:\xampp\htdocs\ShoutIt\database.php on line 3

Error 2

Fatal error: Uncaught Error: Function name must be a string in C:\xampp\htdocs\ShoutIt\database.php:3 Stack trace: #0 C:\xampp\htdocs\ShoutIt\index.php(1): include() #1 {main} thrown in C:\xampp\htdocs\ShoutIt\database.php on line 3

index.php file

<?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 file

> <?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(); }

Answer

Maybe just correcting your code ? (removing the chevrons, replacing $mysqli_connect by mysqli_connect, etc.)

Take a look at 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();
}