How to fetch from MySQL database “Hindi” हिन्दी text (Indian local language) - php

Database which stores my data is this:
Now I want to fetch that data and display on my php page, but when I'm trying to fetch data in my php code I'm getting text into the following formate
UID= ????/??????????/????/?????/?????/Test upgrade/1
UID= ????/??????????/??????/??????/??????????/159/1
UID= ????/??????????/??????/??????/??????????/190/1
UID= ????/??????????/??????/??????/??????????/194/1
UID= ????/??????????/??????/???????/?????? (??.)/730/1
UID= ????/??????????/??????/???????/?????? (??.)/742/1/1
UID= ????/??????????/??????/???????/?????? (??.)/732/1
UID= ????/??????????/??????/??????/??????/98/8/1
UID= ????/??????????/??????/??????/??????/48/10/1
Referring to this question I have changed my database charset to "utf8_unicode_ci", but Still not working. I have written following code to fetch the data
datebase connection page
<?php
// Database configuration
$dbHost = "localhost";
$dbUsername = "user";
$dbPassword = "xxxxxxxxxxxxx";
$dbName = "tutorialsssxxxxx";
// Create database connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
and index page
<?php
include $_SERVER['DOCUMENT_ROOT']."/header.php";
?><br>
<!DOCTYPE HTML>
<html lang="hi">
<head>
<title><?php echo $_GET['dta']; ?> Tutorials Mrtutorials.net</title>
<link href='style.css' rel='stylesheet' type='text/css'>
<script src="jquery.min.js"></script>
<script type="text/javascript">
// Show loading overlay when ajax request starts
$( document ).ajaxStart(function() {
$('.loading-overlay').show();
});
// Hide loading overlay when ajax request completes
$( document ).ajaxStop(function() {
$('.loading-overlay').hide();
});
</script>
</head>
<body>
<div class="content">
<div class="dta"> <div class="list_item"><h2><?php echo $_GET['dta']; ?> Tutorials</h2></div>
<div class="post-wrapper">
<div class="loading-overlay"><div class="overlay-content">Loading.....</div></div>
<div id="posts_content">
<?php
//Include pagination class file
include('Pagination.php');
//Include database configuration file
include('dbConfig.php');
$limit = 10;
//get number of rows
$queryNum = $db->query("SELECT COUNT(*) as postNum FROM posts");
$resultNum = $queryNum->fetch_assoc();
$rowCount = $resultNum['postNum'];
//initialize pagination class
$pagConfig = array('baseURL'=>'getData.php', 'totalRows'=>$rowCount, 'perPage'=>$limit, 'contentDiv'=>'posts_content');
$pagination = new Pagination($pagConfig);
//get rows
$query = $db->query("SELECT * FROM posts Where type=$yyy ORDER BY id DESC LIMIT $limit");
if($query->num_rows > 0){ ?>
<div class="posts_list">
<?php
while($row = $query->fetch_assoc()){
$postID = $row['id'];
?>
<table width="" border="0" cellspacing="5" cellpadding="0">
<tr class="up">
<td style="font-size: 45px; padding-left:5px; padding-right:5px"><?php echo $row["id"]; ?></td>
<td valign="left" width="100%"><?php echo $row["title"]; ?> <br> <?=$value['type']?></td>
</tr>
</table>
<?php } ?>
</div>
<?php echo $pagination->createLinks(); ?>
<?php } ?>
</div>
</div></div>
</div>
</body>
</html><?php
include $_SERVER['DOCUMENT_ROOT']."/footer.php";
?>

You need to use "set_charset"
Try this: (in your index.php)
//initialize pagination class
$pagConfig = array('baseURL'=>'getData.php', 'totalRows'=>$rowCount, 'perPage'=>$limit, 'contentDiv'=>'posts_content');
$pagination = new Pagination($pagConfig);
mysqli_set_charset( $db, 'utf8');
//get rows
$query = $db->query("SELECT * FROM posts Where type=$yyy ORDER BY id DESC LIMIT $limit");
To be precise, In your case you need to add this in code where you fetching the db:
mysqli_set_charset( $db, 'utf8');

