Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

simple sql help

  • 02-02-2008 01:51PM
    #1
    Closed Accounts Posts: 94 ✭✭


    Hi,

    I have a table in a database which has 2 fields, userid and impressions

    what I want to do is specify the userid and read the impressions for that userid

    what query would i use? thanks


Comments

  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    In SQL you'd do this

    select impressions from tablenamehere where userid=50;
    

    ???


  • Closed Accounts Posts: 94 ✭✭gnomer


    thank you!

    i dont no anything about sql


  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    no problem :)


  • Closed Accounts Posts: 94 ✭✭gnomer


    also this would just show the impression number if I executed this in PHP?


  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    forbairt wrote: »
    select impressions from tablenamehere where userid=50;
    
    <?php
    $userid = 2;
    $query = "select impressions from tablenamehere where userid=$userid";
    $res = mysql_query($query);
    $row = mysql_fetch_row($res))
    echo $row['impressions'];
    ?> 
    

    from my head ... may / may not work


  • Advertisement
  • Closed Accounts Posts: 94 ✭✭gnomer


    more trouble

    heres my PHP:


    $query = "select impressions from unpaidhits where userid='1'";

    $result = mysql_query($query);
    echo $result;

    ?>
    $result should be 1 but it is Resource id #3

    what am I doing wrong?


  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    gnomer wrote: »
    more trouble

    heres my PHP:


    $query = "select impressions from unpaidhits where userid='1'";

    $result = mysql_query($query);
    echo $result;

    ?>
    $result should be 1 but it is Resource id #3

    what am I doing wrong?

    you've sent the query to mysql ... its sent back a resource ... which you need to perform a mysql_fetch_row on .. and that will in turn send back an associative array ... and you'll want to extract that like in my example


  • Closed Accounts Posts: 94 ✭✭gnomer


    just saw your post

    your line 17 is wrong somewhere

    you have 2 close brackets and one open bracket and no ; and when i changed your last closed bracket to ; it gives me :


    Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/***/public_html/test.php on line 17


  • Closed Accounts Posts: 94 ✭✭gnomer


    oops myfault i forgot it fill out the tablenamehere with my table name.

    when I run your example im just getting a blank page


  • Closed Accounts Posts: 94 ✭✭gnomer


    anyone?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    er in my example change the userid to 1 ? :)


  • Closed Accounts Posts: 94 ✭✭gnomer


    actually ive sorted it all out now

    for anyone wanting to know what I did
    <?php
    $userid='someid';
    $q="select * from yourtable where userid='$userid'";
    $r=mysql_query($q);
    while ($row = mysql_fetch_array($r)){
    $userid=$row/COLOR][COLOR=#FF0000]'userid'[/COLOR][COLOR=#66CC66;
    $impressions=$row/COLOR][COLOR=#FF0000]'impressions'[/COLOR][COLOR=#66CC66;
    echo "Userid: $userid\n";
    echo "Impressions: $impressions";
    }
    ?>


  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    [php]<?php
    $userid='someid';
    $q="select * from yourtable where userid='$userid'";
    $r=mysql_query($q);
    while ($row = mysql_fetch_array($r)){
    $userid=$row;
    $impressions=$row;
    echo "Userid: $userid\n";
    echo "Impressions: $impressions";
    }
    ?>
    [/php]

    er wasn't that pretty much what I said to do ??? :D


  • Closed Accounts Posts: 94 ✭✭gnomer


    lol i dont know im no sql expert someone on another forum gave me that and it seems to work perfectly.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    you could do without the "while". Also what's going to be displayed id no records are found?

    there is a revised code
    [php]
    <?php
    $impressions = "";
    $userid='someid';
    if($userid != ""){//execute query
    $q="select * from yourtable where userid='$userid'";
    $r=mysql_query($q);
    $row = mysql_fetch_array($r);
    $userid=$row;
    $impressions=$row;
    }
    if($impressions != ""){
    echo "Userid: $userid\n".
    "Impressions: $impressions";
    }else{
    echo "Welcome";
    }

    ?>
    [/php]


Advertisement