0

I am using the w3schools.com tutorial for PHP mysqli to learn how to use PHP to send information between a database and server. However the tutorial does not go into much depth in what the code is actually doing. I am unsure as to why it seems that I need to run a conditional 'if' statement to actually send the query.

I took the code below from the website and played around with commenting out code to try to figure out myself how it behaved and noticed that I got an error if I just had $conn->query($sql) instead of the if else statements that contained it.

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
    echo "Database created successfully";
} else {
    echo "Error creating database: " . $conn->error;
}

$conn->close();
?>

I received this error:

Parse error: syntax error, unexpected '$conn' (T_VARIABLE)

Clearly I am not grasping something basic about how these database connections work. Is it possible to send queries without a conditional statement? Also can somebody please explain the little details about how this works and why as this if statement seems a little confusing.

Shadow
  • 33,525
  • 10
  • 51
  • 64
user208480
  • 135
  • 3
  • Code you provide the condition you've removed? – MyLibary Jan 12 '19 at 08:36
  • connect_error) { die("Connection failed: " . $conn->connect_error); } // Create database $sql = "CREATE DATABASE myDB"; $conn->query($sql) $conn->close(); ?> This is the code that led to error – user208480 Jan 12 '19 at 08:47
  • The problem is that in your original code you are missing a `;` between `$conn->query($sql)` and `$conn->close();` – Nick Jan 12 '19 at 09:25
  • If that's the part where you're stuck, it's not too late to read up on PDO. – mario Jan 12 '19 at 10:04

2 Answers2

3

I read in this comment, that your code that led to error is the next code.

<?php 
    $servername = "localhost"; $username = "username"; $password = "password"; // Create connection 
    $conn = new mysqli($servername, $username, $password); // Check connection 
    if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Create database 
    $sql = "CREATE DATABASE myDB"; 
    $conn->query($sql) 
    $conn->close(); 
?>

It is effectively a syntax error, because you are missing a ";" after $conn->query($sql).

Please for the next time, write the code that led to error in the question.

Community
  • 1
  • 1
Pipe Soto
  • 320
  • 2
  • 9
2

Your code look like good. This error coused is through php parser, not mysqli.

if($conn->query($sql) === TRUE) ... 

not need using checking to true, because $query return fail, true, or mysqli object

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

Is it possible to send queries without a conditional statement?

Yes, but you should check before execute code statement if you want control over everything

learn from php manual instead of w3school,

http://php.net/manual/en/mysqli.quickstart.connections.php

p.l
  • 39
  • 5