MySQL dahil $ değişken

5 Cevap php

Burada MySQL ifadede bir PHP değişkeni dahil benim başarısız girişimleri vardır. Yazdırılan sonuçlarında 1 sonuç ile değişken değiştirilmesi. Herhangi bir yardım takdir edilecektir.

 $query = "
 SELECT name FROM teams
 WHERE id = '$shooterID'";

$shooters = mysql_query($query)
 or die(mysql_error());

$i = 0;
while($shooter = mysql_fetch_array( $shooters )) {
 echo $shooter[$i];
 $i++;
}

$shooters = mysql_query("
 SELECT name FROM teams
 WHERE id = '$shooterID'")
 or die(mysql_error());

$i = 0;
while($shooter = mysql_fetch_array( $shooters )) {
 echo $shooter[$i];
 $i++;
}

Teşekkürler


Burada yöntemlerini kullanmak için çalışılıyor tam sorunu çözülmüş değil (gerçi tekrar teşekkürler). Başka doğrudan sorgudan geliyor gibi burada daha fazla bağlam ile birlikte benim revize çabaları (I verileri sterilize gerekmez vardır.

$shooters = mysql_query("
 SELECT * FROM events JOIN teams
 on events.shooter = teams.id
 ") or die(mysql_error());

$i = 0;
while($results = mysql_fetch_array( $shooters )) {
    $shooterIDs[$i] = $results[0];
    $i++;
}

//var_dump($shooterIDs); == array(1) { [0]=>  string(1) "1" } 

$query = "
 SELECT name FROM teams
 WHERE id = '".$shooterID[0]."'";

$shooters = mysql_query($query)
 or die(mysql_error());

while($shooter = mysql_fetch_array( $shooters )) {
 echo $shooter[0];
}

Turns out my last attempt was missing a 's' in the variable namee $shooterIDs[0]. Stupid error. There were probably others as well that have been already solved with all of your help. Teşekkürler!

5 Cevap

Sorgu senin sorunun değil, çıkış:

Bu wrong,

$i = 0;
while($shooter = mysql_fetch_array( $shooters )) {
 echo $shooter[$i];
 $i++;
}

Bu correct,

while($shooter = mysql_fetch_array( $shooters )) {
 echo $shooter[0];
}

Also

Sadece böyle değişken eklemek istiyorsanız emin düzgün giriş hijyen olduğundan emin olun. Örneğin:

$shooterID = (int)$_GET['shooter_id'];

Bunun sayısını zorlar ya olacak bir 0, bir sayı veya bir 1 onlar shooter_id[]=somthing geçmek, ama asla eğer bir SQL enjeksiyonu değilse dize.

sorgu içinde $ shooterID etrafında tek tırnak koymayın.

muhtemelen de böyle bir şey isteyeceksiniz:

while($shooter = mysql_fetch_array( $shooters )) {
 echo $shooter[0];
 $i++;
}

sonuçlarını yazdırmak için.

Böyle bir şey (yorum netlik eklenmiş) deneyin:

// Create the query, assuming $shooterID is an integer
$query = "SELECT name FROM teams WHERE id = '{$shooterID}'";

// Execute query
$shooters = mysql_query($query);

// Check result
if (!$shooters) { die(mysql_error()); }

// Iterate through rows
while ($shooter = mysql_fetch_array($shooters)) {
  // To display the entire $shooter array
  print_r($shooter);

  // To select the first item in $shooter array (no matter what it is)
  echo $shooter[0];

  // To specifically select the name field in $shooter array
  echo $shooter['name'];

  // To iterate over the $shooter array and display all fields
  // This will only be the name, unless you change the query to SELECT * FROM,
  // in which case this will return all fields in the table
  foreach ($shooter as $field) {
    echo $field;
  }
}

Eğer denedim:

 $query = "SELECT name FROM teams WHERE id = '" . $shooterID . "'";

Ayrıca,I don't see you defining $shooterID anywhere make sure you define it.
I.E.

$shooterID = 0;

Ayrıca,

$i = 0;  
while($shooter = mysql_fetch_array( $shooters )) {
     echo $shooter[$i];
     $i++;
}

olmalıdır

while($shooter = mysql_fetch_array( $shooters )) {
     echo $shooter[0];
}

veya

while($shooter = mysql_fetch_array( $shooters )) {
     echo $shooter['name'];
}

veya

while($shooter = mysql_fetch_object( $shooters )) {
     echo $shooter->name;
}

Ayrıca, muhtemelen çıktı bazı ayrılmasını istiyorum:

while ($shooter = mysql_fetch_array( $shooters )) 
{
   echo $shooter[0], "\n";   //  or '<br>' if outputting to html
}