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
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
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'];
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:
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
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
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: