Page 1 of 1

Unable to Print Data in Excel

Posted: Thu Dec 22, 2011 8:30 pm
by nielsenN
I wanted to have a button to enable the user to generate an excel file from searched result, but in the excel.php I got this error.

<b>Warning</b>: mysqli_connect() [<a href='function.mysqli-connect'>function.mysqli-connect</a>]: (28000/1045): Access denied for user 'root'@'localhost' (using password: YES) in <b>C:\xampp\htdocs\projectli\excel.php</b> on line <b>9</b><br />
Unable to select database

This is the codes for excel.php

Code: Select all

<?php

header("Content-Disposition: attachment; filename=Rekod.xls");


include("connect.php");


mysqli_connect("localhost","root"," ", "project");
@mysqli_select_db($dbcon) or die("Unable to select database");
$queryt = "SELECT * FROM schools";
$export = mysqli_query($query);
$count = mysqli_num_fields($export);

for ($i = 0; $i < $count; $i++) {
$header .= mysql_field_name($export, $i)."\t";
}
while($row = mysqli_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r", "", $data);
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
print "$header\n$data";

?> 

This is the connect.php

Code: Select all

<?php

$dbcon=mysqli_connect("localhost","root","", "project")or trigger_error(mysqli_error(),E_USER_ERROR);


?>

This is the searchschoolsthis.php which have a button to display the searched data in tables and a button to generate an excel file of the searched data.

Code: Select all

      
    <form method="post" action="searchschoolsthis.php">
    <input type="hidden" name="submitted" value="true" />
    
    <label><span class="style2">Pilih Kategori:</span>
    <span class="style1">
    <select name="category">
      <option value="negeri">NEGERI</option>
      <option value="daerah">DAERAH</option>
      <option value="kod_sekolah">KOD SEKOLAH</option>
    </select>
     </span></label>
     
     <span class="style1">
     <span class="style2">Taip Kriteria
     <label>:</label>
     </span>
     <label>
     <input type="text" name="criteria" onKeyUp="this.value = this.value.toUpperCase();"/>
     </label>
     <input type="submit" value="Cari" />
               	 </form>   
                    
                                               
   				<form action="excel.php" method="post" name="cetak" target="_blank" id="cetak">
      	        <input type="submit" name="cetak" id="cetak" value="Cetak" />
                
                 </form>
        
     </span>
     
        
       <table width='100%' color='black' id='header'>
       <thead>
        <tr>
          <td width="40%" scope="col"><div align="center"><span class="style2">Kod Sekolah</span></div></td> 
          <td width="40%" scope="col"><div align="center"><span class="style2">Nama Sekolah</span></div></td> 
          <td width="40%" scope="col"><div align="center"><span class="style2">PTJ</span></div></td> 
          <td width="40%" scope="col"><div align="center"><span class="style2">Server</span></div></td> 
          <td width="40%" scope="col"><div align="center"><span class="style2">PC</span></div></td> 
          <td width="40%" scope="col"><div align="center"><span class="style2">NB</span></div></td> 
          <td width="40%" scope="col"><div align="center"><span class="style2">Mono Laser</span></div></td> 
          <td width="40%" scope="col"><div align="center"><span class="style2">Color Laser</span></div></td> 
          <td width="40%" scope="col"><div align="center"><span class="style2">Dot Matrix</span></div></td> 
          <td width="40%" scope="col"><div align="center"><span class="style2">LCD</span></div></td> 
          <td width="40%" scope="col"><div align="center"><span class="style2">Set LAN</span></div></td> 
          <td width="40%" scope="col"><div align="center"><span class="style2">Jumlah Kos</span></div></td> 
          <td width="40%" scope="col"><div align="center"><span class="style2">Dibayar</span></div></td> 
          <td width="50%" scope="col"><div align="center"><span class="style2">Tanggungan</span></div></td> 
        </tr>
      </thead>
        </table>
         
        
    <?php
	
	if(isset($_POST['submitted'])){
	
	include('connect.php');
	$category=$_POST['category'];
	$criteria=$_POST['criteria'];
	$query="SELECT * FROM schools WHERE $category = '$criteria'";
	$result=mysqli_query($dbcon,$query) or die('error getting data');
			
		
	echo "<table width='120%' border='2' color='black' id='header'>";
		
	
	while ($row = mysqli_fetch_array($result,MYSQL_ASSOC)){
	
		
	echo"<tr><td>";
	
	echo $row['kod_sekolah'];
	
	echo"</td><td>";	
		
	echo $row['nama_sekolah'];
	echo"</td><td>";
	
	echo $row['ptj'];
	echo"</td><td>";
	
	echo $row['server'];
	echo"</td><td>";
	
	echo $row['pc'];
	echo"</td><td>";
	
	echo $row['nb'];
	echo"</td><td>";
	
	echo $row['mono_laser'];
	echo"</td><td>";
	
	echo $row['color_laser'];
	echo"</td><td>";
	
	echo $row['dot_matrix'];
	echo"</td><td>";
	
	echo $row['lcd'];
	echo"</td><td>";
	
	echo $row['set_lan'];
	echo"</td><td>";
	
	echo $row['jumlah_kos'];
	echo"</td><td>";
	
	echo $row['dibayar'];
	echo"</td><td>";
	
	echo $row['tanggungan'];
	//echo"</td><td  style='text-align:right'>";
			
	echo"</td></tr>";
		
	}
			
	echo "</table>";
	
	}
	
	?>
        	       
   
Please help me. I really need this to work. :(

Re: Unable to Print Data in Excel

Posted: Thu Dec 22, 2011 8:59 pm
by phpHappy
mysqli_connect("localhost","root"," ",

mysqli_connect("localhost","root"," database password goes in here ",

Re: Unable to Print Data in Excel

Posted: Thu Dec 22, 2011 11:27 pm
by Gopesh
HI check this link http://ask.metafilter.com/31982/Upgrade ... cess-to-db. It may help u to solve the problem

Re: Unable to Print Data in Excel

Posted: Mon Jan 02, 2012 6:39 pm
by nielsenN
Now I got this error "Unable to select database". Help me please:(

Re: Unable to Print Data in Excel

Posted: Mon Jan 02, 2012 7:12 pm
by twinedev
Well you are telling it to select a database to use that is named in $dbconn, yet in the code you give, this isn't defined. What is in $dbcon?

-Greg

Re: Unable to Print Data in Excel

Posted: Mon Jan 02, 2012 7:27 pm
by nielsenN
What do you mean? How do I define $dbcon?

Re: Unable to Print Data in Excel

Posted: Mon Jan 02, 2012 7:50 pm
by twinedev
I just double checked the syntax (http://php.net/mysqli_select_db) as I don't use mysqli, and it isn't that $dbcon doesn't contain the database name, this should be contain the result of the connection. But you also need to pass it a second parameter that is the name of the database:

Code: Select all

$dbcon = mysqli_connect("localhost","root"," ", "project");
@mysqli_select_db($dbcon,'DATABASE_NAME') or die("Unable to select database");
The one thing I wonder though, what is in connect.php? Usually that is where people have the actual connection, so they don't have to change it in more that one place when you change login/server info, yet you are doing the connection after it?

-Greg