Re: PHP Basic Function Help
1) Yes, like any other parameter.
2) Just like any other array.
3) Use count().
Re: PHP Basic Function Help
Sooo something like this:
PHP Code:
<?php
include 'config.php'; //Sets up the database information
include 'connect.php'; //Connects the the database
function create_table($tablename, $values[]){
while($x <= count($values)){
$VString = $VString . ", " . $values[$x];
++$x;
}
$query = 'CREATE TABLE $tablename($VString)';
$result = mysql_query($query);
}
include 'close.php'; //Closes the connection
?>
Re: PHP Basic Function Help
Remove the [] from the argument list. An array is an argument just like any other. (In fact, you don't even have a way to enforce the passing of an array, safe through testing with is_array inside the function body. That's the thing about weakly typed languages.)
Also, look at the join() function, it simplifies merging arrays into strings. As it is now, you get an SQL syntax error for starting the field list with a ','.
Or not: in fact, you'll get an error now because variable substitution is only done in strings delimited by double quotes - the query you have now will contain the literal strings $tablename and $VString.
Re: PHP Basic Function Help
So how come this won't create a table then:
PHP Code:
function create_table($tablename, $values){
while($x <= count($values)){
if($VString <> ''){
$VString = $VString . ", " . $values[$x];
}else{
$VString = $values[$x];
}
++$x;
}
$query = 'CREATE TABLE ' . $tablename . '(' . $VString . ')';
$result = mysql_query($query);
print 'Created table $tablename';
}
That is contained in "sqlfunctions.php". I call it with:
PHP Code:
<?php
include 'Php/sqlfunctions.php'; //Where the function is
include 'Php/connect.php'; //Connects to the DB
$name = "newlogin"; //The table name
$Values[0] = "id int(6) NOT NULL auto_increment";
$Values[1] = "user varchar(15) NOT NULL";
$Values[2] = "password varchar(15) NOT NULL";
$Values[3] = "PRIMARY KEY (id)";
$Values[4] = "UNIQUE id (id)";
$Values[5] = "KEY id_2 (id)";
create_table($name, $Values[]); //The call to the function
mysql_close($conn);
?>
It doesn't create a new table. Yes I am connected properly, else I would be getting a connection error.
Re: PHP Basic Function Help
Any other errors? Do you have create table permissions?
And have you output the completed SQL statement?
Btw, this;
Code:
while($x <= count($values)){
if($VString <> ''){
$VString = $VString . ", " . $values[$x];
}else{
$VString = $values[$x];
}
++$x;
}
can be simplified to this:
Code:
$VString = join(', ', $values);
Re: PHP Basic Function Help
No other errors, I get the final print stament and it's done.