HOW TO FIX: FATAL ERROR: UNCAUGHT ERROR: CALL TO UNDEFINED FUNCTION
Posted by: Chukwura (197.210.226.---)
Date: September 16, 2019 03:11AM

I created a database and about to create a table but got the following errors:

! ) Fatal error: Uncaught Error: Call to undefined function CREATE TABLE MyGuests ( id INT(6 UNSIGNED
AUTO_INCREMENT PRIMARYKEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR (30) NOT NULL, email

VARCHAR (50), reg_date TIMESTAMP )() in C:\wamp\www\php\revision\mysql\create table obj.php on line 27
( ! ) Error: Call to undefined function CREATE TABLE MyGuests ( id INT(6 UNSIGNED AUTO_INCREMENT PRIMARYKEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR (30) NOT NULL, email VARCHAR (50), reg_date TIMESTAMP )() in C:\wamp\www\php\revision\mysql\create table obj.php on line 27
Call Stack
# Time Memory Function Location
1 0.0707 386320 {main}( ) ...\create table obj.php:0 .
Here is my code:
<!DOCTYPE html>
<html>
<body>

<?php
$servername = "Localhost";
$username = "root";
$password = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

//Check connection
if ($conn->connect_error) {
	die("Connection failed: " . $conn->connect_error());
}

//sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6 UNSIGNED AUTO_INCREMENT PRIMARYKEY,
firstname VARCHAR(30) NOT NULL, 
lastname VARCHAR (30) NOT NULL,
email VARCHAR (50),
reg_date TIMESTAMP
)";

if ($conn->query($sql() === TRUE)) {
	echo "Table MyGuests created successfully";
}else{
    echo "Error creating table: " . $conn->error;	
}
$conn->close();	
?>
	
</body>
</html>

I have checked and wondered how the function is undefined.Please help



Edited 1 time(s). Last edit at 09/16/2019 01:45PM by RiggsFolly.

Options: ReplyQuote
Re: HOW TO FIX: FATAL ERROR: UNCAUGHT ERROR: CALL TO UNDEFINED FUNCTION
Posted by: Otomatic (Moderator)
Date: September 16, 2019 01:36PM

Hi,

This forum is not intended to debug PHP scripts or SQL queries from Wampserver users.

But, because here we are very nice, we will already tell you that your SQL query is wrong, the right syntax is:
CREATE TABLE myguests (
  id int(6) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  firstname varchar(30) NOT NULL,
  lastname varchar(30) NOT NULL,
  email varchar(50) DEFAULT NULL,
  reg_date timestamp NOT NULL,
)

> if ($conn->query($sql() === TRUE)) {
RTFM!
Always, always check the official documentation!
Only in this line, there are at least two fundamental errors!

The documentation : https://www.php.net/manual/en/mysqli.query.php says :
- the query parameter must be a string : Is $sql() a string?
- Can a character string be of the boolean type and be equal to TRUE?

It will be necessary to seriously review your programming concepts and study the documentation.

It would also appear that there is no `$dbname` set in your code before you attempt to use it in this line

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

------------------------------------------------------------------------------------------------------------
Wampserver 3.1.9 32 bit - Apache 2.4.41 - PHP 7.3.10/7.2.23/7.1.32/7.0.33/5.6.40 - MySQL 5.7.28 - MariaDB 10.4.8
Wampserver 3.1.9 64 bit - Apache 2.4.41 - PHP 7.3.10/7.2.23/7.1.32/7.0.33/5.6.40 - MySQL 5.7.28/8.0.18 - MariaDB 10.4.8
PhPMyadmin 4.9.0.1 - MysqlDumper 1.24.5
on W10 and W7 Pro 64 bit
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons
« Ce n'est pas parce qu'ils sont nombreux à avoir tort, qu'ils ont forcément raison. Coluche »
« It's not because they are many to be wrong, they are necessarily right. Coluche »



Edited 2 time(s). Last edit at 09/16/2019 01:48PM by RiggsFolly.

Options: ReplyQuote
Re: HOW TO FIX: FATAL ERROR: UNCAUGHT ERROR: CALL TO UNDEFINED FUNCTION
Posted by: Chukwura (197.210.45.---)
Date: September 17, 2019 02:50AM

Thanks a lot.

I discovered the mistake connecting to a non existing database, as I submitted the code, but for use of $sql

instead of a string I did not know of it and will now switch over to the documentation to familiarize with it.

Thanks once again for being nice.

Options: ReplyQuote
Re: HOW TO FIX: FATAL ERROR: UNCAUGHT ERROR: CALL TO UNDEFINED FUNCTION
Posted by: Otomatic (Moderator)
Date: September 17, 2019 11:20AM

Hi,

The SQL tab of PhpMyAdmin, using the test database, is an excellent way to check queries.

------------------------------------------------------------------------------------------------------------
Wampserver 3.1.9 32 bit - Apache 2.4.41 - PHP 7.3.10/7.2.23/7.1.32/7.0.33/5.6.40 - MySQL 5.7.28 - MariaDB 10.4.8
Wampserver 3.1.9 64 bit - Apache 2.4.41 - PHP 7.3.10/7.2.23/7.1.32/7.0.33/5.6.40 - MySQL 5.7.28/8.0.18 - MariaDB 10.4.8
PhPMyadmin 4.9.0.1 - MysqlDumper 1.24.5
on W10 and W7 Pro 64 bit
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons
« Ce n'est pas parce qu'ils sont nombreux à avoir tort, qu'ils ont forcément raison. Coluche »
« It's not because they are many to be wrong, they are necessarily right. Coluche »

Options: ReplyQuote


Sorry, only registered users may post in this forum.