twilitegxa Posted September 16, 2008 Share Posted September 16, 2008 How do I create a variable from a field from a table in my database? Like I want my variable to be named: $identity But I want that variable to contain the data from the field 'identity' in my table. How can I do that? Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/ Share on other sites More sharing options...
dezkit Posted September 16, 2008 Share Posted September 16, 2008 by table you mean... database table, right? Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642551 Share on other sites More sharing options...
twilitegxa Posted September 16, 2008 Author Share Posted September 16, 2008 Right. I am connected to my database, I just don't remember how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642554 Share on other sites More sharing options...
dezkit Posted September 16, 2008 Share Posted September 16, 2008 $identity = $row["identity"]; just a guess. Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642555 Share on other sites More sharing options...
Maq Posted September 16, 2008 Share Posted September 16, 2008 You need to be a little more specific. Besides there are thousands of examples all over the place. If you are more specific, maybe show some code, we can help you 10x faster. Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642571 Share on other sites More sharing options...
twilitegxa Posted September 16, 2008 Author Share Posted September 16, 2008 Well, this suggestion didn't work. Anyone know how to do it? Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642572 Share on other sites More sharing options...
twilitegxa Posted September 16, 2008 Author Share Posted September 16, 2008 I don't know what code to show you, but I will show what I have so far: <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("smrpg", $con); //get Sailor Moon's data from the "scout" table $result = mysql_query("SELECT * FROM scout where identity = 'Sailor Moon'") or die(mysql_error()); $birthdatemonth = $row["birthdatemonth"]; $birthdateday = $row["birthdateday"]; $birthdateyear = $row["birthdateyear"]; $birthdate = $birthdatemonth . "/" . $birthdateday . "/". $birthdateyear; $identity = $row["identity"]; echo "<h1>This is $identity</h1>"; echo "<table border='0' width='100%'>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td width='25%'>"; echo "<b>Name:</b></td><td>"; echo $row['charactername']; echo "</td></tr><tr><td>"; echo "<b>Element Of Influence:</b></td><td>"; echo $row['elementofinfluence']; echo "</td></tr><tr><td>"; echo "<b>Age:</b></td><td>"; echo $row['age']; echo "</td></tr><tr><td>"; echo "<b>Birthdate:</b></td><td>"; echo $row['$birthdate']; echo "</td></tr><tr><td>"; echo "<b>Height:</b></td><td>"; echo $row['height']; echo "</td></tr><tr><td>"; echo "<b>Blood Type:</b></td><td>"; echo $row['bloodtype']; echo "</td></tr><tr><td>"; echo "<b>Hobbies:</b></td><td>"; echo $row['hobbies']; echo "</td></tr><tr><td>"; echo "<b>Favorite Color:</b></td><td>"; echo $row['favoritecolor']; echo "</td></tr><tr><td>"; echo "<b>Favorite Gemstone:</b></td><td>"; echo $row['favoritegemstone']; echo "</td></tr><tr><td>"; echo "<b>Favorite Food:</b></td><td>"; echo $row['favoritefood']; echo "</td></tr><tr><td>"; echo "<b>Least Favorite Food:</b></td><td>"; echo $row['leastfavoritefood']; echo "</td></tr><tr><td>"; echo "<b>Favorite School Subject:</b></td><td>"; echo $row['favoriteschoolsubject']; echo "</td></tr><tr><td>"; echo "<b>Least Favorite School Subject:</b></td><td>"; echo $row['leastfavoriteschoolsubject']; echo "</td></tr><tr><td>"; echo "<b>Strengths:</b></td><td>"; echo $row['strengths']; echo "</td></tr><tr><td>"; echo "<b>Weaknesses:</b></td><td>"; echo $row['weaknesses']; echo "</td></tr><tr><td>"; echo "<b>Goal:</b></td><td>"; echo $row['goal']; echo "</td></tr></table><p>"; echo $row['biography']; echo "</p>"; echo "</td></tr>"; echo "<tr><td> </td></tr>"; } echo "</table>"; ?> I want to name a variable, and I don't know how to tell in my code that the variable, in this example $identity, will be coming from the table, which is already specified in this code, from the field named "identity". Does this help? Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642577 Share on other sites More sharing options...
beedie Posted September 16, 2008 Share Posted September 16, 2008 you need this: $row = mysql_fetch_array( $result ); before any of the $row['nameetc'] Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642580 Share on other sites More sharing options...
twilitegxa Posted September 16, 2008 Author Share Posted September 16, 2008 I added that code, "$row = mysql_fetch_array( $result );" but it made everything else on my page disappear except for the $identity variable, which did display properly, but nothign else would display. Why did that happen? What am I doing wrong now? <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("smrpg", $con); //get Sailor Moon's data from the "scout" table $result = mysql_query("SELECT * FROM scout where identity = 'Sailor Moon'") or die(mysql_error()); $row = mysql_fetch_array( $result ); $birthdatemonth = $row["birthdatemonth"]; $birthdateday = $row["birthdateday"]; $birthdateyear = $row["birthdateyear"]; $birthdate = $birthdatemonth . "/" . $birthdateday . "/". $birthdateyear; $identity = $row["identity"]; echo "<h1>$identity</h1>"; echo "<table border='0' width='100%'>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td width='25%'>"; echo "<b>Name:</b></td><td>"; echo $row['charactername']; echo "</td></tr><tr><td>"; echo "<b>Element Of Influence:</b></td><td>"; echo $row['elementofinfluence']; echo "</td></tr><tr><td>"; echo "<b>Age:</b></td><td>"; echo $row['age']; echo "</td></tr><tr><td>"; echo "<b>Birthdate:</b></td><td>"; echo $row['$birthdate']; echo "</td></tr><tr><td>"; echo "<b>Height:</b></td><td>"; echo $row['height']; echo "</td></tr><tr><td>"; echo "<b>Blood Type:</b></td><td>"; echo $row['bloodtype']; echo "</td></tr><tr><td>"; echo "<b>Hobbies:</b></td><td>"; echo $row['hobbies']; echo "</td></tr><tr><td>"; echo "<b>Favorite Color:</b></td><td>"; echo $row['favoritecolor']; echo "</td></tr><tr><td>"; echo "<b>Favorite Gemstone:</b></td><td>"; echo $row['favoritegemstone']; echo "</td></tr><tr><td>"; echo "<b>Favorite Food:</b></td><td>"; echo $row['favoritefood']; echo "</td></tr><tr><td>"; echo "<b>Least Favorite Food:</b></td><td>"; echo $row['leastfavoritefood']; echo "</td></tr><tr><td>"; echo "<b>Favorite School Subject:</b></td><td>"; echo $row['favoriteschoolsubject']; echo "</td></tr><tr><td>"; echo "<b>Least Favorite School Subject:</b></td><td>"; echo $row['leastfavoriteschoolsubject']; echo "</td></tr><tr><td>"; echo "<b>Strengths:</b></td><td>"; echo $row['strengths']; echo "</td></tr><tr><td>"; echo "<b>Weaknesses:</b></td><td>"; echo $row['weaknesses']; echo "</td></tr><tr><td>"; echo "<b>Goal:</b></td><td>"; echo $row['goal']; echo "</td></tr></table><p>"; echo $row['biography']; echo "</p>"; echo "</td></tr>"; echo "<tr><td> </td></tr>"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642585 Share on other sites More sharing options...
Maq Posted September 16, 2008 Share Posted September 16, 2008 $result = mysql_query("SELECT * FROM scout where identity = 'Sailor Moon'") or die(mysql_error()); $row = mysql_fetch_array($result); Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642586 Share on other sites More sharing options...
Maq Posted September 16, 2008 Share Posted September 16, 2008 Ah, sorry for the double post, at least you get the idea... Let me know if this works for you. $row = mysql_fetch_array($result); - This basically retrieves an array from the query. Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642587 Share on other sites More sharing options...
Mchl Posted September 16, 2008 Share Posted September 16, 2008 twilitegxa: What are the fields in your table? Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642594 Share on other sites More sharing options...
twilitegxa Posted September 16, 2008 Author Share Posted September 16, 2008 id, identity, charactername, elementofinfluence, age, birthdatemonth, birthdateday, birthdateyear, heightfeet, heightinches, bloodtype, hobbies, favoritecolor, favoritegemstone, favoritfood, leastfavoritefood, favoriteschoolsubject, leastfavoriteschoolsubject, strengths, weaknesses, goal, biography Those are all the fields in this particular table. Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642597 Share on other sites More sharing options...
Mchl Posted September 16, 2008 Share Posted September 16, 2008 Will you display more than one characters on this page, or just one? Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642599 Share on other sites More sharing options...
twilitegxa Posted September 16, 2008 Author Share Posted September 16, 2008 I am trying to write the code to display one character per page, for a profile, when the viewer clicks a name, but since I wasn't sure exactly how to do it, I was starting with the basics of it, which is just pulling the information from each field, and indicating which character it's for. Does that make sense? So to answer your question, yes, only one character one this page. Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642623 Share on other sites More sharing options...
Mchl Posted September 16, 2008 Share Posted September 16, 2008 //get Sailor Moon's data from the "scout" table $result = mysql_query("SELECT * FROM scout where identity = 'Sailor Moon'") or die(mysql_error()); $row = mysql_fetch_array( $result ); $birthdatemonth = $row["birthdatemonth"]; $birthdateday = $row["birthdateday"]; $birthdateyear = $row["birthdateyear"]; $birthdate = $birthdatemonth . "/" . $birthdateday . "/". $birthdateyear; $identity = $row["identity"]; echo "<h1>$identity</h1>"; echo "<table border='0' width='100%'>"; // keeps getting the next row until there are no more to get echo "<tr><td width='25%'>"; echo "<b>Name:</b></td><td>"; echo $row['charactername']; echo "</td></tr><tr><td>"; echo "<b>Element Of Influence:</b></td><td>"; echo $row['elementofinfluence']; echo "</td></tr><tr><td>"; echo "<b>Age:</b></td><td>"; echo $row['age']; echo "</td></tr><tr><td>"; echo "<b>Birthdate:</b></td><td>"; echo $row['$birthdate']; echo "</td></tr><tr><td>"; echo "<b>Height:</b></td><td>"; echo $row['height']; echo "</td></tr><tr><td>"; echo "<b>Blood Type:</b></td><td>"; echo $row['bloodtype']; echo "</td></tr><tr><td>"; echo "<b>Hobbies:</b></td><td>"; echo $row['hobbies']; echo "</td></tr><tr><td>"; echo "<b>Favorite Color:</b></td><td>"; echo $row['favoritecolor']; echo "</td></tr><tr><td>"; echo "<b>Favorite Gemstone:</b></td><td>"; echo $row['favoritegemstone']; echo "</td></tr><tr><td>"; echo "<b>Favorite Food:</b></td><td>"; echo $row['favoritefood']; echo "</td></tr><tr><td>"; echo "<b>Least Favorite Food:</b></td><td>"; echo $row['leastfavoritefood']; echo "</td></tr><tr><td>"; echo "<b>Favorite School Subject:</b></td><td>"; echo $row['favoriteschoolsubject']; echo "</td></tr><tr><td>"; echo "<b>Least Favorite School Subject:</b></td><td>"; echo $row['leastfavoriteschoolsubject']; echo "</td></tr><tr><td>"; echo "<b>Strengths:</b></td><td>"; echo $row['strengths']; echo "</td></tr><tr><td>"; echo "<b>Weaknesses:</b></td><td>"; echo $row['weaknesses']; echo "</td></tr><tr><td>"; echo "<b>Goal:</b></td><td>"; echo $row['goal']; echo "</td></tr></table><p>"; echo $row['biography']; echo "</p>"; echo "</td></tr>"; echo "<tr><td> </td></tr>"; echo "</table>"; You were calling mysql_fetch_array() twice. One is enough for one row, and the second time it was called, it got empty row. Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642631 Share on other sites More sharing options...
twilitegxa Posted September 16, 2008 Author Share Posted September 16, 2008 Well, most of the page displays correctly, including $identity, but the $birthday still won't display for some reason. What am I doing wrong? I tried uploading it to my server to show you, but nothing shows up for some reason. Sorry. I will have to look at it again tomorrow/later today. Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642643 Share on other sites More sharing options...
Mchl Posted September 16, 2008 Share Posted September 16, 2008 I figured you're must be having a hard night Paste your code after the changes, so that we can work on current version As for birthdate: echo "<b>Birthdate:</b></td><td>"; echo $row['$birthdate']; // <--- birthdate is in $birthdate not in $row['birthdate'] echo "</td></tr><tr><td>"; Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-642646 Share on other sites More sharing options...
twilitegxa Posted September 16, 2008 Author Share Posted September 16, 2008 Okay, now it's right. LOL I kept missing things for some reason. Thank you so much for helping me with that! Now, Do you know how I can write it so that it displays is at the month name instead of the number? Like June instead of 06? Is it possible to set it up to do that, even though it is 06 in my database? I'm guessing there's a way, but I'm not sure how exactly. Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-643316 Share on other sites More sharing options...
twilitegxa Posted September 16, 2008 Author Share Posted September 16, 2008 Nevermind that last question. I just changed the way it saves to my database. I figured that would be easier. Thanks for all your help everyone! Quote Link to comment https://forums.phpfreaks.com/topic/124426-solved-naming-variables/#findComment-643343 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.