Page 1 of 1

creating a table for attendance

Posted: Mon Sep 12, 2016 8:44 pm
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);

Re: creating a table for attendance

Posted: Tue Sep 13, 2016 6:36 am
by Celauran
Have you run the query manually? Have you checked PHP's error logs? What steps have you taken to debug this?

Re: creating a table for attendance

Posted: Tue Sep 13, 2016 7:38 am
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);
?>

Re: creating a table for attendance

Posted: Tue Sep 13, 2016 8:21 am
by Celauran

Re: creating a table for attendance

Posted: Tue Sep 13, 2016 8:40 am
by Da_Elf
PHP 5.6 thats the highest one my site upgrades to

Re: creating a table for attendance

Posted: Tue Sep 13, 2016 8:43 am
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.

Re: creating a table for attendance

Posted: Tue Sep 13, 2016 9:17 am
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);
?>

Re: creating a table for attendance

Posted: Tue Sep 13, 2016 9:33 am
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.

Re: creating a table for attendance

Posted: Tue Sep 13, 2016 9:48 am
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;
}