Jump to content

Reading from a text file


kentopolis

Recommended Posts

I am having trouble with this script.  I am trying to create an install file that will install my database and tables.  I would like the database name to and host name to be read from a text file name host.txt.  The honest truth is, I don't know very much about php at all.  I'm sure there is an easy answer, maybe it is just "this can't be done".  But I have tried every configuration I can think of and I honestly just need some help.  I can get ti to work if I manually enter the host name, as below.  I also have the script working to read the file, I just can't seem to integrate the two...  Here is the script as is working without reading the text file into the host names (I have not provided all of the script, i.e. the tables section, because the other part is working great...

<?php
include 'config.php';
include 'opendb.php';
//begin get host name need to figure out how to install it into script
$fp = @fopen("host.txt", "rb") or die("Couldn't open file");
$data = fread($fp, filesize($fp));

while(!feof($fp))
{
$data .= fgets($fp, 1024);
}

fclose($fp);

$values = explode("\r\n", $data);

echo "" . $values[0] . "puppylist";
//end get hostname
$query  = 'CREATE DATABASE hostname_puppylist';
$result = mysql_query($query);

mysql_select_db('hostname_puppylist'); or die('Cannot select database, install db: puppylist dbu: puppylister');

$query = $query = 'CREATE TABLE dogs2( '.
....

here is the section I am having trouble with...
$query  = 'CREATE DATABASE hostname_puppylist';
$result = mysql_query($query);
mysql_select_db('hostname_puppylist'); or die('Cannot select database, install db: puppylist dbu: puppylister');

I want it to look like this...
$query  = 'CREATE DATABASE echo "" . $values[0] . "_puppylist"';
$result = mysql_query($query);
mysql_select_db(echo "" . $values[0] . "_puppylist"); or die('Cannot select database, install db: puppylist dbu: puppylister');

....
I know I've got it all wrong, is there anyone who can help me?
Link to comment
Share on other sites

as an aside you can replace
[code]$fp = @fopen("host.txt", "rb") or die("Couldn't open file");
$data = fread($fp, filesize($fp));

while(!feof($fp))
{
$data .= fgets($fp, 1024);
}

fclose($fp);

$values = explode("\r\n", $data);[/code]
with
[code]$values = file("host.txt");[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.