2013-03-27 87 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