PDA

View Full Version : php script not loading table



don
01-28-2008, 10:07 PM
Although I'm not getting any error messages in the browser when I check the contents of my Mysql table it is empty?
Below is the script and the text file which are both in my www folder.

<script language="php">

# MySQL database User ID, Password and DB name
$sql_id = "username";
$sql_pwd = "password";
$sql_db = "databasename";

# Connect to the database
mysql_connect('','$sql_id','$sql_pwd');

# Delete the current content of the table
$result = mysql_db_query('$sql_id',"DELETE FROM pet") or die ("Invalid DELETE query");

# Optimize the current table (recover empty space)
$result = mysql_db_query('$sql_id',"OPTIMIZE TABLE pet") or die ("Invalid OPTIMIZE query");

# Load local comma separated, fields enclosed by quotes text database - File has to be in the same directory of this file
$result = mysql_db_query('$sql_id',"LOAD DATA LOCAL INFILE 'pet.txt' INTO TABLE pet");

# Get how many records are present in the table now
$result = mysql_db_query('$sql_id',"SELECT * from pet") or die ("Invalid SELECT query");
$rows_count = mysql_num_rows($result);

echo "Records: $rows_count"; mysql_free_result($result);

</script>

'Fluffy', 'Harold', 'cat', 'f', '1993-02-04', '\N'
'Claws', 'Gwen', 'cat', 'm', '1994-03-17', '\N'
'Buffy', 'Harold', 'dog', 'f', '1989-05-13', '\N'
'Fang', 'Benny', 'dog', 'm', '1990-08-27', '\N'
'Bowser', 'Diane', 'dog', 'm', '1979-08-31','1995-07-29'
'Chirpy', 'Gwen', 'bird', 'f', '1998-09-11', '\N'
'Whistler', 'Gwen', 'bird', '\N', '1997-12-09', '\N'
'Slim', 'Benny', 'snake', 'm', '1996-04-29', '\N'

Basil
01-28-2008, 10:23 PM
PHP goes into a php file between <?php and ?> tags, not script tags..

don
01-28-2008, 10:54 PM
Ok, thats what I get for just copying it from the web - I fixed that problem - but I still nothing happens when I try mysql> select * from pet;

<html>
<body>
<?php



# MySQL database User ID, Password and DB name
$sql_id = "xxxx";
$sql_pwd = "xxxx";
$sql_db = "xxxx";

# Connect to the database
mysql_connect('','$sql_id','$sql_pwd');

# Delete the current content of the table
$result = mysql_db_query('$sql_id',"DELETE FROM pet") or die ("Invalid DELETE query");

# Optimize the current table (recover empty space)
$result = mysql_db_query('$sql_id',"OPTIMIZE TABLE pet") or die ("Invalid OPTIMIZE query");

# Load local comma separated, fields enclosed by quotes text database - File has to be in the same directory of this file
$result = mysql_db_query('$sql_id',"LOAD DATA LOCAL INFILE 'pet.txt' INTO TABLE pet");

# Get how many records are present in the table now
$result = mysql_db_query('$sql_id',"SELECT * from pet") or die ("Invalid SELECT query");
$rows_count = mysql_num_rows($result);

echo "Records: $rows_count"; mysql_free_result($result);

?>
</body>

felgall
01-29-2008, 12:32 AM
PHP goes into a php file between <?php and ?> tags, not script tags..

PHP can go in script tags if you enable the option to allow it.


I still nothing happens when I try mysql> select * from pet;

How is the pet table defined, perhaps the fields don't match up properly.

Basil
01-29-2008, 01:47 AM
Huh, I'd never even heard of that. "<?php" is far more common, and I don't see the point of typing all that out.

I'd change
mysql_connect('','$sql_id','$sql_pwd'); to
mysql_connect('','$sql_id','$sql_pwd') or die(mysql_error()); to see if it's even connecting to the database.

I'd also add "or die(mysql_error())" after the LOAD DATA statement.