Hi,

I was wondering why the CREATE TABLE query in my code seems to be called automatically, whilst in the example below from w3schools, there is a line which explicitly calls the CREATE TABLE query.

Why is that the query in the w3schools example isn't ran twice?

My code:

<?php
$connectionname = mysql_connect("localhost","XXX","XXX");
if (!$connectionname)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create table
mysql_select_db("databasename", $connectionname);
$sqlcommandvariable = 
"
    CREATE TABLE Userssss
    (
        userID int(8),
        userName varchar(15),
        firstName varchar(15),
        lastName varchar(15),
        password varchar(32)
    )

";

if (!mysql_query("$sqlcommandvariable")) die(mysql_error());
else {
        echo "success in table creation.";
    }

mysql_close($connectionname);
?>

W3schools example:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";

// Execute query. Why is this called explicitly? When my query runs automatically?
mysql_query($sql,$con);

mysql_close($con);
?>

I'm finding this confusing, so if anyone could explain where the difference is I'd most appreciate it.

Many thanks! :)

Hi,

I was wondering why the CREATE TABLE query in my code seems to be called automatically, whilst in the example below from w3schools, there is a line which explicitly calls the CREATE TABLE query.

Why is that the query in the w3schools example isn't ran twice?

My code:

<?php
$connectionname = mysql_connect("localhost","XXX","XXX");
if (!$connectionname)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create table
mysql_select_db("databasename", $connectionname);
$sqlcommandvariable = 
"
    CREATE TABLE Userssss
    (
        userID int(8),
        userName varchar(15),
        firstName varchar(15),
        lastName varchar(15),
        password varchar(32)
    )

";

if (!mysql_query("$sqlcommandvariable")) die(mysql_error());
else {
        echo "success in table creation.";
    }

mysql_close($connectionname);
?>

W3schools example:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";

// Execute query. Why is this called explicitly? When my query runs automatically?
mysql_query($sql,$con);

mysql_close($con);
?>

I'm finding this confusing, so if anyone could explain where the difference is I'd most appreciate it.

Many thanks! :)

I am a little confused to what you are asking but I think you might mean.

//your code is put into an if statement
<?php if (!mysql_query("$sqlcommandvariable")) die(mysql_error()); ?>

// where the www.w3schools is just calling it
<?php mysql_query($sql,$con); ?>

is that what you meant?

commented: Thanks for your help!! :) +1

Hi leviathan185,

Thanks for your help.

Yes that is what I meant :)

It seems a little different to C++ which at least from my perspective, the state of the TRUE/FALSE evaluation is already established before entering an IF ELSE statement, whilst here it seems, it is called within the IF clause.

It was that what was throwing me off.

Many thanks for you help!! :)

The call to mysql_query returns true if it is successful, false if it is not. You can therefore call it inside the condition of an if-statement. I believe you can do that in C++ too if the function call returns a boolean result. In PHP you don't HAVE to use the returned result of a function call, so w3schools example just ignores it and calls the function without worrying about what is returned. I am sure that their example is just an example, your code is better because it will handle the situation of when the query fails.

Hi leviathan185,

Thanks for your help.

Yes that is what I meant :)

It seems a little different to C++ which at least from my perspective, the state of the TRUE/FALSE evaluation is already established before entering an IF ELSE statement, whilst here it seems, it is called within the IF clause.

It was that what was throwing me off.

Many thanks for you help!! :)

No Worries glad I could help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.