2014-10-10 57 views
-3

我有這樣的代碼如何執行SQL並從PHP獲取結果?

$tnid = mysql_query("SELECT TrainingID FROM training ORDER BY TrainingID DESC LIMIT 1"); 

上面的查詢將從訓練表返回的最後一行。 如果我回顯$ tnid它會顯示'資源ID#5'。 如果我添加

$d = mysql_fetch_array($tnid); 

那麼我回聲$ d,它會在 U顯示錯誤消息

數組字符串轉換:\ XAMPP \ htdocs中\ PDS \行動\ doInsertSchedule.php在線32

如何顯示查詢的確切結果? 有人請幫忙。

+0

這裏指的http://www.w3schools.com/sql/,http://www.w3schools.com/php/php_ref_mysqli.asp – edCoder 2014-10-10 04:29:22

回答

0
<?php 
    $con=mysqli_connect("example.com","peter","abc123","my_db"); 
    // Check connection 
    if (mysqli_connect_errno()) { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
    $result = mysqli_query($con,"SELECT TrainingID FROM training ORDER BY TrainingID DESC LIMIT 1"); 
    while($row = mysqli_fetch_array($result)) { 
     echo " Training ID - " . $row['TrainingID '] .; 
     echo "<br>"; 
    } 
    mysqli_close($con); 
?> 
+1

謝謝空白頭:) – user3152496 2014-10-10 07:05:20

+0

@ user3152496歡迎您:) – 2014-10-10 08:42:40

0

嘗試......

$con = mysql_connect("host_name", "user_name", "password"); 
mysql_select_db("database_name", $con); 
$query="SELECT TrainingID FROM training ORDER BY TrainingID DESC LIMIT 1"; 
$res=mysql_query($query,$con); 
$farow=mysql_fetch_array($res); 
$answer=$farow['TrainingID']; 
+1

應該使用msqli而不是mysql。 – dannmate 2014-10-10 04:25:45

+0

@dannmate這取決於_him_ – edCoder 2014-10-10 04:30:23

+0

你不應該宣傳不好的做法。 'mysql'庫被刪除,並且@dannmate說,你應該宣傳'PDO'或'MySQLi' – Darren 2014-10-10 04:33:50

相關問題