Jump to content

WHERE statement = mySQL value


dfalkowitz

Recommended Posts

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 "????????"

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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)))

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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 />";

  }

 

?>

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.