SQL Question

Jeff_London

Participant
Joined
Jan 22, 2010
Messages
73
I have a fast question about SQL and PHP.

I wanted to include some code in a Php file that when it is accessed it creates certain tables into the database specified.
 

Jeff_London

Participant
Joined
Jan 22, 2010
Messages
73
I wanted to include some code in a Php file that when it is accessed it creates certain tables into the database specified.

I wanted to know what would the code be to do that -- add tables to a specified database through a Php file; pretty much how an installation file does.

Sorry for not making that clear.
 

MattF

Developer
Joined
May 31, 2007
Messages
714
That part was clear enough, but you've not given any useful ancillary info. Are you going to be including the file via another script? If so, is there a working database layer in use, or are you going to be running the script standalone and creating a DB connection specifically for the script? What type of database, (MySQL, PostgreSQL etc), are you connecting to? If there is an active DB layer, what are the $db vars. As you can see, there's more to it than just writing a quick CREATE TABLE string.

The info for each available type of DB interface and its functions in PHP is here:

http://uk3.php.net/manual/en/refs.database.php
 

krisDOTco

Adherent
Joined
Mar 29, 2006
Messages
286
Not sure what to make of this. Some context would be great - tell us exactly what you're planning to do. Do you want a hit on the page to add a table to a connected database?

Also, look into 'stored procedures'. Raw SQL code in a page is not an excellent idea.
 

MattF

Developer
Joined
May 31, 2007
Messages
714
Also, look into 'stored procedures'. Raw SQL code in a page is not an excellent idea.

Overkill if it's a one shot script though. :D Definitely more info needed as you mention though, regarding the O.P's needs and specifics of use.
 

krisDOTco

Adherent
Joined
Mar 29, 2006
Messages
286
Overkill if it's a one shot script though. :D Definitely more info needed as you mention though, regarding the O.P's needs and specifics of use.
That's a fair point - if all it is is an install script, these things are generally throwaway and most of them ask for deletion before you can proceed with using the site you just installed anyway.
 

Jeff_London

Participant
Joined
Jan 22, 2010
Messages
73
Oh, terribly sorry for not explaining that part-- feel like an idiot now. Anyway, I'll just do a brief explanation of how the file already looks and what not but will explain everything as necessary when I can get on a real computer. Please forgive me as I'm typing this in a hurry and the code isn't going to be exactly correct:

<?
$database='db_name';
$username='username';
$password='password';

-put code here that connects to the MySQL database
-put code here that adds a new table called 'test' to $database

?>


Again, sorry about the rudeness and incomplete coding here. I will come back and reply with a more detailed post later when I get on a realmcomputer. Again, I am really sorry about this.
 

MattF

Developer
Joined
May 31, 2007
Messages
714
Again, sorry about the rudeness and incomplete coding here. I will come back and reply with a more detailed post later when I get on a realmcomputer. Again, I am really sorry about this.

Don't worry about it mate. It was just an oversight. We all tend to forget to include some necessary bits of info on occasion. :D I assume you'll be wanting some columns in the table too? Is it a one shot, (installation type), script, btw, or a script which will be accessed regularly by the software for creating tables, say from a user defined input?
 

Jeff_London

Participant
Joined
Jan 22, 2010
Messages
73
Is it a one shot, (installation type), script, btw, or a script which will be accessed regularly by the software for creating tables, say from a user defined input?

It will only be accessed once and, yes, it will contain columns. I believe it will contain 6.
 

Jeff_London

Participant
Joined
Jan 22, 2010
Messages
73
Ah, yes, that's fits what I needed perfectly. Thanks a lot mate, and thank you too, Matt!
 

Liam

Developer
Joined
Oct 8, 2010
Messages
507
I wanted to know what would the code be to do that -- add tables to a specified database through a Php file; pretty much how an installation file does.

Sorry for not making that clear.

This might help:

<?php

mysql_connect("localhost","sql_user","sql_pass");
mysql_select_db("db_name");
$query = "CREATE TABLE mytable;";
mysql_query($query);
mysql_close();

?>
 

stubbs

Neophyte
Joined
Jan 1, 2011
Messages
1
Per the responses above, you can always submit the queries via PHP or you can have a prepared .sql file ready to be called after your installer is ran.

I would assume you want to create a database, create a table that will be sources for content, correct?
 

idcatalin

Neophyte
Joined
Mar 3, 2011
Messages
9
i am agree with krisdotco that will be better with stored procedures . the only problem is that you can work with stored procedures only on mysql 5 or uper. so check your mysql version first
 
Top