Bangla font is not displaying on this PHP code - 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");
}

Related

Selecting data from sql database doesn't work. how do I fix it?

I am trying to select data from a database. I do have a successful connection, but it seems like the query doesn't work even though I know for sure that the query is right. What am I doing wrong?
If I execute the code below, the result I get is: "Connected successfullyBad query". The 'Bad query' should mean that the query is wrong, but I checked it and it isn't wrong...
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql ="SELECT * FROM `producten`";
$result = mysqli_query($conn, $sql) or die("Bad query");
$conn->close();
?>
I expect to only see "connected successfully"
You are missing your database name. You can do it two ways, or in the connect statement:
$conn = new mysqli($servername, $username, $password,$database);
Or you can do it in your select statement:
$sql ="SELECT * FROM `yourdatabase`.`producten`";
If you don´t set your database your query is wrong
Your query is right just write your database name in mysqli constructor.
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
Visit: https://www.php.net/manual/en/mysqli.construct.php
Please give database name also, check below code.
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = ""; //Enter database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql ="SELECT * FROM `producten`";
$result = mysqli_query($conn, $sql) or die("Bad query");
$conn->close();
?>

MySQL table value be equal with php var (then display it)

I have the following MySQL table and I would like to do that RVR11 (500) be equal with a php var $RVR11
I have this code but it doesnt work.
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$price = mysql_query("SELECT RVR11 FROM tablename");
$result = mysql_fetch_array($RVR11);
echo $result['RVR11'];
Sorry about this beginner question.
You should choose a database before any queries, you can pass the fourth arguments to new mysqli constructor.

can anyone explain where have i done something wrong?

When I run this code it shows parse error. Can anyone help me with this?
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "form";
// Create connection
$conn = new mysqli($servername, $username,$password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " .$conn->connect_error);
}
SELECT * FROM my_db;
$conn->close();
You should use php tags and remove those ">" also you should use mysqli_query to query the data from mysql
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "form";
// Create connection
$conn = new mysqli($servername, $username,$password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " .$conn->connect_error);
}
$query = mysqli_query($conn,"SELECT * FROM my_db");
$conn->close();
?>

I want to fetch time column from mysql database and to compare with current time

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

When I add a column, I can't retrieve it from mysql

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

Categories