mysqli_set_charset( $db, 'utf8'); will help you set unicode on the db connection. Now to show it in the page, you will still have to set the character encoding in html.
Remember to do this. Otherwise your page will still not show you the unicode characters.
<head>
<meta charset="UTF-8">
</head>

Nothing worked for me as per reply on stackoverflow.com, to display on web page, the Hindi Text from MySql through PHP.
The following code worked for me. Please write your comments
enter code here
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM audio WHERE AudioFor= 'Aarti' ORDER BY Name";
mysqli_set_charset( $conn, 'utf8'); **// to get the hindi font/text displayed**
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) { ?>
<table cellpadding="10">
<thead>
<tr align= "centre"><h3> Other Aartis </h3></tr>
<tr>
<th>Aarti Of</th>
<th>Wordings</th>
<th>Click to Listen</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td >" . $row["Name"]."</td><td >" . $row["Wording"]. "</td><td >". ' Click To Listen '. '</td></tr>';
}
} //else {
// echo "0 results";
//}
?>

After connecting to SQL Server and Database run the following query
msql_query("SET CHARACTER SET utf8")

Related

Deleting data from database using PHP

I am trying to delete data from MySQL using PHP
<?php
if (isset($_POST['delete'])) {
$queryDelete = "Delete FROM info WHERE userID={$_POST['delete']}";
if (!($database = mysqli_connect("localhost", "root", ""))) {
die("Could not connect to database. </body></html>");
}
if (!mysqli_select_db($database, "project2")) {
die("Could not open books database. </body></html>");
}
if (!(mysqli_query($database, $queryDelete))) {
echo "<p>Could not execute query!</p>";
die(mysqli_error($database) . "</body></html>");
}
mysqli_close($database);
}
this is my delete.php using it on this page
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="Style.css">
</head>
<header>
<div>
<p id="page">Users List</p>
<img id="title_pic" src="images/title_pic.jpg" alt="#">
</div>
</header>
<body>
<?php include 'bar.php' ?>
<?php include 'delete.php' ?>
<br><br><br><br>
<h1 style="color:yellow;"> List of all Users: </h1>
<br>
<?php
$query = "SELECT userID, fName, email FROM info";
if (!($database = mysqli_connect("localhost", "root", ""))) {
die("Could not connect to database. </body></html>");
}
if (!mysqli_select_db($database, "project2")) {
die("Could not open project database. </body></html>");
}
if (!($result = mysqli_query($database, $query))) {
echo "<p>Could not execute query!</p>";
die(mysqli_error($database) . "</body></html>");
}
mysqli_close($database);
while ($row = mysqli_fetch_row($result)) {
foreach ($row as $value) {
echo "<span style='color:white;'> $value </span>";
}
echo ' <form action = "delete.php" method = "POST">';
echo '<input type="submit" name= "delete" value="delete" class="btn">';
echo '</form>';
echo "<br>";
}
?>
</html>
It's redirecting me to delete.php page but when I go back to the second one (Displayuser.php) all info are there and nothing is deleted
I used the same technique to add info but I am having trouble to delete them from the table.
Here is how your code should look like. First in your form, provide the ID of the user you want to delete. Make sure to enable mysqli error reporting and select the right database when connecting.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$database = mysqli_connect("localhost", "root", "", 'project2');
$database->set_charset('utf8mb4'); // always set the charset
$users = $database->query("SELECT userID, fName, email FROM info");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="Style.css">
</head>
<body>
<header>
<div>
<p id="page">Users List</p>
<img id="title_pic" src="images/title_pic.jpg" alt="#">
</div>
</header>
<?php include 'bar.php' ?>
<?php include 'delete.php' ?>
<br><br><br><br>
<h1 style="color:yellow;"> List of all Users: </h1>
<br>
<?php
foreach ($users as $user) {
foreach ($user as $value) {
echo "<span style='color:white;'>'.htmlspecialchars($value).'</span>";
}
echo ' <form action = "delete.php" method = "POST">';
echo '<button type="submit" name="delete" value="'.htmlspecialchars($user['userID']).'" class="btn">Delete</button>';
echo '</form>';
echo "<br>";
}
?>
</html>
Then in your delete.php, read the POST value and delete the row with that ID using prepared statement.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$database = mysqli_connect("localhost", "root", "", 'project2');
$database->set_charset('utf8mb4'); // always set the charset
if (isset($_POST['delete'])) {
$stmt = $database->prepare("DELETE FROM info WHERE userID=?");
$stmt->bind_param('s', $_POST['delete']);
$stmt->execute();
}

