dfalkowitz Posted July 28, 2011 Share Posted July 28, 2011 Is there a way to echo a value from a mySQL database? For example: table ID AGE 1 21 2 22 PHP code $query = "SELECT * FROM table WHERE 'ID' = ?????????"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo $row['AGE']; What would go in replace of the "????????" Quote Link to comment Share on other sites More sharing options...
dfalkowitz Posted July 28, 2011 Author Share Posted July 28, 2011 One thing to add - I want to return only the AGE of either ID = 1 or ID = 2. I have a form drop-down with option 1 and 2, if option 1 is chosen, I only want to show the AGE for ID = 1... Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 $q = mysql_query("SELECT * FROM table WHERE `ID` = '1'") or die(mysql_error()); $r = mysql_fetch_assoc($result) or die(mysql_error()); echo $r['AGE']; Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted July 28, 2011 Share Posted July 28, 2011 Why have you put backticks around ID? and why have you put the number 1 inside quotes? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 personal habit. I always use backticks around field names, and always enclose the values in single quotes. Quote Link to comment Share on other sites More sharing options...
dfalkowitz Posted July 28, 2011 Author Share Posted July 28, 2011 Thanks for replies! I don't want this because this will only show the AGE for option 1 $q = mysql_query("SELECT * FROM table WHERE `ID` = '1'") or die(mysql_error()); $r = mysql_fetch_assoc($result) or die(mysql_error()); echo $r['AGE']; I need ID to = a variable. If option 1 is chosen then show AGE for ID = 1 but if option 2 is chosen then show AGE for ID = 2... Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 replace 1 with variable. $q = mysql_query("SELECT * FROM table WHERE `ID` = '$variableName'") or die(mysql_error()); $r = mysql_fetch_assoc($result) or die(mysql_error()); echo $r['AGE']; Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted July 28, 2011 Share Posted July 28, 2011 personal habit. I always use backticks around field names, and always enclose the values in single quotes. Always using quotes around values will meen that you can not ever use integer values then as wraping a value within quotes in SQL tells it it's a string. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 really? so if ID field in database is an int, you think SELECT * from `tableName` where `id` = '1' doesn't work? Quote Link to comment Share on other sites More sharing options...
dfalkowitz Posted July 28, 2011 Author Share Posted July 28, 2011 What would the variable be? $row['ID'] or $_GET['ID'] Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 the variable depends on how you're passing it over from the previous page: $variableName = $_GET['variableName']; $variableName = $_POST['variableName']; Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 relative to the personal message you sent me with your code: at a (very) quick glance, your form does not have an action or method. instead of just <form> use something like: <form action="pageThatProcessesForm.php" method="get"> method can also be "post". Quote Link to comment Share on other sites More sharing options...
dfalkowitz Posted July 28, 2011 Author Share Posted July 28, 2011 I still get same errror: Notice: Undefined index: id in C:\xampp\htdocs\php_test\test.php on line 11 (which is $variableName = $_POST['ID'] Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\php_test\test.php on line 14 (which is - while ($row = mysql_fetch_array($result))) Quote Link to comment Share on other sites More sharing options...
premiso Posted July 28, 2011 Share Posted July 28, 2011 dfalkowitz, post your form HTML code. As we cannot be of further assistance without seeing how that is setup. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 premiso, he sent it to me in a personal message (don't know why) here it is: <html> <head> <script type="text/javascript"> function showUser(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","test.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <form> <select name="users"> <option value="">Select ID:</option> <option value="1">1</option> <option value="2">2</option> </select> <input name="test" type="button" value="submit" onClick="showUser(this.value)"> <input name="test" type="text" id="txthint" size="50"> </form> <br /> <div id="txtHint"><b>Age info will be listed here.</b></div> </body> </html> Quote Link to comment Share on other sites More sharing options...
dfalkowitz Posted July 28, 2011 Author Share Posted July 28, 2011 test.php below <?php $dbhost = "localhost"; $dbuser = "test"; $dbpass = "test"; $dbname = "php_test"; //Connect to MySQL Server mysql_connect($dbhost, $dbuser, $dbpass); //Select Database mysql_select_db($dbname) or die(mysql_error()); $variableName = $_GET['ID']; $result = mysql_query("SELECT * FROM test WHERE id = $variableName"); while ($row = mysql_fetch_array($result)) { echo $row['AGE']; echo "<br />"; } ?> Quote Link to comment Share on other sites More sharing options...
premiso Posted July 28, 2011 Share Posted July 28, 2011 $variableName = $_GET['ID']; This should correspond with the name of your "users" select. $variableName = $_GET['users']; Since you named it 'users' you use users. If you want it to be ID, change the select box name to be ID. Quote Link to comment 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.