|
-
Jan 2nd, 2006, 12:31 PM
#1
Thread Starter
Frenzied Member
PHP Basic Function Help
Okay, I'm still in the process of learning the basics of PHP. Currently I am interacting with MySql to 'spice' up my site.
At the moment, I am trying to create a function for simplifying the creation of tables via a function.
This would require the table name and it's values. The problem is the values can be 2 or 10, and are usually not the same. So my idea was to handle this with an array, but I need to know a few things first:
-Can you use an array as a paramter for a function
-How do you use that array as a paramter
-How do you get the upper bound of an array
If anyone could help explain the above it would be nice 
PHP Code:
<?php
include 'config.php'; //Sets up the database information
include 'connect.php'; //Connects the the database
function create_table($tablename, $values){
$query = 'CREATE TABLE $tablename, $values';
$result = mysql_query($query);
}
include 'close.php'; //Closes the connection
?>
Note: My idea was to create the values string by creating a loop that ran through all the values of the array and added them to another string like ", $Value1, $Value2" etc.
Age - 15 ::: Level - Advanced
If you find my post useful please ::Rate It::

-
Jan 2nd, 2006, 12:34 PM
#2
Re: PHP Basic Function Help
1) Yes, like any other parameter.
2) Just like any other array.
3) Use count().
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 2nd, 2006, 12:50 PM
#3
Thread Starter
Frenzied Member
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
?>
Last edited by Inuyasha1782; Jan 2nd, 2006 at 12:57 PM.
Age - 15 ::: Level - Advanced
If you find my post useful please ::Rate It::

-
Jan 2nd, 2006, 07:59 PM
#4
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 2nd, 2006, 08:39 PM
#5
Thread Starter
Frenzied Member
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.
Age - 15 ::: Level - Advanced
If you find my post useful please ::Rate It::

-
Jan 2nd, 2006, 09:08 PM
#6
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);
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 2nd, 2006, 09:31 PM
#7
Thread Starter
Frenzied Member
Re: PHP Basic Function Help
No other errors, I get the final print stament and it's done.
Age - 15 ::: Level - Advanced
If you find my post useful please ::Rate It::

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|