Help Needed.Errors in PHP Codes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nielsenN
Forum Newbie
Posts: 17
Joined: Wed Nov 16, 2011 6:46 pm

Help Needed.Errors in PHP Codes

Post by nielsenN »

I tried to print a pdf file using fpdf but I keep on getting 4 errors in my php. I've been trying to figure this out for weeks already.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.

Code: Select all

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();

User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Help Needed.Errors in PHP Codes

Post by twinedev »

Well, the other errors cascade down from the first error, the misuse of mysqli_query(). I admit, I don't use it, I either use mysql_query or pdo. So two second look at the "book" (http://php.net/mysqli_query) shows the correct way to use it, passing the database connection link first, THEN the actual query. (and the use on the line below it, while passing the query in the correct position, is not passing it the database connection link).

-Greg
nielsenN
Forum Newbie
Posts: 17
Joined: Wed Nov 16, 2011 6:46 pm

Re: Help Needed.Errors in PHP Codes

Post by nielsenN »

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.

Code: Select all

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

Code: Select all

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();
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Help Needed.Errors in PHP Codes

Post by twinedev »

die() only takes one parameter, and you are passing it two, which is why it chokes on the comma which separates the two parameters.

You were probably meaning to use trigger_error() http://www.php.net/trigger_error

-Greg

PS. You are still going to have an issue on the next line, as you are calling mysqli_query again, but this time passing it the Result Object of the first query.
nielsenN
Forum Newbie
Posts: 17
Joined: Wed Nov 16, 2011 6:46 pm

Re: Help Needed.Errors in PHP Codes

Post by nielsenN »

After changing die to trigger_error now I got these error. I'm confused :(

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\projectli\cetakpdf.php on line 131

Fatal error: in C:\xampp\htdocs\projectli\cetakpdf.php on line 131
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Help Needed.Errors in PHP Codes

Post by twinedev »

You need to give it a paremeter, and you didn't give it one.

-Greg
nielsenN
Forum Newbie
Posts: 17
Joined: Wed Nov 16, 2011 6:46 pm

Re: Help Needed.Errors in PHP Codes

Post by nielsenN »

What do you mean by giving a parameter? Can you give me an example? Sorry for asking a simple question. I'm quite new to programming n php :(
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Help Needed.Errors in PHP Codes

Post by twinedev »

Parameters are the values you pass to a function (within the parenthesis after the function name, separated by commas). When you are using a function you are not used to and/or you get an error on one, like the ones you keep posting here, always go to the documentation for it, (ie. http://php.net/mysqli_error just type in php.net/function_name for just about any function to get to the docs for it), they will give you a list of parameters it expects (and/or are optional), as well as what you can expect to be returned by the function. Also for most of them, they have good examples to show their usage.

Now, one thing to note, the mysqli functions can be used as a class or directly, so they give both examples of Object Oriented Use and Procedural style. Until you know what classes are and know you are using them, you want the Procedural style. (DateTime functions are another common used ones that have both methods available), most other functions come up as one or another, so only one style to deal with. ;-)

You should really get used to using this to check things, then come here if you need more explanation or help. (and if you want to be a real geek, in your spare time, click onto functions you have never used before, over the years while learning PHP there were many that I learned to use just from doing that).

-Greg
Post Reply