RE: [PHP-DB] Create Table from a file in php

From: Date: Sat, 12 Jan 2002 18:39:52 +0000
Subject: RE: [PHP-DB] Create Table from a file in php
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-15703@lists.php.net to get a copy of this message
Dave, From the MySLQ manual : DROP TABLE [IF EXISTS] tbl_name [, tbl_name,...] [RESTRICT | CASCADE], but I don't know if it would work in a mysql_query(). Alternately, use mysql_tables() and mysql_tablename, and parse the results to determine if the table exists. If it does, use ALTER TABLE I guess. I've had the luxury of being able to do this at the console, not through a script. Not exactly nuggets, but hope it's helpful - Miles At 09:15 AM 1/12/2002 +0000, Dave Carrera wrote:
Miles You got me thinking so of i went and produced this working block of code. <?include('./i_inc/dbcon.php'); // db connection file location $sqlfile = "./i_inc/members.php"; // location of sql file $readsql =@fopen($sqlfile, "r") or die ("SQL file dosn't excist"); $sqlsql = fread($readsql,filesize($sqlfile)); mysql_query($sqlsql) or die ("members not created or already excists"); fclose($readsql); ?> This works perfectly but i have one thing i have been trying to solve for the last two days. Some kind of if table excist loop is required. For the life of me i can't seem to get this info any where. 5 Books, 20+ websites and no closer....:-( Can you throw some nugets of wisdom this way. Please. Thanks for the help and i hope the included code sample can help someone else. Thanks Dave C -----Original Message----- From: Miles Thompson [mailto:milesthompson@ns.sympatico.ca] Sent: 11 January 2002 18:49 To: Dave Carrera; php-db@lists.php.net Subject: Re: [PHP-DB] Create Table from a file in php I don't know how you would do it from a file off the top of my head, so let's invent it, in pseudocode, anyway. The important thing to remember is that all of the commands to create or alter tables / databases are SQL queries. So the mysql_query() function is used to do most of the work. Assumptions: We have the database We have a connection to it We have the proper user/password for creation rights. We have a file with the necessary SQL to create a tablle : CREATE TABLE ..... etc., with no trailing semi-colon. pseudo code .. Open the file & get the file handle Read it all as one chunk, assigning it to a variable, say $sql. If the SQL is broken into lines the file will have to be read and concatenated to the variable. Use the variable in a mysql_query, testing for success. Issue a mysql_list_tables to see that you have the new tables, if that's successful a list_fields for the new table. Refinements . things to check ... 1. Maybe check for the existence of the table and drop it before you create it. That can be done as a mysql_query. 2. You may need to add an opening double-quote and a closing double-quote to what you are assigning to $sql. Offhand, I think not. Alternate approach, although you probably don't have the necessary permission on the server would be to exec( mysql database_name < file_containing_table_creation_code); I hope you find this helpful - Miles Thompson At 10:57 AM 1/11/2002 +0000, Dave Carrera wrote: Hi All I want to create a table from a file that contains the sql to create the table. I have checked Mysql.com and have seen how to do it from telnet, but i want to do it from a php script. Can anyone please help. Thanks in Advance Dave C The two rules for success are: 1. Never tell them everything you know. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net For additional commands, e-mail: php-db-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net


« previous php.db (#15703) next »