Simple Machines Community Forum

General Community => Scripting Help => Topic started by: Doh004 on January 27, 2004, 02:30:38 PM

Title: Err... help...please
Post by: Doh004 on January 27, 2004, 02:30:38 PM
I took into consideration what people said and I am trying to write a script that reads a directory, then puts the info into a mysql database:

<?php
$farray
= glob('*.*');
natcasesort($farray);
foreach (
$farray as $fname) {
include(
"dbconnect.php");
//Don't worry about this part
$result = mysql_query('INSERT INTO `roms_info` ( `rom_id` , `name` , `size` , `ss_1` , `ss_2` , `downloads` , `country` , `dl_url` , `system` )
//Insert the values
VALUES (

//Insert the rom ID. Remember to keep the numbers from overlapping
\'+1\',

//Insert the roms name
\'$fname\',

//Insert the file size in KB
\'filesize($fname)\',

//Keep this as the default
\'upload.png\',

//Ignore this
\'\',

//This is the download number, leave this at 0
\'0\',

//This is the country logo
//If the rom is european -which most of the roms arent- put europe.png
//If the rom is japanese -which most of the roms arent-put japan.png
\'usa.png\',

//This is the file name that is shown in the ftp. Copy as you see it there
\'$fname\',

//The system for the rom
\'SNES\'
)'
)
or die(mysql_error());
echo
'It worked';

}

?>

And I'm getting this error:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '//Insert the values VALUES ( //Insert the rom ID. Remember to

I know I did somethign wrong and I probably screwed the script. Any help.

Thanks :)

Oh and I don't know howto for each file it sees, it adds +1 as the file ID. I'm know I'm really wrong right here :(
Title: Re: Err... help
Post by: Cadish on January 27, 2004, 02:44:57 PM
You can't comment inside a query :)

So delete all the comments inside your query

[edit]oh, and about that ID+1: you have to set your column ID to 'auto_increment' (you can do this in phpmyadmin)[/edit]
Title: Re: Err... help
Post by: Doh004 on January 27, 2004, 03:23:43 PM
Ooo didn't know that. I will try that ;)
Title: Re: Err... help
Post by: Doh004 on January 27, 2004, 03:31:56 PM
*watches my jaw drop*

errr

www.dohgames.com/roms/snes/showfile.php

