2014-04-02 232 views
0

我需要幫助來解決我的查詢,以便我可以得到結果並將其設置在行上。請幫我在SQLPHP - MYSQL查詢結果

我得到這個錯誤新:mysql_num_rows()預計參數1是資源,鑑於布爾

變量$deets是一樣的分貝爲插入..

我的代碼:

<?php 
$deets = $_POST['deets']; 
$deets = preg_replace('#[^0-9/]#i', '', $deets); 

include ("connect.php"); 


$events = ''; 
$query = mysql_query('SELECT H.hora FROM horario H WHERE NOT EXISTS(SELECT EV.idhora  FROM eventos EV WHERE H.hora = EV.idhora AND data= "'.$deets.'")'); 
$num_rows = mysql_num_rows($query); 
if($num_rows > 0) { 
$events .= '<div id="eventsControl"><button class="btn2" style=" float:right;" onMouseDown="overlay()"><b>Fechar</b></button><p><b> ' . $deets . '</b></p></div> <br />'; 

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

    $desc = $row['hora']; 
    $form = "<form style='float:right;' action='marcarhora.php' method='post'><input type='hidden' name='gethora' value='$desc '><input type='hidden' name='getdate' value='$deets '><input type='hidden' name='getuser' value='{$_SESSION["email"]}'>"; 



    $desc1 = "<input type='submit' class='btn1' name='insert' value='Marcar Hora'/></form>"; 
    $events .= '<div id="eventsBody">' . $desc . '&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;'.$form .$desc1. '<br /><hr><br /></div>'; 
} 
    } 

    echo $events; 
    ?> 
+0

也不要使用mysql_ *函數,因爲它們在較新版本的php中被棄用。改爲使用mysqli_ *函數讀入。你的mysql_query調用失敗,因此它返回false並給出你的錯誤。 – flauntster

+0

我認爲錯誤查詢後簡單添加或死亡(mysql_error())後查詢。 –

回答

2
<?php 
$deets = $_POST['deets']; 
$deets = preg_replace('#[^0-9/]#i', '', $deets); 

include("connect.php"); 


$events = ''; 

// use $res not $query, as mysql_query returns a result not a query 
//$query = mysql_query('SELECT H.hora FROM horario H WHERE NOT EXISTS(SELECT EV.idhora  FROM eventos EV WHERE H.hora = EV.idhora AND data= "' . $deets . '")'); 
// you are using wrong " quotes to encapsulate value, given that $deets should be a number (as you have filtered it above, then you don't even need need quotes around it 
$sql = "SELECT H.hora FROM horario H WHERE NOT EXISTS(SELECT EV.idhora  FROM eventos EV WHERE H.hora = EV.idhora AND data= '{$deets}')"; 

// always check for errors, in case query failed 
$res = mysql_query($sql) or die(mysql_error()); 

$num_rows = mysql_num_rows($res); 

if ($num_rows > 0) { 
    $events .= '<div id="eventsControl"><button class="btn2" style=" float:right;" onMouseDown="overlay()"><b>Fechar</b></button><p><b> ' . $deets . '</b></p></div> <br />'; 

    // mysql_fetch_array is not necessary unless you need both numeric and textual indexing 
// while ($row = mysql_fetch_array($query)) { 
    while ($row = mysql_fetch_assoc($res)) { 

     $desc = $row['hora']; 
     $form = "<form style='float:right;' action='marcarhora.php' method='post'><input type='hidden' name='gethora' value='$desc '><input type='hidden' name='getdate' value='$deets '><input type='hidden' name='getuser' value='{$_SESSION["email"]}'>"; 


     $desc1 = "<input type='submit' class='btn1' name='insert' value='Marcar Hora'/></form>"; 
     $events .= '<div id="eventsBody">' . $desc . '&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;' . $form . $desc1 . '<br /><hr><br /></div>'; 
    } 
} 

echo $events; 
?> 
+0

讓我檢查.. sec plz – Frank021

+0

woot man ..作品像魅力!!非常感謝 !!!只需要將data ='{$ deets}'更改爲data ='「。$ deets。」'' – Frank021

1

一旦嘗試運行查詢在phpmyadmin.Is它給你的結果呢?您的查詢可能存在一個簡單的錯誤。如果結果集從查詢返回錯誤值Read here也嘗試切換到任何MySQLiPDO堂妹在MySQL PHP5.5不支持此錯誤是造成+ http://www.php.net/manual/en/migration55.deprecated.php

也曾經嘗試:

// this may help you knowing your error more precisely 
$resultSet = mysql_query('your query') or die('SQL Error :: '.mysql_error()); 
+0

是啊它的顯示正常,查詢在phpmyadmin工作正常,只是不工作在我的PHP文件.. – Frank021

+0

@ Frank021然後我更喜歡一次檢查你的數據庫連接。 – Robz

+0

其良好..我有很多的選擇...只有這一個沒有工作 – Frank021