2017-07-31 7 views
0

データベーステーブルの列を配列に格納したいので、配列と比較することができます。テーブルから1つの列のみを選択しています。すべての行を格納しています。私はそれを行うことができましたが、2次元配列で2d配列を私の1次元配列と比較するとエラーが発生します。ですから、1d配列にデータを変換するには手伝ってください2dではなく1dの配列にデータベースからデータを格納する

array( 
    [0]=> array(
     ["service_id"]=> "1" 
    ) 
    [1]=> array(
     ["service_id"]=> "7" 
    ) 
    [2]=> array( 
     ["service_id"]=> "11" 
    ) 
) 

そして

array(
    [0]=>"0011" 
    [1]=>"0012" 
) 
+2

[リトルボビー](http://bobby-tables.com/)と言い** [スクリプトがSQLインジェクション攻撃のリスクがある](HTTPを:)だ

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $sql = $conn->query("your query"); $sql->fetch(PDO::FETCH_ASSOC); 

.com/questions/60174/how-can-i-prevent-sql-injection-in-php)**。 [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)の[Prepared Statements](http://en.wikipedia.org/wiki/Prepared_statement)について学んでください。 ** [文字列をエスケープする](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)**でも安全ではありません! – GrumpyCrouton

+0

あなたの質問は何ですか? –

+0

@GrumpyCroutonは、長い時間内に小さなブービーを見たことがありません –

答えて

3

をあなたはmysqli_fetch_assocを使用しているので、あなたが連想列を取得する必要があります。次のように

$array = array(); 
$serviceId = "SELECT service_id FROM servicesorders WHERE order_id = '$orderId'"; 
$resultId = mysqli_query($connection, $serviceId) or die(mysqli_error($connection)); 
while ($serviceIdfinal = mysqli_fetch_assoc($resultId)) { 
$array[] = $serviceIdfinal; //the array I used to store the data 
} 
var_dump($array); 

$isCheckedstring = $_POST['show']; 
$isCheckedarray = str_split($isCheckedstring, 4); // the array I want to compare the stored data with 
var_dump($isCheckedarray); 

2つの配列ののvar_dumpです。データベースが配列を返すときあなたは

while ($serviceIdfinal = mysqli_fetch_assoc($resultId)) { 
    $array[] = $serviceIdfinal['service_id']; //the array I used to store the data 
} 
0

while ($serviceIdfinal = mysqli_fetch_assoc($resultId)) { 
    $array[] = $serviceIdfinal; //the array I used to store the data 
} 

を変更する必要が

、それは連想配列であるそれぞれの行の配列を、返しています。各行に1つの要素が含まれていても問題はありません。配列の配列です。

$newArray = []; 
foreach($array as $row) { 
    // This line here is where you'll do any needed string modifications 
    // such as padding with leading zeroes, etc. 
    $newArray[] = $row["service_id"]; 
} 
var_dump($newArray) 

これはあなたの正味する必要があります:

array( 
    [0]=> "1", 
    [1]=> "7", 
    [2]=> "11" 
) 

EDIT:またはあなたがそれを行う必要があります

あなたはforeachループで別のフォームから$arrayを変換する必要があるとしていますwhileループでは、ミッチェルがhis answerで指摘したように、フェッチしているときにすでに使用しています。

0

mysqliの代わりにPDOを使用できます。 // stackoverflowの:それは

+0

PDOは依然として行の配列を返し、各行は配列です。それは問題を解決しません。 – RedDwarfian

関連する問題