Show number of new records on page

I want to display the exact number of new entries (records) on my page of my phpmyadmin database after reloading the page. I looked online but couldn't find anything usefull. Maybe someone can help me. This is the code for the page:
<?php
header("Refresh: 120");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="verzoekstyle.css">
<title>Verzoekpagina DJ</title>
</head>
<body id="body">
<div id="tabel">
<?php
$con = mysqli_connect('database','username','password');
if (!$con)
{
echo 'Geen verbinding met server';
}
if (!mysqli_select_db($con, 'eventqy179_verzoeken'))
{
echo 'Database niet geselecteerd';
}
$sql = "SELECT * FROM info";
$query = mysqli_query($con,$sql);
if(!$query)
{
echo 'Onbekende fout gevonden';
}
echo '
<table class="table">
<tr>
<th><h3><b>Artiest</b></h3></th>
<th><h3><b>Titel</b></h3></th>
<th><h3><b>Verwijderen</b></h3></th>
</tr>';
while ($row = mysqli_fetch_array($query))
{
echo '<tr>';
echo '<td><h3>'.$row['artiest'].'</h3></td>';
echo '<td><h3>'.$row['titel'].'</h3></td>';
echo '<td><h3><a href=delete.php?id='.$row['id'].'>Verwijderen</a></h3></td>';
echo '</tr>';
}
echo '</table>';
?>
</div>
</body>
</html>
Already found another way, just ORDER BY id DESC. Question is therefor answered

How to get data from database by id?

I want to get data from "data_show.php?id=2"
my code below (detail1.php)
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if(!$mysqli){
die("Connection failed: " . $mysqli->error);
}
$query = sprintf("SELECT name, humidity, ph, degree FROM dataklm");
$result = $mysqli -> query($query);
$data = array();
foreach ($result as $row) {
$data[] = $row;
}
$result->close();
$mysqli->close();
print json_encode($data);
i've success retrieve data with above code, but i want select them per id. i just tried like below. but it's not work
$query = sprintf("SELECT name, humidity, ph, degree FROM dataklm WHERE id='$_GET['id']");
i passed data to this
<?php
include 'header.php';
include 'conn.php';
?>
<html>
<style type="text/css">
#chart-container {
width: 1000px;
height: 1000px;
}
</style>
<div class="container">
<div class="jumbotron">
<h1 class="display-3" style="text-align:center;">Smartfish</h1>
<p class="lead" style="text-align:center;">Monitoring System</p>
</div>
</div>
<body>
<div id="chart-container">
<canvas id="mycanvas"></canvas>
</div>
<script src="./assets/chart.js"> </script>
<script src="./app.js"> </script>
</body>
</html>
I didn't find anything that is passing the id from your html page to the php page.
Pass the id from html to php page as:-
view more
Doing this you will be passing an id from url to the next page. and you can get this id by using GET method as
$id = $_GET['id'];
then use your query.
Hope this will help you.
If you write the "$_GET['id']" just like this in the string it's gonna be threated as a string.
$query = sprintf("SELECT name, humidity, ph, degree FROM dataklm WHERE id='".$_GET['id']."'");

