creating a table for attendance

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

creating a table for attendance

Post by Da_Elf »

Im not sure if ive got a php problem going on or a mysql problem going on but its not creating the table

Code: Select all

$date="2016-09-12";
$enddate="2016-09-27";
$datef=date_create($date);
$table_name = "Attend_SF_2016-2017_Form1A_Term1";
$attend_table = "CREATE TABLE ".$table_name." (";
$attend_table .= "id INT(9) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY,";
$attend_table .= "studentsid VARCHAR(9),";

while($date != $enddate){
	date_add($datef,date_interval_create_from_date_string("1 day"));
	$date = date_format($datef,"Y-m-d");
	$day = date_format($datef,"D");
	if (($day != "Sat")&&($day != "Sun")){
			$attend_table .= $date." ENUM('-','NA','H','A','P') NOT NULL DEFAULT '-',";
		}
}
$attend_table = rtrim($attend_table,",");
$attend_table .= ")";
​mysql_query( $attend_table, $conn);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: creating a table for attendance

Post by Celauran »

Have you run the query manually? Have you checked PHP's error logs? What steps have you taken to debug this?
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: creating a table for attendance

Post by Da_Elf »

i ran the query manually and found it had some problems with unwanted characters which ive taken care of but apparently its not even getting to that stage.
this is the error i get followed by the updated code

PHP Fatal error: Call to undefined function ​mysql_query() in /home1/elfproth/public_html/UCS/tests/datestuff.php on line 24
Line 24 of course is the mysql_query();

Code: Select all

<?php
include("----connection file------");

$date="2016-09-12";
$enddate="2016-09-27";
$datef=date_create($date);
$table_name = "AttendSF20162017Form1ATerm1";
$attend_table = "CREATE TABLE ".$table_name." (";
$attend_table .= "id INT(9) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY,";
$attend_table .= "studentsid VARCHAR(9),";

while($date != $enddate){
	date_add($datef,date_interval_create_from_date_string("1 day"));
	$date = date_format($datef,"Y-m-d");
	$day = date_format($datef,"D");
	if (($day != "Sat")&&($day != "Sun")){
		$colname = str_replace("-","",$date);
			$attend_table .= "d".$colname." ENUM('-','NA','H','A','P') NOT NULL DEFAULT '-',";
		}
}
$attend_table = rtrim($attend_table,",");
$attend_table .= ");";
echo $attend_table;
​mysql_query($attend_table, $DBConn);
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: creating a table for attendance

Post by Celauran »

Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: creating a table for attendance

Post by Da_Elf »

PHP 5.6 thats the highest one my site upgrades to
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: creating a table for attendance

Post by Celauran »

Hmm. mysql_query was only removed in PHP 7, but has been bad practice for at least a decade, so maybe now is a good opportunity to start updating your code.
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: creating a table for attendance

Post by Da_Elf »

ive updated the code to use mySQLi_Query() and i get the exact same error this time about mySQLi_Query() being undefined
On everything else queries to that database work. The only difference is this is a create query. Could there be a problem? ive already given permission to that user to create

Code: Select all

<?php
$Con = mysqli_connect("localhost","username","password","database");
$date="2016-09-12";
$enddate="2016-09-27";
$datef=date_create($date);
$table_name = "AttendSF20162017Form1ATerm1";
$attend_table = "CREATE TABLE ".$table_name." (";
$attend_table .= "id INT(9) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY,";
$attend_table .= "studentsid VARCHAR(9),";

while($date != $enddate){
	date_add($datef,date_interval_create_from_date_string("1 day"));
	$date = date_format($datef,"Y-m-d");
	$day = date_format($datef,"D");
	if (($day != "Sat")&&($day != "Sun")){
		$colname = str_replace("-","",$date);
			$attend_table .= "d".$colname." ENUM('-','NA','H','A','P') NOT NULL DEFAULT '-',";
		}
}
$attend_table = rtrim($attend_table,",");
$attend_table .= ");";
echo $attend_table;
​mysqli_query($Con ,$attend_table);
mysqli_close($Con);
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: creating a table for attendance

Post by Celauran »

That doesn't make a ton of sense. Either the function exists or it doesn't. Also, if it's 5.6, it exists. I'd say check that the extension is installed, but if the function is working elsewhere in your code, then the extension is clearly there.
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: creating a table for attendance

Post by Da_Elf »

i just switched to use this and it worked. Strange that SELECT, INSERT, UPDATE and DELETE work with normal mysql_query but CREATE does not.

Code: Select all

if ($Conn->query($attend_table) === TRUE) {
    echo "Table Attendance created successfully";
} else {
    echo "Error creating table: " . $Conn->error;
}
Post Reply