在MYSQL查询之前使用PHP变量 [英] Using a PHP variable before a MYSQL query
问题描述
!!请向下滚动到编辑4"!
这真让我发疯了!
我有以下代码,但是它不起作用:
I have the following piece of code, and it doesn't work:
$importsectie = join("','",$importsectie);
$query1 = "SELECT * FROM smoelenboek WHERE sectie1 IN ('$importsectie') OR sectie2 IN ('$importsectie') OR sectie3 IN ('$importsectie') OR sectie4 IN ('$importsectie') OR sectie5 IN ('$importsectie') AND actief='ja' ORDER BY achternaam ASC";
$result1 = mysql_query($query1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
here the stuff I want to do with the results
}
当我echo $importsectie;
时,我得到Tekenen','Wiskunde
很好.
当我echo $query1;
时,我得到了我在PhpMyAdmin中尝试过的SELECT * FROM smoelenboek WHERE sectie1 IN ('Tekenen','Wiskunde') OR sectie2 IN ('Tekenen','Wiskunde') OR sectie3 IN ('Tekenen','Wiskunde') OR sectie4 IN ('Tekenen','Wiskunde') OR sectie5 IN ('Tekenen','Wiskunde') AND actief='ja' ORDER BY achternaam ASC
,它给了我想要的记录,所以也很好.
When I echo $query1;
, I get SELECT * FROM smoelenboek WHERE sectie1 IN ('Tekenen','Wiskunde') OR sectie2 IN ('Tekenen','Wiskunde') OR sectie3 IN ('Tekenen','Wiskunde') OR sectie4 IN ('Tekenen','Wiskunde') OR sectie5 IN ('Tekenen','Wiskunde') AND actief='ja' ORDER BY achternaam ASC
which I tried in PhpMyAdmin, it gave me the records I wanted, so this is fine as well.
我怀疑它与$importsectie
有关,因此我尝试了以下操作:
I suspected it had something to do with $importsectie
so I tried it without:
$importsectie = join("','",$importsectie);
$query1 = "SELECT * FROM smoelenboek WHERE sectie1 IN ('$importsectie') OR sectie2 IN ('$importsectie') OR sectie3 IN ('$importsectie') OR sectie4 IN ('$importsectie') OR sectie5 IN ('$importsectie') AND actief='ja' ORDER BY achternaam ASC";
$query2 = "SELECT * FROM smoelenboek WHERE sectie1 IN ('Tekenen','Wiskunde') OR sectie2 IN ('Tekenen','Wiskunde') OR sectie3 IN ('Tekenen','Wiskunde') OR sectie4 IN ('Tekenen','Wiskunde') OR sectie5 IN ('Tekenen','Wiskunde') AND actief='ja' ORDER BY achternaam ASC";
$result1 = mysql_query($query2) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
here the stuff I want to do with the results
}
echo $query1;
和echo $query2;
现在给出的结果完全相同(与上面的结果相同),但是脚本仍然无法正常工作.
echo $query1;
and echo $query2;
give exactly the same result now (same result as above), but still the script doesn't work.
现在这是很奇怪的事情:当我不小心在$importsectie
前面加上一些斜线时,它突然起作用了!
Now here is the weird thing: when I accidentally put some slashes before $importsectie
, it suddenly worked!!
//$importsectie = join("','",$importsectie);
$query1 = "SELECT * FROM smoelenboek WHERE sectie1 IN ('$importsectie') OR sectie2 IN ('$importsectie') OR sectie3 IN ('$importsectie') OR sectie4 IN ('$importsectie') OR sectie5 IN ('$importsectie') AND actief='ja' ORDER BY achternaam ASC";
$query2 = "SELECT * FROM smoelenboek WHERE sectie1 IN ('Tekenen','Wiskunde') OR sectie2 IN ('Tekenen','Wiskunde') OR sectie3 IN ('Tekenen','Wiskunde') OR sectie4 IN ('Tekenen','Wiskunde') OR sectie5 IN ('Tekenen','Wiskunde') AND actief='ja' ORDER BY achternaam ASC";
$result1 = mysql_query($query2) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
here the stuff I want to do with the results
}
为什么?!?我真的很想使用$importsectie
中的信息.
我要使用的东西是使用FPDF制成pdf文件.
$importsectie
在代码的其他任何地方都没有使用.
Why?!? I really want to use the information in $importsectie
.
The stuff I want to do with it is make a pdf file with FPDF.
$importsectie
isn't used anywhere else in the code.
我尝试了另一种方式:
$importsectie = $importsectie[0];
$query1 = "SELECT * FROM smoelenboek WHERE sectie1='$importsectie' OR sectie2='$importsectie' OR sectie3='$importsectie' OR sectie4='$importsectie' OR sectie5='$importsectie' AND actief='ja' ORDER BY achternaam ASC";
$query2 = "SELECT * FROM smoelenboek WHERE sectie1='Tekenen' OR sectie2='Tekenen' OR sectie3='Tekenen' OR sectie4='Tekenen' OR sectie5='Tekenen' AND actief='ja' ORDER BY achternaam ASC";
$result1 = mysql_query($query1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
here the stuff I want to do with the results
}
echo $query1;
和echo $query2;
都再次给出相同的结果.
现在使用$query2
可以正常工作(因此在$importsectie
之前没有延迟).
使用$query1
将开始加载pdf,加载的图片将填满,然后显示无法加载PDF文件".
Both echo $query1;
and echo $query2;
give the same results again.
Now using $query2
works fine (so no slahes before $importsectie
).
Using $query1
will start loading the pdf, the loading picture fills up and then it says "The PDF-file could not be loaded".
$aantalsecties = count($importsectie);
for($i=0;$i<=$aantalsecties-1;$i++){
$importsectiedeel = $importsectie[$i];
$query1 = "SELECT * FROM smoelenboek WHERE sectie1='$importsectiedeel' OR sectie2='$importsectiedeel' OR sectie3='$importsectiedeel' OR sectie4='$importsectiedeel' OR sectie5='$importsectiedeel' AND actief='ja' ORDER BY achternaam ASC";
$query2 = "SELECT * FROM bonasmoelenboek WHERE sectie1='Tekenen' OR sectie2='Tekenen' OR sectie3='Tekenen' OR sectie4='Tekenen' OR sectie5='Tekenen' AND actief='ja' ORDER BY achternaam ASC";
$result1 = mysql_query($query1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
here the stuff I want to do with the results
}
}
echo $query1;
和echo $query2;
的结果均与以前相同.现在他们两个都将开始加载pdf,但是加载的图片没有填满,页面上显示正在加载...".当$query1
内置在if语句中时,我遇到了同样的问题.
Both echo $query1;
and echo $query2;
give the same results as before again. Now both of them will start loading the pdf, but the loading picture doesn't fill up, the page keeps "Loading...". I have experienced the same problem when $query1
was built inside an if-statement.
因此,我多次建议将结合准备好的语句的mysqli切换到mysqli.由于我仍然遇到相同的问题,因此我现在将发布整个代码.
So I switched to mysqli combined with prepared statements as suggested several times. Since I still have the same problem, I will now post the whole code.
首先,我有一个带有复选框的页面:
First I have a page with checkboxes:
<form action="pdfrender.php" method="POST">
<input type="checkbox" name="sectie[]" value="Tekenen">Tekenen<br>
<input type="checkbox" name="sectie[]" value="Wiskunde">Wiskunde<br>
<input type="submit" value="Sectiesmoelenboek">
</form>
pdfrender.php如下:
The pdfrender.php is the following:
<?php
//Informatie binnenhalen
$importsectie = $_POST['sectie'];
//Starten met het PDF-maak-script
require('../fpdf/fpdf.php');
class PDF extends FPDF
{
//Koptekst
function Header()
{
// Logo en een stukje leeg daaronder
$this->Image('burggravenlaanheader.jpg',0,0,210);
$this->Ln(20);
}
//Voettekst
function Footer()
{
//1.5 cm boven de paginarand
$this->SetY(-15);
$this->SetFont('Arial','I',8);
//Een hokje van paginabreed, 1 cm hoog, daarin pagina/totaal, geen rand, centreren
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
//PDF aanmaken, pagina's laten tellen, nieuwe pagina beginnen
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
//Teller nodig om foto's links en rechts op de pagina te plaatsen
$teller = 1;
//This is going to be dynamic later, with a for-loop scrolling through the parts of the array
$importsectiedeel = $importsectie[1];
//Informatie uit database halen
include '../dbconi.inc';
//Prepare en bind
$query1 ="SELECT * FROM smoelenboek WHERE sectie1=? OR sectie2=? OR sectie3=? OR sectie4=? OR sectie5=? AND actief='ja' ORDER BY achternaam ASC";
$stmt = $con->stmt_init();
if(!$stmt->prepare($query1)){
print "Failed to prepare statement\n";
}else{
$stmt->bind_param("sssss",$importsectiedeel,$importsectiedeel,$importsectiedeel,$importsectiedeel,$importsectiedeel);
$stmt->execute();
$result1 = $stmt->get_result();
while ($row1 = $result1->fetch_assoc()) {
//Alle parameters klaarmaken voor printen
$afkorting = $row1['afkorting'];
$voorletters = $row1['voorletters'];
$voornaam = $row1['voornaam'];
$tussenvoegsel = $row1['tussenvoegsel'];
$achternaam = $row1['achternaam'];
$sectie1 = $row1['sectie1'];
$sectie2 = $row1['sectie2'];
$sectie3 = $row1['sectie3'];
$sectie4 = $row1['sectie4'];
$taak1 = $row1['taak1'];
$taak1 = str_replace("?","",$taak1);
$taak2 = $row1['taak2'];
$taak2 = str_replace("?","",$taak2);
$taak3 = $row1['taak3'];
$taak3 = str_replace("?","",$taak3);
$taak4 = $row1['taak4'];
$taak4 = str_replace("?","",$taak4);
$taak5 = $row1['taak5'];
$taak5 = str_replace("?","",$taak5);
//Alle secties en taken onder elkaar zetten
$onderelkaar = $sectie1."\n".$sectie2."\n".$sectie3."\n".$sectie4."\n".$taak1."\n".$taak2."\n".$taak3."\n".$taak4."\n".$taak5;
//Zorgen dat minder dan 4 secties en minder dan 5 taken geen extra witregels oplevert
$onderelkaar = str_replace("\n\n","\n",$onderelkaar);
$onderelkaar = str_replace("\n\n","\n",$onderelkaar);
$onderelkaar = str_replace("\n\n","\n",$onderelkaar);
$onderelkaar = str_replace("\n\n","\n",$onderelkaar);
//Fotolink klaarzetten indien aanwezig, anders geenfoto.jpg klaarzetten
if (file_exists('fotos/'.$afkorting.'.jpg')){
$foto = 'fotos/'.$afkorting.'.jpg';
}else{
$foto = 'fotos/geenfoto.jpg';
}
//Afkorting hoofdletters voor het afdrukken (was klein voor het aanroepen van de jpg)
$afkorting = strtoupper($afkorting);
//Naam en afkorting koppelen
$titel = $voorletters.' ('.$voornaam.') '.$tussenvoegsel.' '.$achternaam.' ('.$afkorting.')';
//Extra spaties verwijderen bij mensen zonder tussenvoegsel
$titel = str_replace(" "," ",$titel);
//Als de teller oneven is, persoon links plaatsen
if($teller%2!=0){
$pdf->SetFont('Arial','',12);
$pdf->Cell(95,7,$titel,0,1);
$y = $pdf->GetY();
$x = $pdf->GetX();
$pdf->SetFont('Arial','',10);
$pdf->Cell(30,45,$pdf->Image($foto,$pdf->GetX(),$pdf->GetY(),30),1,0);
$pdf->MultiCell(65,5,$onderelkaar,0);
//Als de teller even is, de persoon rechts plaatsen
}else{
$pdf->SetY($y-7);
$pdf->SetX($x+95);
$pdf->SetFont('Arial','',12);
$pdf->Cell(95,7,$titel,0,0);
$pdf->SetY($y);
$pdf->SetX($x+95);
$pdf->Cell(30,45,$pdf->Image($foto,$pdf->GetX(),$pdf->GetY(),30),1,0);
$pdf->SetY($y);
$pdf->SetX($x+125);
$pdf->SetFont('Arial','',10);
$pdf->MultiCell(65,5,$onderelkaar,0);
$pdf->SetY($y+45);
$pdf->SetX(10);
$pdf->Cell(0,9,'',0,2);
}
//Teller laten oplopen
$teller = $teller+1;
//Afsluiten van de while-loop
}
//Afsluiten van de else
}
//Als de laatste persoon links geplaatst is, cursor naar beneden zetten (ipv ernaast)
if($teller%2==0){
$pdf->SetY($y+45);
$pdf->SetX(10);
$pdf->Cell(0,9,'',0,2);
}
//PDF daadwerkelijk aanmaken
$pdf->Output();
//Statement en databaseconnectie sluiten
$stmt->close();
$con->close();
?>
当我用'Tekenen'
替换五个?
并删除bind_param
时,将正确呈现pdf.当我使用上面的代码时,它不会.该页面保留正在加载...".我在每个变量的整个代码中都使用了var_dump
.在两种情况下(工作和不工作),我都得到相同的输出.谁能帮忙???
When I replace the five ?
's with 'Tekenen'
and remove the bind_param
, the pdf is correctly rendered. When I use the code above, it doesn't. The page keeps "Loading...". I used var_dump
throughout the code on every variable. I get the same output in both cases (working and not working). Can anyone help???
我将其范围缩小到以下行(在代码中出现两次):
I narrowed it down to the following line (appears two times in the code.):
$pdf->Cell(30,45,$pdf->Image($foto,$pdf->GetX(),$pdf->GetY(),30),1,0);
更具体地讲,这可行:
echo '<img src="fotos/geenfoto.jpg" width=100>';
这不是:
$pdf->Image("fotos/geenfoto.jpg",0,0,30);
如前所述,它可以在不使用de MySql(i)查询中使用变量的情况下正常工作,这可能是错误的吗?
As stated earlier, it does work without using variables in de MySql(i) query, what can be wrong?
推荐答案
除了Alex Andrei注释仍然有效外,我强烈建议使用准备好的语句,而不是普通的SQL查询. 这是一篇有关如何处理MySQL准备的语句和数组参数(和不进行处理)的相关文章: mysqli bind_param用于字符串数组
Apart from the Alex Andrei comment which is still valid I would strongly suggest to use a prepared statement instead of a plain SQL query. This is a related post about what to do (and not to do) with MySQL prepared statements and array parameters: mysqli bind_param for array of strings
预备语句为您提供了免费"的更可靠和安全的动态参数绑定,这是您想要的,并且还具有更好的性能,特别是对于重复查询,因为预备语句由数据库引擎(预)编译,并且效率更高.执行.
Prepared statements offer "for free" a more reliable and secure dynamic parameter binding which is what you want, and also better performances especially with recurring queries because prepared statements are (pre-)compiled by the database engine and much more efficient to execute.
这篇关于在MYSQL查询之前使用PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!