use Ionic framework with php files

I'm creating a mobile app with the Ionic Framework. I got all the html pages created online. Now i want some backend code to get data from a sql server. Receiving the data is no problem with php. But when I use php pages I don't have the interface I created with Ionic.
How can I use php pages (instead of html) and still get the lay out from ionic? Example: my scorebord.php
<?php
$servername = "localhost:3306";
$username = "ssss";
$password = "dffd";
$dbname = "ddddd";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT user_username,user_city,user_highscore FROM tbl_user";
$result = $conn->query($sql);
?>
<ion-view style="" title="Scorebord">
<ion-content class="has-header" overflow-scroll="true" padding="true" style="background: url(img/hX1ml1TVGgABo3ENE6Qg_menu3.png) no-repeat center;">
<h1 style="">Scorebord</h1>
<table style="width:100%">
<?php
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row["user_username"] ?></td>
</tr>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
</table>
</ion-content>
</ion-view>
btw: Is it safe to just configure my database in a php file like that? Anyone a good alternative?
The Mobile App will be saved on a device which probably can't interpret PHP code, unlike a web server.
If you dont know/want javascipt then an iframe is probably your only option.
example.com/table.php
<h1 style="">Scorebord</h1>
<table style="width:100%">
<?php
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {?>
<tr> <td><?php echo $row["user_username"] ?></td></tr>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
</table>
your app
<ion-view style="" title="Scorebord">
<ion-content class="has-header" overflow-scroll="true" padding="true" style="background: url(img/hX1ml1TVGgABo3ENE6Qg_menu3.png) no-repeat center;">
<iframe src="example.com/table.php"/>
</ion-content>
</ion-view>
But a better solution is to use JavaScript to make http requests for JSON data or full html templates(like table.php).
I would use a json backend
highscores.php
<?php
$servername = "localhost:3306";
$username = "ssss";
$password = "dffd";
$dbname = "ddddd";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT user_username,user_city,user_highscore FROM tbl_user";
$result = $conn->query($sql);
$json = mysqli_fetch_all ($result, MYSQLI_ASSOC);
echo json_encode($json);
your app
angular.module('ionicApp', [])
.controller('MainCtrl', function($scope, $http) {
$http.get('http://example.com/highscores.php').then(function(resp) {
$scope.users = resp.data;
}, function(err) {
console.error('ERR', err);
})
});
<html ng-app="ionicApp">
<body ng-controller="MainCtrl">
<ion-content>
<ion-list>
<ion-item ng-repeat="user in users">
{{user.user_username}}
</ion-item>
</ion-list>
</ion-content>
</body>
</html>

Cannot show longblob in an image tag

I have been struggling with this problem for many hours now. The page is supposed to display a list of images from a database stored as longblobs, but all it does is to show me the 'img' icon for each icon. Any suggestions to what I can do?
showImage.php
<?php
include_once('connection.php');
$sql = "SELECT filetype, picture FROM postcards WHERE ID=". $_GET['ID'];
$rows=$conn->query($sql);
foreach ($rows as $row) {
header("Content-type: ". $row["filetype"]);
echo $row["picture"];
}
$conn=null;
?>
listImage.php
<?php include_once('connection.php'); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="uft-8">
<title>List database content</title>
</head>
<body>
<?php
$sql = "SELECT * FROM postcards";
$rows=$conn->query($sql);
foreach ($rows as $row) {
echo '<p>Upload by person: '.$row["title"].'</p>';
echo '<p><img src="showimage2.php?ID='.$row["ID"].'"></p>';
}
$conn=null;
?>
</body>
</html>
connection.php
<?php
$dsn = "mysql:host=localhost;dbname=tellastory";
$username="root";
$password="";
$e="";
try {
$conn = new PDO($dsn, $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
};
?>
Thanks in advance!

Categories