5 Replies - 9479 Views - Last Post: 22 December 2011 - 07:21 AM Rate Topic: -----

#1 nielsen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-July 11

Warning: mysqli_query() expects parameter 1 to be mysqli, string given

Posted 21 December 2011 - 08:14 PM

I tried to print a pdf file using fpdf but I keep on getting 4 errors in my php. I hope someone can help me to debug this. :(

The 4 errors are:

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\projectli\cetakpdf.php on line 133

Warning: mysqli_query() [function.mysqli-query]: Empty query in C:\xampp\htdocs\projectli\cetakpdf.php on line 134

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\projectli\cetakpdf.php on line 149

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\projectli\cetakpdf.php:133) in C:\xampp\htdocs\projectli\fpdf16\fpdf.php on line 1017
FPDF error: Some data has already been output, can't send PDF file

Below are the part of the code that I think the problem is from.
include("connect.php");

$category=$_POST['category'];
$criteria=$_POST['criteria'];

mysqli_select_db($dbcon,$select);

$result=mysqli_query("SELECT * FROM schools WHERE $category = '$criteria'",$dbcon);
$select = mysqli_query($dbcon,$result); 
//$jumlah = mysql_num_rows($result);
 {
$pdf=new PDF('L','mm','A3');

$pdf->SetMargins('20','20','20');
$pdf->AddPage();
$pdf->TableHeader();
$pdf->Ln();
$pdf->Ln();

$pdf->Row(array("KOD SEKOLAH","NAMA SEKOLAH","PTJ","SERVER","PC","NB","MONO LASER","COLOR LASER","DOT MATRIX","LCD","SET LAN","JUMLAH KOS","DIBAYAR","TANGGUNGAN"));
$pdf->SetFont('Arial','',8);
$count =0;
$jum=0;
while($row = mysqli_fetch_array($result,MYSQL_ASSOC)){
	$count++;
	$pdf->Row(array($count,$row['kod_sekolah'],$row['nama_sekolah'],$row['ptj'],$row['server'],$row['pc'],$row['nb'],$row['mono_laser'],$row['color_laser'],$row['dot_matrix'],$row['lcd'],$row['set_lan'],$row['jumlah_kos'],$row['dibayar'],$row['tanggungan']));
	}

$pdf->ln();
}
$pdf->Output();



Is This A Good Question/Topic? 0
  • +

Replies To: Warning: mysqli_query() expects parameter 1 to be mysqli, string given

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Warning: mysqli_query() expects parameter 1 to be mysqli, string given

Posted 21 December 2011 - 09:00 PM

Your query, whichever one is being executed, is failing. Verify that your SQL syntax is correct. Also use mysql_error() to help determine the error.

You get the headers already sent error because your previous error is being sent to the browser thus causing the headers to be sent. Headers can only be sent once per response. There must be no output whatsoever before starting a session or sending a header using the header() function.
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,986
  • Joined: 08-June 10

Re: Warning: mysqli_query() expects parameter 1 to be mysqli, string given

Posted 21 December 2011 - 09:36 PM

Take a closer look at these two lines:
$result=mysqli_query("SELECT * FROM schools WHERE $category = '$criteria'",$dbcon);
$select = mysqli_query($dbcon,$result); 


First you execute a query, and then you execute it again using the $dbcon as the SQL query and the result from the first query as the MySQLi object to execute it on.


You should only be executing once, and then checking the return value for errors.
$result = mysqli_query("...", $dbcon);
if ($result) {
    // The query was successful!
}
else {
    // The query failed. Show an error or something.
}



Also, since you are already using MySQLi, you should use prepared statements to avoid SQL Injection, which you current code is wide open to.
Was This Post Helpful? 0
  • +
  • -

#4 nielsen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-July 11

Re: Warning: mysqli_query() expects parameter 1 to be mysqli, string given

Posted 21 December 2011 - 11:50 PM

I did some changes in the codes and I got this error

