1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

More trouble migrating to PHP5...now w/ SQL queries

Discussion in 'PHP' started by Sanzbar, Dec 30, 2007.

  1. #1
    Yesterday some of the guys helped me out with some php include issues that were happening when migrating to php5 here:

    http://forums.digitalpoint.com/showthread.php?t=625008

    I decided that rewriting my variables in the code was the best thing to do to avoid future issues. Once I solved this problem, a completely different problem poped up when running some standard SQL queries.

    I have two different database based scripts that work perfectly in PHP4, but as soon as I switch it over to PHP5, one of them all of a sudden finds no values in the database and the other gives the following error:

    mysql_num_rows(): supplied argument is not a valid MySQL result resource

    Again, no changes in the code...it works perfect in PHP4 but doesn't work on PHP5.

    Any ideas on issues with SQL compatibility between PHP4 and 5, or any other ideas of why this wouldn't work when migrating? Below is a rough view of some of the code that I edited down to show the main structure.

    Thanks in advance!

    <?php
    require_once("config.php");
    
    $sql_query = "SELECT * FROM database WHERE `primary` = '".$id."'";
    $result = mysql_query($sql_query);
    
    if(mysql_num_rows($result))
    {
    
    while($row = mysql_fetch_row($result))
    {
    print ("$row[0]");
    }
    
    }
    
    else
    {
    echo "no values in the database";
    }
    ?>
    
    Code (markup):
     
    Sanzbar, Dec 30, 2007 IP
  2. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are those query run after a form is processed? I mean, does it depend on data entered by visitors?
    If so, you have 2 choices :

    1 - rewrite all variables that come from forms like this :
    $var becomes $_GET['var'] or $_POST['var'] , the choice depends on the method property used on the html form.
    2 - Set the register_globals to ON in the php config file (php.ini), but this is a bad choise, even PHP will not allow you to use this technique anymore in PHP 6.
     
    selling vcc, Dec 30, 2007 IP
  3. Sanzbar

    Sanzbar Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No. The query is run based on a URL passed variable. For example, script.php?id=whatever, and it uses id as a sort.
     
    Sanzbar, Dec 30, 2007 IP
  4. kendo1979

    kendo1979 Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i think it's your register_global setting, it's by default ON on php 4 and OFF on php5

    since it will be removed on php6, you should start changing your code.

    change all variable you get from a url like script.php?id=whatever from mere $id to $_GET['id']

    watch out form post / get variable, don't get them mixed up.
     
    kendo1979, Dec 30, 2007 IP
  5. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #5
    So your variable are stored on the $_GET array,
    $_GET['id']=watever;

    My first reply is still valid as url based variable are stored in the $_GET array if the register_globals is On, if it's Off these variable are stored in the same name in the url ($id=variable).
     
    selling vcc, Dec 30, 2007 IP
  6. coches

    coches Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    instead of us guessing what the problem is,
    you can basically debug it a bit yourself

    <?php
    require_once("config.php");
    echo $id;// where does this come from $_GET or $_POST ?
    $sql_query = "SELECT * FROM database WHERE `primary` = '".$id."'";
    $result = mysql_query($sql_query) OR die(mysql_error());// show an error

    if(mysql_num_rows($result) > 0 ) // are there more then 0 results
    {

    while($row = mysql_fetch_row($result))
    {
    print ("$row[0]");
    }

    }

    else
    {
    echo "no values in the database";
    }
    ?>
     
    coches, Dec 31, 2007 IP
  7. Sanzbar

    Sanzbar Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks guys!
     
    Sanzbar, Dec 31, 2007 IP
  8. kendo1979

    kendo1979 Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i thought hen register global is ON, all passed variable will be available as their name? so it's reverse of hat you say. CMIIW
     
    kendo1979, Jan 1, 2008 IP
  9. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #9
    My mistake!!!
     
    selling vcc, Jan 1, 2008 IP