carleihar Posted November 12, 2009 Share Posted November 12, 2009 I've tried to find this, but I can't seem to find a clear answer. I'm simply trying to extract one piece of information/cell (column and row) from a database and assign that value to a variable. Could someone help me understand? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/181292-cells/ Share on other sites More sharing options...
s0c0 Posted November 12, 2009 Share Posted November 12, 2009 What database engine are you using (for instance MySQL). Can we see a query? Can we see code you've written? Do you know SQL so you can get that data out? More information is helpful here. To give you an idea though. // database connection here $sql = "SELECT cell FROM table WHERE id='$id'"; $r = mysql_fetch_assoc(mysql_query($sql)); // reference data through the $r['cell'] variable Quote Link to comment https://forums.phpfreaks.com/topic/181292-cells/#findComment-956446 Share on other sites More sharing options...
carleihar Posted November 12, 2009 Author Share Posted November 12, 2009 Oh yes, I'm sorry. I'm using MySQL. So say if I wanted to extract a value in a certain column in a certan row using a user ID. (Say for instance, the email address for a certain user and then store that email address in variable $address, so that I could easily create html with that variable) Does that make much sense? I'm pretty knew to MySQL so I don't know much of any code in PHP to accesse my databases. Quote Link to comment https://forums.phpfreaks.com/topic/181292-cells/#findComment-956459 Share on other sites More sharing options...
mikesta707 Posted November 12, 2009 Share Posted November 12, 2009 pretty much what s0c0 said. $sql = "SELECT email FROM table WHERE id=$id LIMIT 1";//limit to 1 row returned $query = mysql_query($sql); $row = mysql_fetch_assoc($query);//could also use fetch_array or fetch_row i guess, but i prefer fetch_assoc $cell = $row['email'];//$cell now contains the email address just replace table with your table name, and id with whatever the userID column is, and obviously $id with whatever variable you are using that stores the id of the row you want to access Quote Link to comment https://forums.phpfreaks.com/topic/181292-cells/#findComment-956463 Share on other sites More sharing options...
carleihar Posted November 12, 2009 Author Share Posted November 12, 2009 I'll try that. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/181292-cells/#findComment-956474 Share on other sites More sharing options...
s0c0 Posted November 12, 2009 Share Posted November 12, 2009 You need to establish a mysql connection first. It may be helpful for you to get a book from the library on php mysql. I learned from this book many years ago and it did wonders for my career: http://www.amazon.com/gp/product/0672329166/ref=pd_lpo_k2_dp_sr_1/190-7625085-1967410?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=lpo-top-stripe-1&pf_rd_r=17DBYJW757PXX0S7B2V3&pf_rd_t=201&pf_rd_p=486539851&pf_rd_i=0672317842 its called PHP and MySQL Web Development. For information on mysql connections: http://php.net/manual/en/function.mysql-connect.php Quote Link to comment https://forums.phpfreaks.com/topic/181292-cells/#findComment-956492 Share on other sites More sharing options...
carleihar Posted November 12, 2009 Author Share Posted November 12, 2009 Oh yes, I know a small amount about SQL and I can connect with my database and such. Quick question: is $row a designated php variable? I've used this before and I didn't know if I could change it or if it stands for something special. Also, if I wanted to do something similar to this (with say, password) would I need to rename all these variables differently? or could I leave $sql/$query/$row? Thanks again everyone for the help! Quote Link to comment https://forums.phpfreaks.com/topic/181292-cells/#findComment-956497 Share on other sites More sharing options...
mikesta707 Posted November 13, 2009 Share Posted November 13, 2009 variable names don't matter. you could name your variables goboldygook1, 2, and 3 and it would work fine Quote Link to comment https://forums.phpfreaks.com/topic/181292-cells/#findComment-956573 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.