News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Err... help...please

Started by Doh004, January 27, 2004, 02:30:38 PM

Previous topic - Next topic

Doh004

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 :(

Cadish

#1
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]

Doh004

Ooo didn't know that. I will try that ;)

Doh004

*watches my jaw drop*

errr

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

:'(

Doh004

Please help  :(

It has to do with the variables...I believe so....

Grudge

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
I'm only a half geek really...

Doh004

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

Doh004

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', '

Shadow

Change
filesize($fname)
to
'filesize($fname)'
Just do it, go Charter! [Unknown] offered me a spot on the dev team! I swear it!

"Sup foos'! I'm Marshie! Capital M and then arshie! I'm going this way!!!"

Doh004

Well its not increasing the ID +1 at all. Its staying on one.

Shadow

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.
Just do it, go Charter! [Unknown] offered me a spot on the dev team! I swear it!

"Sup foos'! I'm Marshie! Capital M and then arshie! I'm going this way!!!"

Grudge

^ 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)
I'm only a half geek really...

Doh004


Grudge

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)
I'm only a half geek really...

Doh004

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

Shadow

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
Just do it, go Charter! [Unknown] offered me a spot on the dev team! I swear it!

"Sup foos'! I'm Marshie! Capital M and then arshie! I'm going this way!!!"

Doh004


Doh004

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

[Unknown]

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]

Doh004

That's teh only thing I hate about PHP... stupid quotations and apostrophes...

Thanks again :)

Advertisement: