English 中文(简体)
当与MySQL挂钩时,用购买力平价变量处理
原标题: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

请参阅以下守则:

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.

最佳回答

你们需要适当引用价值:

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

根据您目前的法典,如果你看一下来源的话,你可能会看到这样的情况:

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

但是,如果适当引述的话,你就会看到正确的价值。

问题回答

你们可以使用这一方法,而不必加以压缩:

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

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

你们可以打破这种局面(为了你的智力清晰度,实际上没有这样做):

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

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





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签