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
https://forums.phpfreaks.com/topic/243096-where-statement-mysql-value/
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...

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.

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

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

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>

 

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

  }

 

?>

Archived

This topic is now archived and is closed to further replies.

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