what s wrong with this piece of code?

Discussion in 'PHP' started by schlogo, Sep 27, 2008.

  1. #1
    Hello

    here s the beginning of a script, when it gets to case forum 2, it gives me an error msg saying "no selected database" What s wrong?Must be something obvious I don't see :D Any idea is welcome


    
    $dbhost = 'localhost';
    $dbuser = 'user';
    $dbpass = 'password';
    $dbbase = 'database';
    
    $sql = mysql_connect($dbhost,$dbuser,$dbpass);
     mysql_select_db($dbbase);
    
    
    if(isset($_POST) && !empty($_POST['login']) && !empty($_POST['login2']) && !empty($_POST['pass']))
    {
      extract($_POST);
    
      switch ($_POST['cat'])
      {
        case "forum2" :
         
            $qry = "select licence,pass,nom,prenom,sexe from autorise where nom='".$login."' and prenom='".$login2."'";
            $req = mysql_query($qry) or die('ça marche pas !<br>'.$qry.'<br>'.mysql_error());
            $data = mysql_num_rows($req);
    PHP:

     
    schlogo, Sep 27, 2008 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    are all of these set correctly?
    $dbhost = 'localhost';
    $dbuser = 'user';
    $dbpass = 'password';
    $dbbase = 'database';
    PHP:
    do you have the database set up? with adequate permissions for the user?

    change
    $sql = mysql_connect($dbhost,$dbuser,$dbpass);
     mysql_select_db($dbbase);
    PHP:
    to
    $sql = mysql_connect($dbhost,$dbuser,$dbpass) or die('COULD NOT CONNECT TO DB');;
    mysql_select_db($dbbase) or die('COULD NOT SELECT DB');
    PHP:
     
    JAY6390, Sep 27, 2008 IP
    schlogo likes this.
  3. schlogo

    schlogo Well-Known Member

    Messages:
    1,615
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    #3
    db is installed and set correctly, user and things are ok , i ll try your code (last one ) in 2 minutes

    Thanks for your answer :d
     
    schlogo, Sep 27, 2008 IP
  4. schlogo

    schlogo Well-Known Member

    Messages:
    1,615
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    #4
    ok..I can t believe i ve spent 3 days (not 24/24h ) on that ... Because there were no die instruction after the select, i took for granted that it was working and rather looked at the line giving the error...the first mysql instruction after the select.

    After I ve added the die check, i of course realised the db name (the one i ve checked 20 times in the last 3 days) is not the good one, i changed a "-" for a "_" .

    I knew it was something obvious :D

    Tks for your help, you get rep for it :D
     
    schlogo, Sep 27, 2008 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    lol, it had to be one of them two lines :)
     
    JAY6390, Sep 27, 2008 IP