Selecting username from one database and then inserting into another...

Discussion in 'PHP' started by NeoPhyte101, Mar 3, 2008.

  1. #1
    Hi guys,

    I'm having a problem with a php script. here's the code below.

    $sql= mysql_query("SELECT username FROM `ulmundo`.`users` where userid =$cookie_uid");
    
    $result1= mysql_result($sql);  
    
    
    //add comment
    $q ="INSERT INTO `comments` (article_id, poster_id, page, date, time, username, ip, comment) VALUES ($global_user_id, $cookie_uid, '".$_POST['page']."', '".$_POST['date']."', '".$_POST['time']."', $result1 , '".$_SERVER['REMOTE_ADDR']."','".addslashes($_POST['comment'])."')";
    
    PHP:

    I'm trying to insert the string username taken form the user table and insert it into the comments page. I'm getting an sql error when this code is executed.

    The error is telling me that mysql_result is wrong parameter count. The code works perfectly until i try to insert the value $result1

    Any help would be great thanks
     
    NeoPhyte101, Mar 3, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If it's only erroring when you try to insert the $result1, then I'm going to point out the first error I saw: $result1 holds the username, correct? It's text, so it has to be enclosed in single quotes the same way all your other variables are: '".$result1."' (just setting it up same way you have your other vars)
     
    zerxer, Mar 3, 2008 IP
  3. NeoPhyte101

    NeoPhyte101 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    I've changed it to as you said, there is now no error but it is not entering the username into the field in the database.

    is this statement wrong???

    $result1=mysql_result($sql);

    The field in the database remains blank

    thanks for any help
     
    NeoPhyte101, Mar 3, 2008 IP
  4. chanakya

    chanakya Peon

    Messages:
    361
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    echo $result1 and see the correct output is coming or not
     
    chanakya, Mar 3, 2008 IP
  5. NeoPhyte101

    NeoPhyte101 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry i am getting an error here it is

    Warning: Wrong parameter count for mysql_result() in C:\AppServ\www\view_profile.php on line 211
     
    NeoPhyte101, Mar 3, 2008 IP
  6. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    When you originally connected to the database, you stored the connection in a variable, right? You need to pass that variable as the second parameter.

    $connect_var = mysql_connect(blah,blah,blah);

    //your other stuff

    $result1=mysql_result($sql,$connect_var);
     
    zerxer, Mar 3, 2008 IP
  7. NeoPhyte101

    NeoPhyte101 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi got this working now. Changed mysql_result to

    $result1=mysql_result($sql, 0);


    Thanks guys
     
    NeoPhyte101, Mar 4, 2008 IP