user501582
user501582

Reputation: 95

Issue with PHP variable when fetched from MySQL

I have a variable like "Doctor and Surgical" I take this value from Mysql database and store it in a variable. When I echo this variable it gives me only "Doctor" Can anyone tell me how can I take the above value retrieved from Mysql as an entire variable...

EDIT

Please find the code below

query.php
<form action="process.php" method="post">
<select name="cat">
<?php
$sql="select distinct Category from tbl_1 order by Category asc";
$query=mysql_query($sql);

while($row=mysql_fetch_array($query))
{

 echo "<option value=".$row[Category].">".$row[Category]."</option>";
}
?>
</select>
<select name="station">
<?php
$sql_1="select distinct Station from tbl_1 order by Station asc";
$query_1=mysql_query($sql_1);

while($row=mysql_fetch_array($query_1))
{

echo "<option value=".$row[Station].">".$row[Station]."</option>";
}
?>
</select>
<input name="C" type="submit" />
</form>



process.php

$myValue =$_POST['cat'];
$myStation=$_POST['station'];

echo $myValue;
echo "<br/>";
echo $myStation;

$mySqlStm ="SELECT Name FROM tbl_1 WHERE Category='$myValue' and Station='$myStation'";

$result2 = mysql_query($mySqlStm) or die("Error:mysql_error()"); 

if(mysql_num_rows($result2) == 0){ 
echo("<br/>no records found"); 
} 
ELSE 
{ 
echo "<table border='1'>"; 

//ECHO THE RECORDS FETCHED
while($row = mysql_fetch_array($result2)) 
{ 
echo "<tr>"; 

echo "<td>" . $row['Name'] . "</td>"; 
}}

Here when I echo $myValue it gives me "Doctor" instead of "Doctor and Surgeon" I need "Doctor and Surgeon as an entire variable.

Upvotes: 0

Views: 150

Answers (4)

Rohit
Rohit

Reputation: 401

Use single quotes & Just insure the value of $row['Category'] Try it: echo "".$row['Category']."";

Upvotes: 0

JakeParis
JakeParis

Reputation: 11210

Think of the quotes just like you would a bracket or an html tag. When you open one, you are inside until you close it. In this case, you needs quotes to tell php what to echo out and different quotes that will print for html attributes. It's usually easier to use single quotes for the php echo commands and double quotes for the html code. Then the . just means to stitch a bunch of stuff together without having to put the echo command ten times in a row. So when you have this:

echo '<option value="'.$row[Category].'">'.$row[Category].'</option>';

you can break it down like this (just for your mental clarity, don't actually code this):

echo 
    '<option value=" '
    .
    $row[Category]
    .
    ' "> '
    .
    $row[Category]
    .
    '</option>'
;

Upvotes: 0

Jack
Jack

Reputation: 301

You can use this without having to concatenate:

echo "<option value='{$row[Category]}'>{$row[Category]}</option>";

Upvotes: 1

Reza S
Reza S

Reputation: 9748

You need to quote the value properly:

 echo "<option value='".$row[Category]."'>".$row[Category]."</option>";

With your current code, if you look at the source you will probably see something like this:

 <option value=Some Category>Some Category</option>

But with proper quotation you'll see the correct value.

Upvotes: 2

Related Questions