2014-03-26 198 views
0

我已經修改了我的代碼,並希望這是多一點就應該是這樣,謝謝大家的幫助,這是非常讚賞SQLI準備好的聲明

<?php 
$mysqli = new mysqli("localhost", "root", "", "project"); 
if (mysqli_connect_errno()) { 
printf("Connect failed: %s\n", mysqli_connect_error()); 
exit(); 
} 
if ($stmt = $mysqli->prepare("SELECT * FROM users WHERE ? BETWEEN ? AND ?")){ 



$stmt->bind_param('iii',$weight, $weight1 ,$weight2); 

$weight = "weight"; 
$weight1 = "weight + 1"; 
$weight2 = "weight - 1"; 

$stmt->execute(); 

echo "{$weight}"; 
echo "{$weight1}"; 
echo "{$weight2}"; 

$stmt->close(); 
} 
else { 

printf("Prepared Statement Error: %s\n", $mysqli->error); 
} 
?> 
+1

您正在使用2'?'在您的準備聲明中。這意味着你必須在'bind_param()'中使用兩個變量' – Tabby

+1

注意:這行if($ stmt = $ mysqli-> prepare(「SELECT ....'缺少關閉')' –

+0

@弗雷德的筆記,請只在這裏提供代碼,你已經嘗試過 - 如果它會導致一個解析錯誤,需要先修復':)'。 – halfer

回答

0

難道你需要的變量分配在你綁定之前?像下面

<?php 
$mysqli = new mysqli("localhost", "root", "", "project"); 

if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

$weight = ???; 
$betweenOne = ???; 
$betweenTwo = ???; 

if ($stmt = $mysqli->prepare("SELECT * FROM users WHERE ? BETWEEN ? AND ?")) { 

    $stmt->bind_param('iii', $weight, $betweenOne, $betweenTwo); 

    $stmt->execute(); 

    echo $weight; 

    $stmt->close(); 
} else { 
    printf("Prepared Statement Error: %s\n", $mysqli->error); 
} 

注意到,你是不是傳遞值之間要麼使我在

固定收盤)感謝評議:)

看看實例添加他們在文檔中http://php.net/manual/en/mysqli.prepare.php

+0

重量包含數字,將'ss'更改爲'ii' – Tabby

+1

旁註:此行if($ stmt = $ mysqli-> prepare(「SELECT ....'缺少關閉''' ---和OP一樣。注意SO的語法突出顯示? –

+0

'echo $ weight';''刪除'''' –

2

首先,請自己幫忙,不要學習mysqli預處理語句,而應考慮PDO預處理語句。

接下來,無論您選擇哪種方法,您都首先關心錯誤報告。

接下來,無論您選擇哪種方法,您都必須計算佔位符。他們來匹配綁定變量的數量。在你的代碼中他們不是。

<?php 
ini_set('display_errors',1); 
error_reporting(E_ALL); 

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 
$mysqli = new mysqli("localhost", "root", "", "project"); 
$stmt = $mysqli->prepare("SELECT * FROM users WHERE weight BETWEEN ? AND ?"); 

$stmt->bind_param('ii', $from, $to); 
$from = -1; 
$to = 1; 
$stmt->execute(); 
$res = $stmt->get_result(); 
var_dump($res->fetch_assoc()); 

這樣的事情。但是你需要首先用你的邏輯來表達你的想法。