Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
My PHP isn't working! Database Help
Old 09-12-2009, 10:38 PM My PHP isn't working! Database Help
Physicsguy's Avatar
404 - Title not found

Latest Blog Post:
Challenges
Posts: 824
Name: Scott
Location: Ontario
Trades: 0
The Code:
PHP Code:
$fp fopen('serialnumdb.txt','a+');
if (!
$fp) {echo 'ERROR: Unable to open file.'; exit;}

while (!
feof($fp)) {
    
$line fgets($fp,1024); //use 2048 if very long lines
    
list ($SerialDBContents) = split ('\|'$line);
    echo 
"Database number: $SerialDBContents";
}

$reviewNum $SerialDBContents 1;

$reviewSerialKey "$reviewNum\n";

echo 
"<br />reviewSerialKey: $reviewSerialKey";

fwrite ($fp$reviewSerialKey);

fclose($fp); 
The Explanation:

OK, this is the code that's not working. I don't know why, it just doesn't.

Here's what it does, line by line.

1. Opens the database called "serialnumdb.txt" with an open at end of file for reading and writing.
2. Error message if database fails to open.
3.
4. Don't really know what this does...
5. This one too.
6. Something about making a variable with splitters the | and \ characters, and then a variable called $line.
7. Tells you what the database contains.
8.
9.
10. Makes it so that the variable $reviewNum is $SerialDBContents + 1, so that serialnumdb.txt only contains numbers. Originally the file is blank, and the script adds one to it, so that the next line on the database is 1, and the next time someone submits the form, it's 2, and 3, and 4...
11.
12. Now makes it so that a new variable is made, called "reviewSerialKey", and it is equal to $reviewNum (which is the $SerialDBContents + 1 value) plus an \n, which makes a line break in the database.
13.
14. Now, an fwrite writes into the database the $reviewSerialKey variable, which is the $reviewNum with the \n.
15.
16. Here, an fclose just closes the database.

The Problem:

With one visit to this script, here is what is entered into the database:

Code:
1
1
Yes, with the enter space at the end. (You can't tell with these code boxes, but after the last 1, there is a blank line)

Isn't there only supposed to be one 1? Apparently not.

Here's what the database looks like if I run the script again, without clearing the database:
Code:
1
1
1
1
This time with no enter space at the end. (Although it looks like it with these code boxes)

It just doubled it! WTH?

What I want this script to do is:

1. Open the blank file which is serialnumdb.txt
2. Read the last line of it (which now is nothing)
3. Add 1 to that nothing, so now when you submit that to the database, the database should contain the number 1 on a new line
4. Submit that into the database.
5. Close the database

That's what I've done, right? I think so...
__________________
Check out my
Please login or register to view this content. Registration is FREE
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-12-2009, 10:55 PM Re: My PHP isn't working! Database Help
bmcoll3278's Avatar
Super Talker

Posts: 118
Name: Brian Collins
Trades: 0
Why not use sql it be so much better.
__________________
I hope to build a site with something for every one

Please login or register to view this content. Registration is FREE
bmcoll3278 is offline
Reply With Quote
View Public Profile Visit bmcoll3278's homepage!
 
Old 09-13-2009, 09:30 AM Re: My PHP isn't working! Database Help
Physicsguy's Avatar
404 - Title not found

Latest Blog Post:
Challenges
Posts: 824
Name: Scott
Location: Ontario
Trades: 0
Because for some reason my SQL isn't working. I'm trying to use PHPMyAdmin, but I can't figure it out...
__________________
Check out my
Please login or register to view this content. Registration is FREE
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 09-20-2009, 09:30 PM Re: My PHP isn't working! Database Help
dweebsonduty's Avatar
Junior Talker

Posts: 3
Name: Shane Burgess
Trades: 0
Here is an xml solution for you. Just learned this today, thanks for the idea.
PHP Code:
<?php
$xml 
simplexml_load_file("serialnumdb.xml"); //Load The xml into an array
$entries = (sizeof($xml->number)-1); //Look at how many entries there are and subtract 1 since 0 is an entry
//var_dump($xml);// Debug
$lastnumber $xml->number[$entries]; //look at the last entry
$newnumber $lastnumber+1// add 1 to it
echo "<br>$newnumber</br>";
$xml->addChild("number",$newnumber);// add it to the xml arrray
//var_dump ($xml);

$fp fopen('serialnumdb.xml','w');//write the whole file again

fwrite ($fp"<numbers>\n");
foreach (
$xml->number as $xmlnumber)
{
fwrite ($fp"<number>$xmlnumber</number>\n");
}
fwrite ($fp"</numbers>\n");
fclose($fp);
?>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


Last edited by dweebsonduty; 09-20-2009 at 09:32 PM..
dweebsonduty is offline
Reply With Quote
View Public Profile
 
Old 09-21-2009, 04:35 PM Re: My PHP isn't working! Database Help
Physicsguy's Avatar
404 - Title not found

Latest Blog Post:
Challenges
Posts: 824
Name: Scott
Location: Ontario
Trades: 0
WOW!!!!! I have one question; could I change the name from serialnumdb.xml to serialnumdb.txt? Thanks!

And when I release this software I'm making, you'll be credited, along with everyone who has contributed in any way!

Wow, error central...

Warning: simplexml_load_file() [function.simplexml-load-file]: serialnumdb.xml:1: parser error : Document is empty in /home/scott/web/process.php on line 10

Warning: simplexml_load_file() [function.simplexml-load-file]: in /home/scott/web/process.php on line 10

Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/scott/web/process.php on line 10

Warning: simplexml_load_file() [function.simplexml-load-file]: serialnumdb.xml:1: parser error : Start tag expected, '<' not found in /home/scott/web/process.php on line 10

Warning: simplexml_load_file() [function.simplexml-load-file]: in /home/scott/web/process.php on line 10

Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/scott/web/process.php on line 10

1

Fatal error: Call to a member function addChild() on a non-object in /home/scott/web/process.php on line 16

??
__________________
Check out my
Please login or register to view this content. Registration is FREE

Last edited by Physicsguy; 09-21-2009 at 05:34 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 10-09-2009, 08:47 AM Re: My PHP isn't working! Database Help
Banned

Posts: 2
Name: atiya
Trades: 0
Use my sql it is a best option.. why mysql is not working? what is a problem with mysql
khushboo is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to My PHP isn't working! Database Help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28134 seconds with 12 queries