:'(
Title: Re: Err... help
Post by: Doh004 on January 27, 2004, 04:02:34 PM
Please helpĀ  :(

It has to do with the variables...I believe so....
Title: Re: Err... help...please
Post by: Grudge on January 27, 2004, 05:17:04 PM
There are many things you could do to improve that code but here's two important ones

Firstly, put the incldue statement OUTSIDE the foreach statement.

Secondly, the problem is in the query $fname is INSIDE a ' ' string - it needs to be changed to look like:
\'' . $fname . '\'
instead of:
\'$fname\'

Personally I'd change the query to be inside " quotes instead then you don't need to escape the ' symbols. In your example would mean:

$result = mysql_query("INSERT INTO roms_info ( rom_id , name , size , ss_1 , ss_2 , downloads , country , dl_url , system )
VALUES ( '+1', '$fname', filesize($fname), 'upload.png', '', '', 'usa.png', '$fname', 'SNES')") or die... (etc)


That should work :)

Note your code is still inefficient. Your best bet is to do the insert in one go (in the foreach loop build up a string of all the inserts and then stick them in at once - but the code above should work fine me thinks
Title: Re: Err... help...please
Post by: Doh004 on January 27, 2004, 05:28:32 PM
Nope parse error on line seven
<?php
include("dbconnect.php");
$farray = glob('*.*');
natcasesort($farray);
foreach (
$farray as $fname) {
$result = mysql_query("INSERT INTO roms_info ( rom_id , name , size , ss_1 , ss_2 , downloads , country , dl_url , system )
VALUES ( '+1', '
$fname', filesize($fname), 'upload.png', '', '', 'usa.png', '$fname', 'SNES')")')
or die(mysql_error());
}
?>

Worked
Title: Re: Err... help...please
Post by: Doh004 on January 27, 2004, 05:31:18 PM
Hold on I got rid of an extra ')

and heres what i get :

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(3 Ninjas Strike Back.zip), 'upload.png', '', '0', 'usa.png', '
Title: Re: Err... help...please
Post by: Shadow on January 27, 2004, 05:58:22 PM
Change
filesize($fname)
to
'filesize($fname)'
Title: Re: Err... help...please
Post by: Doh004 on January 27, 2004, 06:13:22 PM
Well its not increasing the ID +1 at all. Its staying on one.
Title: Re: Err... help...please
Post by: Shadow on January 27, 2004, 06:19:37 PM
That isn't a valid command as far as I was aware.

Set the extra for ID to "auto_increment" and set the type to bigint or something.
Title: Re: Err... help...please
Post by: Grudge on January 27, 2004, 06:39:01 PM
^ What he said. +1 will not work. Set rom_id to auto_incremement using phpmyadmin and then just remove that column from that insert command. (It will auto inc it for you automatically)
Title: Re: Err... help...please
Post by: Doh004 on January 27, 2004, 07:04:07 PM
www.dohgames.com/roms/n64/showfile.php

It works so far but now with teh file size... :-\
Title: Re: Err... help...please
Post by: Grudge on January 27, 2004, 07:07:51 PM
filesize needs an absolute position of the file and $fname looks like it's just the filename, so you need to add the directory where the files are to the query

e.g
filesize('/usr/ftp/snes/' . $fname)
Title: Re: Err... help...please
Post by: Doh004 on January 27, 2004, 07:23:12 PM
Thanks for the help so far but yet another error:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ''. 1080 Snowboarding.zip)', 'upload.png', '', '0', 'usa.png', '1

<?php
include("dbconnect.php");
$farray = glob('*.*');
natcasesort($farray);
foreach (
$farray as $fname) {
$result = mysql_query("INSERT INTO roms_n64 ( name , size , ss_1 , ss_2 , downloads , country , dl_url , system , rate_up , rate_down )
VALUES ( '
$fname', 'filesize('/home/dohgames/public_html/roms/n64'. $fname)', 'upload.png', '', '0', 'usa.png', '$fname', 'N64' , '0' , '0')")
or die(mysql_error());
}
?>
Worked
Title: Re: Err... help...please
Post by: Shadow on January 27, 2004, 07:33:55 PM
Try this...
<?php
include("dbconnect.php");
$farray = glob('*.*');
natcasesort($farray);
foreach (
$farray as $fname) {
$filesize = filesize('/home/dohgames/public_html/roms/n64'. $fname);
$result = mysql_query("INSERT INTO roms_n64 ( name , size , ss_1 , ss_2 , downloads , country , dl_url , system , rate_up , rate_down )
VALUES ( '
$fname', '$filesize', 'upload.png', '', '0', 'usa.png', '$fname', 'N64' , '0' , '0')")
or die(
mysql_error());
}
?>

The single quotes just needed to be escaped, but I don't like putting PHP commands in queries... just my thing though
Title: Re: Err... help...please
Post by: Doh004 on January 27, 2004, 08:25:03 PM
Ah thanks a bunch :D
Title: Re: Err... help...please
Post by: Doh004 on January 28, 2004, 08:28:06 PM
Gots another question...

I entered from information for one and I want to update the system colum for each row... Heres' what i have:
<?php include("dbconnect.php");
$result2=mysql_query("UPDATE roms_snes SET system=Super Nintendo WHERE system=Sega Genesis");
echo
'<a href="javascript:history.back(1)">Thank You. Click here to return.</a>';
?>


But it wont work. Thanks
Title: Re: Err... help...please
Post by: [Unknown] on January 28, 2004, 08:44:51 PM
system=Super Nintendo

How does it know you want it to be "Super Nintendo"?  It can't read your mind... maybe you mean this:

UPDATE roms_snes
SET system='Super', Nintendo=''
WHERE system='Sega', Genesis = '';

You need to use quotation marks.

UPDATE roms_snes
SET system='Super Nintendo'
WHERE system='Sega Genesis';

-[Unknown]
Title: Re: Err... help...please
Post by: Doh004 on January 28, 2004, 08:47:30 PM
That's teh only thing I hate about PHP... stupid quotations and apostrophes...

Thanks again :)
Title: Re: Err... help...please
Post by: Shadow on January 28, 2004, 10:07:05 PM
That's the thing I love about PHP, no misunderstandings :)