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.

PHP4 > PHP5 help

Discussion in 'PHP' started by BRUm, Nov 18, 2006.

  1. #1
    Hello,

    I have a problem with my script. Originally, it worked perfect on my old server which was running a PHP4 version. However, I had to transfer it to a new server which is running PHP5. I'm now experiencing problems with it, and I think it's down to the new version.

    When logging in, I'm getting:

    The corresponding PHP code lines for 46 are:

    
     $query1 = mysql_query("SELECT * FROM users WHERE username = '$username'");	  
    while($info = mysql_fetch_array( $query1 )){
    ...
    }
    
    PHP:
    And for 70 they are:

    
    if (!$info=mysql_fetch_array($query1)){
    ...
    }
    
    PHP:
    Any help is greatly appreciated as this script is very important to me.

    Thanks,

    Lee.
     
    BRUm, Nov 18, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    If you changed the server, did your database go with you? Try replacing this.
    
    $query1 = mysql_query("SELECT * FROM users WHERE username = '$username'");    
    
    PHP:
    with this.
    
    $query1 = mysql_query("SELECT * FROM users WHERE username = '$username'") OR die( mysql_error() );  
    
    PHP:
     
    nico_swd, Nov 18, 2006 IP
    BRUm likes this.
  3. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #3
    I had to make a new DB but it's all configured to it within the code. I'll try what you advised, thanks :)

    EDIT: I just tried it, and it doesn't seem to have given any more output :/
     
    BRUm, Nov 18, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    I don't think this is an PHP5 issue... hm, try echoing the query string and see if it contains the expeced content.

    
    echo "SELECT * FROM users WHERE username = '$username'";
    
    PHP:
    Also, you can try putting this at the top of your page.
    
    
    error_reporting(E_ALL);
    
    PHP:
     
    nico_swd, Nov 18, 2006 IP
  5. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #5
    brum, this warning is the result of trying to fetch a result from a query that returned zero results. not a php5 issue.

    as advised, add or die(mysql_error()); at the end of each of the mysql_ functions you're using including the connection to the db and the db selection, so that you can debug whats going on.
     
    legend2, Nov 19, 2006 IP
  6. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #6
    I added the or die(mysql_error()); and it didn't give any more information than it did before I used it :/
     
    BRUm, Nov 19, 2006 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Did the error_reporting() give any more info?
     
    nico_swd, Nov 19, 2006 IP
  8. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #8
    Oh my WORD! Sorry guys! I just realised how much of an idiot I've just been ... I have THREE folders on my webspace referring to cpcads, lol I've been using the wrong folder! I just uploaded to the right folder, and I get:

     
    BRUm, Nov 19, 2006 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    This means there's an non existant key in an array. This isn't necessarily a bad thing. The error_reporing just informs you of that. Does the MySQL part work after you changed the folder?
     
    nico_swd, Nov 19, 2006 IP
  10. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #10
    Damn it, after I deleted the debugging code, it returns this again:

     
    BRUm, Nov 19, 2006 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    Did you echo the query string as I suggested? Maybe it contains unexpected content.
     
    nico_swd, Nov 19, 2006 IP
  12. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #12
    When I echo it as you said, it just echoes the command:

     
    BRUm, Nov 19, 2006 IP
  13. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #13
    Post your whole code.
     
    nico_swd, Nov 19, 2006 IP
  14. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #14
    OK, here goes:

    
    <?php
    
    $logout = $_REQUEST['out'];
    if($logout == 1){
    setcookie('username','',time()-5);
    setcookie('submit','',time()-5);
    setcookie('pass','',time()-5);
    ECHO "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=account.php'>";
    exit;
    }  
    		  
    $dbh=mysql_connect ("localhost", "***", "***") or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("***");
    		  
    		  
    		  $promo = $_REQUEST['promo'];
    		  $marketer = $_REQUEST['marketer'];
    		  
    		  IF($promo == 0 && ($marketer == 0)){
    		  setcookie('checkbox','none',time()+5);
    		  setcookie('submit','2');
    		  ECHO "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=account.php'>";
    		  //ECHO "Didn't select one account";
    		  }ELSE{
    		  }
    		  
    		  IF($promo == 1 && ($marketer == 1)){
    		  setcookie('checkbox','both',time()+5);
    		  setcookie('submit','2');
    		  ECHO "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=account.php'>";
    		 // ECHO "Selected both accounts";
    		   }ELSE{
    		   }
    		  
    		  $username = $_REQUEST['username'];
    		  $password = $_REQUEST['password'];
    		  
    		  	IF($promo == 1){  
    			
    			//echo "Promo == 1";
    				//echo "Username is $username";
    				//echo "<br>password is $password";
    		  // now check
    		  $query1 = mysql_query("SELECT * FROM users WHERE username = '$username'");
    
    			 
    		  while($info = mysql_fetch_array( $query1 )){
    		
    		
    				//echo $info['username'];
    				
    
    			
    //setcookie('submit','2');
    if ($password != $info['password']){
    //Wrong password, send back
    //echo "wrong pass entered";
    setcookie('pass','2',time()+5);
    setcookie('submit','2');	
    ECHO "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=account.php'>";
    }ELSE{
    //Right! send back
    //echo "username and pass OK";
    setcookie('submit','2');	
    setcookie('username',"$username");
    setcookie('acctype',"<b>P</b>romo");
    ECHO "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=account.php'>";
    exit;
    }
    }
    if (!$info=mysql_fetch_array($query1)){
    //echo "Username not found";
    setcookie('pass','2',time()+5);
    setcookie('username','',time()-5);
    setcookie('submit','2');
    ECHO "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=account.php?user=2'>";
    }
    }
    
    
    IF($marketer == 1 && ($promo == 0)){
    
    // now check
    		  $query1 = mysql_query("SELECT * FROM users2 WHERE username = '$username'");
    		 while($info = mysql_fetch_array( $query1 )){
    		  
    		  
    		  //$result = mysql_query($query1);
    		  //$exist=mysql_num_rows($result);
    		
    				
    setcookie('submit','2');
    $ppoints = $info['points'];
    $ppused = $info['pused'];
    setcookie('points',"$ppoints");
    setcookie('pused',"$ppused");		  
    if ($password != $info['password']){
    //Wrong password, send back
    setcookie('pass','2',time()+5);
    ECHO "<META HTTP-EQUIV='Refresh' CONTENT='0;URL='account.php'>";
    exit;
    }
    //Right! send back
    setcookie('username',"$username");
    setcookie('acctype',"<b>M</b>arketer");
    ECHO "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=account.php'>";
    exit;
    }
    if (!$info=mysql_fetch_array($query1)){
    //echo "Username not found";
    setcookie('pass','2',time()+5);
    setcookie('username','',time()-5);
    setcookie('submit','2');
    ECHO "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=account.php?user=2'>";
    }
    }
    
    ?>
    
    PHP:
    P.S. Hope there aren't any rival PPC companies watching this thread ;)
     
    BRUm, Nov 19, 2006 IP
  15. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I would remove the single quotes from around the variables in the SQL queries. The single quotes can have the effect of literalizing the query so that MySql is looking for $username instead of the "bob".

    See if that does the trick.
     
    clancey, Nov 19, 2006 IP
    BRUm likes this.
  16. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #16
    Thanks for the help. I tried removing the single quotes but it's still giving the same errors :/
     
    BRUm, Nov 20, 2006 IP
  17. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #17
    I tested a version of the parts of your script which are giving you a problem on a local database. I changed the minimum needed to make it work -- leaving the code intact. I did not have a problem with it. I am using PHP 5.1.1 with Zend 2.1.0

    So I tested on a second database and I was able to generate the same errors because the user did not have the privileges needed to access the database. Is this the problem?
     
    clancey, Nov 20, 2006 IP
  18. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #18
    Hmm interesting. Thanks for your experimenting :) I'll have a look.. I'll make another user with full privileges and replace it with the old one in my code, and see if that works.

    EDIT: Grrr still doesn't work and I've doubled checked the name of the DB, username and password. Hmm..
     
    BRUm, Nov 21, 2006 IP
  19. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #19
    This is the worst part of debugging. In you situation, I would do the following.

    Write a very small routine to check database access. From the code we know the user is able to access MySql. This is because it is not dieing with a failed access to MySql.

    I would do a redundant check to be certain I granted that user full access to the specific database.

    I would pick a table and with a simple query, see if I can access that table and display contents from a column. I normally do this at the command line because it is much faster than using a browser to test. The error codes are the same.

    I also capture and print row counts to the screen to help verify that it is working. I see that you were doing something similar.

    If that works, I would then try the query which is giving me grief, pre-setting tthe values in the test script.

    If that works, I would try it from the web. If that fails, I would make sure all error settings are turned on and direct them to a separate file. Those errors may give us a clue as to what is the issue between your web servers, PHP, and MySql.

    I am certain you have tried this, but sometimes stepping through once more reveals the problem.

    This is especially mysterious because the code worked under PHP 4.x. I do encounter problems in running other people's scripts under PHP 5.x because it is stricter than 4.x, but it looks like you follow good coding style.
     
    clancey, Nov 21, 2006 IP
  20. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #20
    Thanks a lot for your advice Clancey, I'll crack on with debugging.
     
    BRUm, Nov 22, 2006 IP
    clancey likes this.