Parse error: syntax error, unexpected ',' in C:\xampp\htdocs\projectli\cetakpdf.php on line 131

which is in this line of code. I think there is nothing wrong with this code, but the error seems to be from this line of code. I'm confused...:(
$result=mysqli_query($dbcon,"SELECT * FROM schools WHERE $category = '$criteria'")or die(mysqli_error(),E_USER_ERROR);


include("connect.php");

$category=$_POST['category'];
$criteria=$_POST['criteria'];

mysqli_select_db($dbcon,$select);

$result=mysqli_query($dbcon,"SELECT * FROM schools WHERE $category = '$criteria'")or die(mysqli_error(),E_USER_ERROR);
$select = mysqli_query($dbcon,$result); 

{
$pdf=new PDF('L','mm','A3');

$pdf->SetMargins('20','20','20');
$pdf->AddPage();
$pdf->TableHeader();
$pdf->Ln();
$pdf->Ln();

$pdf->Row(array("KOD SEKOLAH","NAMA SEKOLAH","PTJ","SERVER","PC","NB","MONO LASER","COLOR LASER","DOT MATRIX","LCD","SET LAN","JUMLAH KOS","DIBAYAR","TANGGUNGAN"));
$pdf->SetFont('Arial','',8);
$count =0;
$jum=0;
while($row = mysqli_fetch_array($result,MYSQL_ASSOC)){
	$count++;
	$pdf->Row(array($count,$row['kod_sekolah'],$row['nama_sekolah'],$row['ptj'],$row['server'],$row['pc'],$row['nb'],$row['mono_laser'],$row['color_laser'],$row['dot_matrix'],$row['lcd'],$row['set_lan'],$row['jumlah_kos'],$row['dibayar'],$row['tanggungan']));
	}

$pdf->ln();
}
$pdf->Output();

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,170
  • Joined: 08-June 10

Re: Warning: mysqli_query() expects parameter 1 to be mysqli, string given

Posted 22 December 2011 - 05:26 AM

die() does not expect 2 parameters. omit the second one, your script would fail anyways if that part is called.

This post has been edited by Dormilich: 22 December 2011 - 05:28 AM

Was This Post Helpful? 0
  • +
  • -

#6 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Warning: mysqli_query() expects parameter 1 to be mysqli, string given

Posted 22 December 2011 - 07:21 AM

You also have curly braces that are wrapped around some code but nothing proceeding them that would require them. I marked them with comments below:
include("connect.php");

$category=$_POST['category'];
$criteria=$_POST['criteria'];

mysqli_select_db($dbcon,$select);

$result=mysqli_query($dbcon,"SELECT * FROM schools WHERE $category = '$criteria'")or die(mysqli_error(),E_USER_ERROR);
$select = mysqli_query($dbcon,$result); 

{ // <--- This brace
$pdf=new PDF('L','mm','A3');

$pdf->SetMargins('20','20','20');
$pdf->AddPage();
$pdf->TableHeader();
$pdf->Ln();
$pdf->Ln();

$pdf->Row(array("KOD SEKOLAH","NAMA SEKOLAH","PTJ","SERVER","PC","NB","MONO LASER","COLOR LASER","DOT MATRIX","LCD","SET LAN","JUMLAH KOS","DIBAYAR","TANGGUNGAN"));
$pdf->SetFont('Arial','',8);
$count =0;
$jum=0;
while($row = mysqli_fetch_array($result,MYSQL_ASSOC)){
	$count++;
	$pdf->Row(array($count,$row['kod_sekolah'],$row['nama_sekolah'],$row['ptj'],$row['server'],$row['pc'],$row['nb'],$row['mono_laser'],$row['color_laser'],$row['dot_matrix'],$row['lcd'],$row['set_lan'],$row['jumlah_kos'],$row['dibayar'],$row['tanggungan']));
	}

$pdf->ln();
}  // <--- and this brace
$pdf->Output();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1