Results 1 to 10 of 10

Thread: MySQL and PHP Madness! I could really use some help.

  1. #1
    XxDarthDexterxX is offline x10Hosting Member
    Join Date
    Aug 2012
    Posts
    1

    Angry MySQL and PHP Madness! I could really use some help.

    First off, I'm brand new to this place, and I don't know which category this should go in. Seeing as I have free hosting, and I need support, I figured this would be the best place.

    Me and a close friend have decided to start developing our own strategy game. We have a good few members on our forum, all helping out, and coding was going well, until today. I've recently finished a moderately long script, that randomly generates islands, and names for the islands. When I run it on my PC with WAMP, I get 100 islands inserted into the database, every time I run it.

    The idea was to set up a CRON job to generate 100 islands, every two hours, but after running the script a few times to test it worked on the new server, I realized something was wrong. Instead of getting 100 islands each time I ran the script, I only ever got 1 island, and it was only the first time I ran the script. Nothing about it has changed, only the log in details, and database name. All the tables, remained with the same name, as did their headers. I'd really like help with this, as it's driving me insane. I've already torn out hair, and I'll lose more if I can't fix it.

    I've looked through all the e-mails, and things you're supposed to read when you set up the account, again, looking for something to do with queries per second or minute, but I can't anything anywhere. If it's there and I'm not seeing it, could someone point it out please?

    Website: http://www.spacepirates.x10.mx

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    8,349

    Re: MySQL and PHP Madness! I could really use some help.

    1. Wrong forum

    http://x10hosting.com/forums/scripts...s-programming/

    2. We are not mind readers. Posting code is vital to try to get help. Make sure it it is between

    code:

    Code:
    some
    code here
    or php

    PHP Code:
    if( youdoitright()){
        
    success True;

    tags (click on 'go advanced' to get the buttons).
    Nothing is always absolutely so.

  3. #3
    Sharky's Avatar
    Sharky is offline The Biker
    Join Date
    Oct 2007
    Location
    The Agency
    Posts
    3,859
    Moved!

  4. #4
    spacepir is offline x10Hosting Member
    Join Date
    Aug 2012
    Location
    Scotland, United Kingdom
    Posts
    41

    Re: MySQL and PHP Madness! I could really use some help.

    Ok. It's me again, but I'm really confused as to how the forum works. Turns out we got this account when we signed up and I failed to notice. The activation e-mail didn't send so I looked through all my e-mail addresses and discovered this one existed.

    Anyway, the code:

    PHP Code:

     $name 
    generatename(); //Generate random island name.

    $islandsize $size_id 10// Turn the size ID into the islands actual size.   ### NEEDS CLEANED UP ALONG WITH SIZE GENERATION ###

    mysql_select_db("spacepirates");// Select the required database.

    $result mysql_query("SELECT * FROM islands WHERE island_name = '$name'"); //Check if name already exists.
    $no mysql_num_rows($result); // Check if name exists.

    IF($no==1// If it does exist, notify admin.
    {
        Echo(
    "The Island Name '$name' already exists. Island will be regenerated"); //Notify admin island exists already exists.
        
    $loopcount--; // Make sure the loop will run again. This ensures we're always generating 100 islands per run of the script.
    }
    ELSE 
    //If it doesn't already exist:
    {
        
    mysql_query("INSERT INTO islands (island_name, island_size, island_type, island_oreM, island_bootyM, island_techM) VALUES ('$name', '$islandsize', '$type', '$oreM', '$bootyM', '$techM' )");
        
    // ^^ Insert island details into the database! :D

    I don't know why it's not working right, and I've almost given up entirely. Any help at all would be great, even if it's criticizing. I believe all criticism is constructive.

  5. #5
    Skizzerz's Avatar
    Skizzerz is offline Contributors
    Join Date
    Nov 2007
    Location
    Texas
    Posts
    2,480

    Re: MySQL and PHP Madness! I could really use some help.

    You should catch the return result of mysql_query and check that if it has errors. Then you can use mysql_error() to get the error string if it indeed had an error so you can see what is wrong with it. Otherwise, I'm expecting misson or essellar will come around soonish and give you the standard lecture about not using mysql_* functions, why PDO is awesome, and possibly some other stuff.
    Ryan Schmidt | Contributor
    █ 888-X10-9668 - ryan[@]x10hosting.com
    x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

  6. #6
    spacepir is offline x10Hosting Member
    Join Date
    Aug 2012
    Location
    Scotland, United Kingdom
    Posts
    41

    Re: MySQL and PHP Madness! I could really use some help.

    Quote Originally Posted by Skizzerz View Post
    You should catch the return result of mysql_query and check that if it has errors. Then you can use mysql_error() to get the error string if it indeed had an error so you can see what is wrong with it. Otherwise, I'm expecting misson or essellar will come around soonish and give you the standard lecture about not using mysql_* functions, why PDO is awesome, and possibly some other stuff.
    Lol. As I said, me and my friend are beginners, and don't know it all yet. If there's anything more awesome than what we're doing, I'd be happy to use it.

    I have used .mysl_error(); and it's given errors, but those were promptly fixed.

  7. #7
    essellar's Avatar
    essellar is online now Community Advocate
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,704

    Re: MySQL and PHP Madness! I could really use some help.

    Yes, one or the other of us is bound to drop by and mention that. PDO is awesome, but that's not the point, really — mysql_* will be gone soon, and if you're writing new code, there's really no sense in writing code you know will need to be rewritten. PDO is a big shift, though, so you might find it easier to adapt what you know to the mysqli_* methods. (That's the improved MySQL extension. It's not cross-DB like PDO, so you're still locked into MySQL, but it doesn't have the holes in it that ext_mysql has.)

    You are, however, using SELECT * FROM ... when you only need to know if a row exists at all. There's no reason to bring back the whole row, especially if you're going to trash it anyway. SELECT island_name FROM islands WHERE island_name = $name will give you the same result (as far as your code is concerned, since you're just checking to see if there's a result at all) with less work and less memory used.

    That, however, doesn't really answer the main question, which is "why is only one island being created?" Well, that's all I would expect from the code posted here. There is nothing in the code that would cause more than one island to be created—no loops or anything of that nature have been posted. It could be simply a matter of grabbing too little code when you copied and pasted. Or it could be that, in modifying the code along the way, you somehow nuked a loop that used to be working just fine and dandy, thank-you. (Unless you are working from a repository, like git, Mercurial, or SubVersion, it may be difficult to tell when the change was made and by whom, since in a flat-file space the changes simply overwrite the existing files.)
    Last edited by essellar; 08-19-2012 at 08:32 PM. Reason: spelling
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  8. #8
    spacepir is offline x10Hosting Member
    Join Date
    Aug 2012
    Location
    Scotland, United Kingdom
    Posts
    41

    Re: MySQL and PHP Madness! I could really use some help.

    Quote Originally Posted by essellar View Post
    Yes, one or the other of us is bound to drop by and mention that. PDO is awesome, but that's not the point, really — mysql_* will be gone soon, and if you're writing new code, there's really no sense in writing code you know will need to be rewritten. PDO is a big shift, though, so you might find it easier to adapt what you know to the mysqli_* methods. (That's the improved MySQL extension. It's not cross-DB like PDO, so you're still locked into MySQL, but it doesn't have the holes in it that ext_mysql has.)

    You are, however, using SELECT * FROM ... when you only need to know if a row exists at all. There's no reason to bring back the whole row, especially if you're going to trash it anyway. SELECT island_name FROM islands WHERE island_name = $name will give you the same result (as far as your code is concerned, since you're just checking to see if there's a result at all) with less work and less memory used.

    That, however, doesn't really answer the main question, which is "why is only one island being created?" Well, that's all I would expect from the code posted here. There is nothing in the code that would cause more than one island to be created—no loops or anything of that nature have been posted. It could be simply a matter of grabbing too little code when you copied and pasted. Or it could be that, in modifying the code along the way, you somehow nuked a loop that used to be working just fine and dandy, thank-you. (Unless you are working from a repository, like git, Mercurial, or SubVersion, it may be difficult to tell when the change was made and by whom, since in a flat-file space the changes simply overwrite the existing files.)
    Ah, thanks for your reply. There is a loop, and I can assure you that it is working as it should. I'd post the entire file, but when it's 800 lines long....

    I'll double check it though, to be 100% sure.

  9. #9
    misson is offline x10 Spammer
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,573

    Re: MySQL and PHP Madness! I could really use some help.

    Quote Originally Posted by spacepir View Post
    Ah, thanks for your reply. There is a loop, and I can assure you that it is working as it should. I'd post the entire file, but when it's 800 lines long....
    In cases like this, you can create a minimal test case.

    To add to essellar's comments on PDO, one of the nicer features is support for prepared statements (which mysqli also supports, starting with PHP 5.4), which lets you keep data separate from statements instead of interpolating it. Both also support the Traversable interface for results, which means you can use a foreach loop to access result items.

    PHP Code:
    foreach ($results as $result) {
        echo 
    '<tr><td>'implode('</td><td>'$result), '</td></tr>';

    Note the above code works when $results is the result of an SQL query, an array, or any other sort of traversable. Abstraction, it's a big win.

    Combine that with a fetch mode of PDO::FETCH_CLASS and you get a completely OOP dev experience, with the consumer of the results completely unaware that the results are actually a PDOStatement (as far as the consumer is concerned, the results are a collection of objects). Then, if you later want to change the database access layer (DAL) or feed a different collection type to the consumer, you don't need to change a single statement in the consumer. Abstraction, it's a super-big win.

    Follow the advice in my sig and search the forums for more.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Jon Skeet's and Eric Raymond's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  10. #10
    misson is offline x10 Spammer
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,573

    Re: MySQL and PHP Madness! I could really use some help.

    Quote Originally Posted by spacepir View Post
    Ah, thanks for your reply. There is a loop, and I can assure you that it is working as it should. I'd post the entire file, but when it's 800 lines long....
    In cases like this, it's best to create a minimal test case.

    To add to essellar's comments on PDO, one of the nicer features is support for prepared statements (which mysqli also supports), which lets you keep data separate from statements instead of interpolating it. Both also support the Traversable interface for results (though mysqli only added support for it in PHP 5.4), which means you can use a foreach loop to access result items.

    PHP Code:
    foreach ($results as $result) {
        echo 
    '<tr><td>'implode('</td><td>'$result), '</td></tr>';

    Note the above code works whether $results is the result of an SQL query, an array, or any other sort of traversable. Abstraction, it's a big win.

    Combine that with a fetch mode of PDO::FETCH_CLASS and you get a completely OOP dev experience, with the consumer of the results completely unaware that the results are actually a PDOStatement (as far as the consumer is concerned, the results are a collection of objects). Then, if you later want to change the database access layer (DAL) or feed a different collection type to the consumer, you don't need to change a single statement in the consumer. Abstraction, it's a super-big win.

    Follow the advice in my sig and search the forums for more.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Jon Skeet's and Eric Raymond's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

Similar Threads

  1. Midtown Madness 2
    By beatles-143 in forum Off Topic
    Replies: 0
    Last Post: 01-04-2011, 04:35 PM
  2. XHTML Madness!
    By thorx10 in forum Scripts, 3rd Party Apps, and Programming
    Replies: 4
    Last Post: 12-27-2009, 05:56 AM
  3. XHTML Madness!
    By thorx10 in forum Graphics & Webdesign
    Replies: 0
    Last Post: 12-26-2009, 11:35 PM
  4. March madness
    By BentFX in forum Off Topic
    Replies: 2
    Last Post: 03-21-2008, 12:43 PM
  5. Referrer Madness
    By engel in forum Scripts, 3rd Party Apps, and Programming
    Replies: 2
    Last Post: 11-04-2007, 01:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
dedicated servers