2013-03-27 92 views
-3

我想將MySQL以下的查詢改爲MySQLi(準備好的語句),但我不知道該怎麼做,因爲它有多行可供選擇。任何人都可以以正確的方式指向我。選擇多行mysqli

$check_added_files = mysql_query("select * from `vpb_uploads` where `username` = '".mysql_real_escape_string($username)."' and `firstname` = '' and `image_one` != '' and `image_two` != '' and `image_three` != '' and `image_four` != '' and `image_five` != ''"); 
     if(mysql_num_rows($check_added_files) == 1) 
     { 
      echo 'up_to_five_already'; 
     } 
+0

我不確定確切的問題是什麼,主要區別在於您使用佔位符(s)作爲變量並且事後綁定值。你檢查過手冊嗎? – jeroen 2013-03-27 16:01:42

+0

其他人嗎?我真的需要幫助 – Magna 2013-03-27 17:10:41

回答

-1

它很簡單,我不知道爲什麼每次我提問時都會投下一票。

$mysqli = new mysqli("localhost", "root", "", "database"); 
    if ($mysqli->connect_errno) { 
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
    } 

$username = $_SERVER['REMOTE_ADDR']; 
    $stmt = $mysqli->prepare("select * from `vpb_uploads` where `username` = ? and `firstname` = '' and `image_one` != '' and `image_two` != '' and `image_three` != '' and `image_four` != '' and `image_five` != ''"); 
    $stmt->bind_param('s', $username); 
    $stmt->execute(); 
    $stmt->store_result(); 

    if ($stmt->num_rows == 1) { 

    echo 'up_to_five_already'; 
    } 
2

正確的方法是將其更改爲PDO

$sql = "select * from vpb_uploads where username=? and firstname='' 
     and image_one != '' and image_two != '' and image_three != '' 
     and image_four != '' and image_five != ''"; 
$stm = $pdo->prepare($sql); 
$stm->execute(array($username)); 
$files = $stm->fetchAll(); 
+0

我複製了你的代碼,但它不起作用。 – Magna 2013-03-27 17:12:58