Php mysql querys

Discussion in 'PHP' started by -bank-, Jan 19, 2007.

  1. #1
    Hi Guys,

    I just want to ask, as I have just started to learn, how the best way to get single items out of a database and equal the single item, ie username, to equal a single variable?

    Thanks Alot
     
    -bank-, Jan 19, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    mysql_query("SELECT * FROM `table` WHERE username = '$username' LIMIT 1");
    
    PHP:
     
    krakjoe, Jan 19, 2007 IP
  3. -bank-

    -bank- Well-Known Member

    Messages:
    674
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Ok, so would that mean:

    $username = 'username';
    $userlogin = mysql_query("SELECT * FROM `table` WHERE username = '$username' LIMIT 1");

    so then $userlogin would equal username correct?

    Thanks
     
    -bank-, Jan 19, 2007 IP
  4. ThomasNederman

    ThomasNederman Peon

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    maybe this helps

    function DBsql($sql)
    {

    $link = mysql_connect("localhost", "xxx", "xxx")
    or die("Could not connect : " . mysql_error());
    mysql_select_db("database") or die("Could not select database");

    $resultx = mysql_query($sql) or die("Query failed : " . mysql_error());

    return $resultx;
    };


    $RS = DBsql("select UserID from UserSessions where username='".$user."';");
    $row = mysql_fetch_array($RS, MYSQL_ASSOC);

    Echo "UserID = ".$row['UserID'];
     
    ThomasNederman, Jan 19, 2007 IP
  5. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #5
    You wouldn't want to do this for every query.
    
    function DBsql($sql)
    {
    
    $link = mysql_connect("localhost", "xxx", "xxx")
    or die("Could not connect : " . mysql_error());
    mysql_select_db("database") or die("Could not select database");
    
    $resultx = mysql_query($sql) or die("Query failed : " . mysql_error());
    
    return $resultx;
    };
    
    PHP:
    Do the connect seperately.

    This won't work cause $user never gets there.
    
    function LoggedIn(){
    $RS = DBsql("select UserID from UserSessions where username='".$user."';");
    $row = mysql_fetch_array($RS, MYSQL_ASSOC);
    
    return $row['UserID'];
    
    }
    
    PHP:
     
    noppid, Jan 19, 2007 IP
  6. -bank-

    -bank- Well-Known Member

    Messages:
    674
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    120
    #6
    Ok, say I requested a page like, http://somesite.com/page.php?item=10

    therefore the code would be something link this:

    $item;
    $description = mysql_query("select `id` from `items` where `id`='".$item."';");

    therefore if I write:

    $text = $description->$item;
    echo $text;

    then the content of that item will be shown?

    Thanks
     
    -bank-, Jan 19, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    Almost,

    
    <?
    /*
     Database connection
    */
    $db = mysql_connect("localhost", "root", "WyeValley");
    if($db)
    {
    	if(!@mysql_select_db("help"))
    	{
    		die("Cannot select database");
    	}
    }
    else
    {
    	die("Cannot connect to mysql server");
    }
    /*
     Script start ....
    */
    # Set $id from the value of id in the address bar, stripslashes does what it sounds
    # like, trim takes away white space from the beginning and end of the variable, just
    # incase it exists, and mysql_real_escape_string is some built in sql injection protection
    $id = mysql_real_escape_string(stripslashes( trim( $_GET['id'] ) ), $db); 
    if($id != "") # If the id is set carry on with operations
    {
     	# Query the database to collect the row for the id set from $id, you
     	# want to limit the query, just because it makes good practive to tell
     	# the server exactly what to select, you could go one step further and 
     	# specify the columns you want to retrieve data from,
     	# "SELECT id,title,description FROM `items` WHERE id = '$id' LIMIT 1"
     	# the above line is an example of how to select specific rows from the
     	# table
    	$result = mysql_query("SELECT * FROM `items` WHERE id = '$id' LIMIT 1", $db);
    	# If the query is successfull and more than one row is affected
    	# by the above line, then go ahead and do something with the data
    	# in this case, I'll just echo out the whole row
    	if($result and mysql_num_rows( $result ) > 0)
    	{ 
    	  # This is how to achieve a loop over a result
    	  # this will collect all the data you need into
    	  # an associative array
    	  while($temp = mysql_fetch_assoc( $result ))
    	  {
    	    echo "<pre>";
    	    print_r($temp);
    	    echo "</pre>";
    	  }
    	}
    	# The query did not succeed, so die
    	else
    	{
    		die("Cannot query databse for $id");
    	}
    }
    else
    {
     	# The id is not set, so die
    	echo "No ID selected, please include the id in the url like <a href=\"?id=1\">THIS</a>";
    }
    ?>
    
    PHP:
    Theres some examples of interaction with some helpful info near by ..... something to remember would be that mysql_query doesnt return text it returns a resource, look further into mysql_result and mysql_fetch_assoc or mysql_fetch_array to get more details on how to retrieve the actual data stored in the database, $res = mysql_query(""); returns 1 or true on success and 0 or false on failure
     
    krakjoe, Jan 19, 2007 IP
  8. solidphp

    solidphp Peon

    Messages:
    46
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I think this is what your looking for:

    
    $item=9;
    $description = mysql_fetch_assoc( mysql_query("select `id` from `items` where `id`='".$item."'") );
    echo $description['id'];
    
    PHP:
     
    solidphp, Jan 19, 2007 IP
  9. -bank-

    -bank- Well-Known Member

    Messages:
    674
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    120
    #9
    Thanks for the help. I think i'm sorted, else I'll purchase a book down the city today :)
     
    -bank-, Jan 20, 2007 IP