2013-04-27 96 views
0

我一直在嘗試各種各樣的事情,出於某種原因,無論我做什麼,我似乎無法用可靠的查詢...迄今爲止,這裏是我的代碼,它並非全部,但香港專業教育學院銷指出哪裏我的問題是....

<?php 




    $hostname = 'localhost';  // Your MySQL hostname. Usualy named as 'localhost', so you're NOT necessary to change this even this script has already online on the internet. 
$dbname = 'ServiceHistoryDB'; // Your database name. 
$username = 'root';    // Your database username. 
$password = '';     // Your database password. If your database has no password, leave it empty. 

// Let's connect to host 
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!'); 
// Select the database 
mysql_select_db($dbname) or DIE('Database name is not available!'); 

$custnum = '1'; 

    function connect(){ 

     mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!'); 
     mysql_select_db($dbname) or DIE('Database name is not available!'); 
    } 

    function close(){ 
     mysql_close(); 
    } 

     function query(){ 
      $mydata = mysql_query("SELECT * FROM vehicles WHERE CustNum=1"); 
      while($record = mysql_fetch_array($mydata)){ 
       echo '<option value="' . $record["Model"] . '">' . $record["Model"] . '</option>'; 
     } 

    } 










?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>testdropdown</title> 
</head> 

<body> 

<select name="dropdown"> 
<?php query() ?> 
</select> 
<?php close() ?> 
</body> 
</html> 
+0

你得到什麼錯誤或問題?還有什麼你在'或死(mysql_error())'? – 2013-04-27 04:31:39

+0

扔掉這個,用mysqli或PDO重新開始:) – 2013-04-27 04:31:58

+0

你可以這樣做:$ mydata = mysql_query(「SELECT * FROM vehicles WHERE CustNum = $ CustNum」);'我假設CustNum是int – 2013-04-27 04:33:16

回答

0

好了,好了,你可以簡單地在變量像下面寫:

$variable = 'someData'; 
"Select someColumn from yourTable where someColumn = $variable"; 

但是,如果你從一個函數這樣做您可能需要聲明global變量以檢索值i ˚F的值設置功能外:

function someFunc(){ 
    global $variable; // set above 
    "Select someColumn from yourTable where someColumn = $variable"; 
} 

使用準備好的語句

您應該切換到mysqli prepared statements因爲它們更安全:

if($stmt = $mysqli->prepare($yourSQL)){ 
    $stmt->bind_param("s", $variable); 
    $stmt->execute(); 
    $stmt->bind_result($thisHoldsThe_QueryResult); 
    $stmt->fetch(); 
    $stmt->close(); 
} 
+0

我認爲全球變量是什麼人,感謝您的及時幫助,下一次我有問題,肯定是在這裏張貼他們 – user2325960 2013-04-27 14:36:02