Why after exporting an excel it does have space.I want to start in 1 the data I already clear the spaces on my codes but nothing happen. Thanks for helping me
<?php
function db_connect(){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sti_lms";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $conn;
}
?>
<?php
/* File name : download_data_in_excel.php */
include('dbcon.php'); // include database connection file
$filename ="Books.xls";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment;filename='.$filename);
$sql = "SELECT * FROM book";
$conn = db_connect();
if($conn){
$result = $conn->query($sql);
echo "Accession\tCall_Num\tAuthor\tBook_Title\tCatalog\tbookcopies\tpublishername\tcopyrightyear\n"; // prints header line with field names
if ($result->num_rows > 0) {// output data of each row
while($row = $result->fetch_assoc()) {
echo $row['acc_id']."\t".$row['callnum']."\t".$row['author']."\t". $row['book_title']."\t".$row['catalog']."\t".$row['book_copies']."\t".$row['publisher_name']."\t".$row['copyright_year']."\n";// prints each record with five fields in a row
}
}
}
Related
I need to access a database remotely using PHP and display the fetched data in html. My code is as below
$servername = "192.168.56.1:3306";
$username = "root";
$password = "";
$dbname = "grh";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
error_log("Failed to connect to database!", 0);
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM ip_patient_logs";
$result = $conn->query($sql);
$rows = array();
if ($result->num_rows > 0) {
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
} else {
echo "0 results";
}
$conn->close();
I expected to be able to connect to the database and fetch the data , but it displays blank page. I am a beginner in PHP.
I want to fetch time column from mysql database and to compare with current time.
It should execute only if it matches with the current time.
Please tell me a MySql query or php code to do this.
I am new to php.
Try This:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$today = date("h:i a")
$sql = "SELECT * FROM Table_name WHERE SUBSTRING_INDEX(time, ' ', 2) = '".$today."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// execute you code
} else {
}
$conn->close();
?>
Try this: Updated Code
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$today = date("h:i a")
$sql = "SELECT * FROM Table_name WHERE time = $today";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// execute you code
} else {
}
$conn->close();
?>
After I add a column to my database, I want to retrieve it but not expected.
In PHP, I try reopening apache and mysql still not work.
Does anyone know how to resolve it? Thanks!
your question is not fully explanatory but with what I could try to understand you want to retrieve data or records from your database
you could try the code below and tweak it to work your way
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM databaseName";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data
while($row = $result->fetch_assoc()) {
print $row"<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I have two MySQL connections for the same database in the one .php file.
Second connection is base on the result of First one.
How can I combine two connection into one ? Please help & teach me how to modify it ?
First connection:
<?php
header('Content-Type: text/html; charset=utf-8');
$servername = "localhost";
$username = "abcabc";
$password = "12341234";
$dbname = "abc1234";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Logic data
$previous_page = ($_GET['crno'] - 1);
$next_page = ($_GET['crno'] + 1);
// select data
$sql = 'SELECT * FROM ComData WHERE com_no = '. $_GET['crno'];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$pageTitle = $row['com_eng_name'] . $row['com_chi_name'];
$com_no = $row['com_no'];
$br_no = $row['br_no'];
$com_eng_name = $row['com_eng_name'];
$com_chi_name = $row['com_chi_name'];
$com_type = $row['com_type'];
$date_of_incorp = $row['date_of_incorp'];
$active_status = $row['active_status'];
$date_commenced_dormancy = $row['date_commenced_dormancy'];
$remarks = $row['remarks'];
$date_of_dissolution = $row['date_of_dissolution'];
$register_charges = $row['register_charges'];
$name_history = $row['name_history'];
$phone = $row['phone'];
$email = $row['email'];
$address = $row['address'];
$website = $row['website'];
$background = $row['background'];
$update_time = $row['update_time'];
}
} else {
echo "No Results";
}
$conn->close();
?>
Second connection:
<?php
header('Content-Type: text/html; charset=utf-8');
$servername = "localhost";
$username = "abcabc";
$password = "12341234";
$dbname = "abc1234";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// select data for similar search
$string = "$com_eng_name";
$words = implode(' ', array_slice(explode(' ', $string ), 0, 2));
$sql = "SELECT * FROM ComData
WHERE com_eng_name REGEXP '$words'
ORDER BY com_no
DESC
LIMIT 20";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>CR No.</th><th>Company Name</th><th>Active Status</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["com_no"]."</td><td><a href='search.php?crno=".$row["com_no"]."' >".$row["com_eng_name"]." ".$row["com_chi_name"]."</a></td><td>".$row["active_status"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
If those 2 pieces of code both exist in the same physical file then as the variable holding your connection in both cases is $conn you can just throw away the second attempt at making a connection to the database.
So just remove of comment out this code from the piece of code you called Second Connection i.e. this bit
$servername = "localhost";
$username = "abcabc";
$password = "12341234";
$dbname = "abc1234";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Then remove this line
$conn->close();
from the piece of code you called First Connetion so you do not close the connection before the second piece of code runs.
However if in fact those 2 pieces of code are in seperate .php files. they have to stays the way they are.
It's not clear either if your two connection are in the same page or not, and if your two queries are execute sequentially or alternately.
Anyway, if the queries are on the same page or if you want put it in same page, sure you can use one only connection.
Start your script in this way:
header('Content-Type: text/html; charset=utf-8');
$servername = "localhost";
$username = "abcabc";
$password = "12341234";
$dbname = "abc1234";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
then, if you have to performs both queries, continue in this way:
// Logic data
$previous_page = ($_GET['crno'] - 1);
$next_page = ($_GET['crno'] + 1);
// select data
$sql = 'SELECT * FROM ComData WHERE com_no = '. $_GET['crno'];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
(...)
} else {
echo "No Results";
}
// select data for similar search
$string = "$com_eng_name";
$words = implode(' ', array_slice(explode(' ', $string ), 0, 2));
(...)
$conn->close();
Otherwise, if the queries are executed only on condition, after connection check continue in this way:
if( /* Your Condition Here */ )
{
// Logic data
$previous_page = ($_GET['crno'] - 1);
(...)
}
else
{
// select data for similar search
$string = "$com_eng_name";
(...)
}
If you want use connection in any different file, you can create a file dbconnect.php with this content:
$servername = "localhost";
$username = "abcabc";
$password = "12341234";
$dbname = "abc1234";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Then, in each file with db query, include it in this way:
header('Content-Type: text/html; charset=utf-8');
include( 'dbconnect.php' );
// Logic data
$previous_page = ($_GET['crno'] - 1);
(...)
header('Content-Type: text/html; charset=utf-8');
include( 'dbconnect.php' );
// select data for similar search
$string = "$com_eng_name";
(...)
etc...
Please note that, depending on 'dbconnect.php' file location, you can have to change include( 'dbconnect.php' ) with include( '/Full/Or/Relative/Path/dbconnect.php' ).
Here is a PHP code
php
<?php
$servername = "server";
$username = "user";
$password = "password";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT idbn, infobn, datebn FROM `update`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td><a> ";
echo $row["idbn"];
echo "</a></td><td><a> ";
echo $row["infobn"];
echo "</a></td><td><a>" . $row["datebn"]. "</a></td></tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
But the problem is, it is not retrieving the Bengali data from mysql
mysqli_query('SET CHARACTER SET utf8');
mysqli_query(“SET SESSION collation_connection ='utf8_general_ci'") or die (mysql_error());
I also added this code here. But every time it lost the data. if I remove the UTF8 code then I got ?????. So I need help out of this.
I guess your problem is with utf8 connection to db.
1) make use your db column in utf8_general_ci
2) set mysqli connection to use utf8
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
$mysqli->set_charset("utf8")
Check here: http://php.net/manual/en/mysqli.set-charset.php
This one worked for me. Try this code below.
public function __construct()
{
$hostname = "server_address";
$username = "username";
$password = "password";
$database = "database_name";
$this->link = mysqli_connect($hostname, $username, $password, $database);
mysqli_set_charset($this->link, "utf8");
}