data retrieved from database in Hebrew presented as question mark - php

I did all changes as the answer instructed in
this post
in order to be able to print hebrew strings coming from the database but didnt work.
this is my php code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
header('Content-Type: text/html; charset=utf-8');
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "dbName";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
mysql_query("SET NAMES 'utf8'");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Score: " . $row["score"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
every thing works accept that insted of the hebrew strings i only see question marks (???).
any idea why??

Set the Browser's Character Settings to Hebrew.
FireFox:Tools=>Options=>Content=>Advanced=>Fallback Character Encoding = Hebrew
Google Chrome: Menu=>Settings=>Show Advanced Settings=>Language and input settings
UPDATE:
When you save the text first recode the text.
Try using GNU Recode?
$text = mysqli_real_escape_string(recode_string(characterSet,$text));
Where characterSet complies with RFC-1345, e.g. 'latin1'
Valid Character sets: http://www.faqs.org/rfcs/rfc1345.html

Related

How to ask a user input and use it as a parameter for a stored procedure in a PHP script?

Sorry for the newbie question, but I have this task which kind of got me stuck.
So, I made a database in PhpMyAdmin, created a table with data : Products(id, name, city) and created a stored procedure that will actually do a query on the table to find out the product with a certain name (which will be input-ed by the user on the web page). My stored procedure is: proc_test and takes one VARCHAR paramter.
So, how can I do this in a php script? How can i ask the user for some data, (on the site he should have like a box to type it) then click a search button, and get redirected to the page with the query results. This is my code so far:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practice";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "CALL proc_test('pencil');";
if ($result = mysqli_query($conn, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['numec'] . "<br/>";
}
}
$conn->close();
?>
Here, of course, I manually give the parameter in the script. But I don't know how to change this and ask for a user input instead. Any help is welcome!
You can use ajax to send the data from your website to the php file where you can search for it in the database and send that data back to the user. In my example, the data is being displayed on the same webpage. You could echo the link to the website and redirect the user on the webpage using JavaScript if you really wanted to but my example is just a proof of concept.
index.html
<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<input required type="text" id="user_input">
<button onclick="sendData()">Search</button>
<p id="result"></p>
<script>
function sendData() {
var var_params = $('#user_input').val();
$.post('test.php', {params: var_params}, function(data) {
document.getElementById('result').innerHTML = "Your search result: " + data;
});
}
</script>
</body>
</html>
test.php
<?php
if(isset($_POST['params'])) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practice";
$conn = new mysqli($servername, $username, $password, $dbname); // Create connection
if ($conn->connect_error) { // Check connection
die("Connection failed: " . $conn->connect_error);
}
$param = mysqli_real_escape_string($conn, $_POST['params']);
$sql = "CALL proc_test('$param');";
if($result = mysqli_query($conn, $sql)) {
while($row=mysqli_fetch_assoc($result)) {
echo $row['numec']. "<br/>";
}
}
$conn->close();
}
?>

Show Output/Echo of PHP File executing at load on webpage

im trying to create my first website and Im clueless in this case.
So I have a MySQL-Database with a table. And I have a php-File called database.php which reads from the database and echos all the lines of a query:
<?php
$servername = "xxxxxxxxxx.hosting-data.io";
$username = "xxxxxxxx";
$password = "xxxxxxx";
$dbname = "xxxxxxx";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT ID, Name, Beschreibung, Datum, Uhrzeit FROM Termine";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row["ID"]. " - Name: " . $row["Name"]. " - Beschreibung: " . $row["Beschreibung"]. " - Datum: " . $row["Datum"]. " - Uhrzeit: " . $row["Uhrzeit"]."<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Now on my index.php I want to execute this php-code on calling/loading the webpage and print all the lines (data entries).
But i have no idea how to get the echo (=data entries) of the php file printed in the body of my webpage. I read about AJAX and using a js-script but I still wasnt able to figure it out.
Thanks for your help.
Option 1: Place your PHP code inside the HTML body.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
echo 'Hello World';
// ...
?>
</body>
</html>
Option 2: Create a separate PHP file containing your code above and include/require it into your body.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
include_once('your_php_file.php');
?>
</body>
</html>
Option 3: Call your PHP file using an AJAX call (e.g. by using jQuery load()).
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="aDiv"></div>
<script> $( "#aDiv" ).load('your_php_file.php', function() { console.log('Loaded'); });</script>
</body>
</html>
If your index file is index.php then the PHP code will be run when you load the webpage. That is assuming, of course, that your web server (local or remote) has PHP installed.

Getting Gibberish on phpmyadmin output

I'm trying to send Hebrew content through to show up on phpmyadmin. English letters go through perfectly, but Hebrew gives me something like this: חן דו×ק.
phpmyadmin collation is set on utf8_unicode_ci (Also tried utf8_general_ci). How can I solve it?
This is my code:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
header('Content-Type: text/html; charset=utf-8');
$servername = "//";
$username = "//";
$password = "//";
$dbname = "//";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO nigunim (name, time, day)
VALUES ('בדיקה', 'בדיקה', 'בדיקה')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Finally fixed by creating a new table and then adding mysql_set_charset("UTF8", $conn); along with making sure collation is set to utf8_general_ci.

Not sending cyrillic letters to mysql database with php

In my .html document where the form is i have
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
in the .php file where i connect to the database i tried with
mysql_set_charset('utf8');
// and
// <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
and this:
header('Content-Type: text/html; charset=utf-8');
The database rows are with Collation: utf8_unicode_ci and charset utf8
So my issue is that when i send the code from the html form through the php i see this in my database: ИзбереÑ
Here's the .php document code:
<?php
// header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/html; charset=utf8');
// mysql_set_charset('utf8_unicode_ci');
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbname";
$name = $_POST['name'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// mysql_set_charset('utf8');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO oglasi (Name)
VALUES ('$name')";
if ($conn->query($sql) === TRUE) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
// mysql_set_charset('utf8');
?>
PROCEDURAL:
mysqli_set_charset(connection,charset);
$con=mysqli_connect("localhost","my_user","my_password","my_db");
mysqli_set_charset($con,"utf8");
OOP:
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
exit();
} else {
printf("Current character set: %s\n", $mysqli->character_set_name());
}
before your execute your insert query, you should run this:
$conn->query("SET NAMES 'utf8'");

HTML document was not declared

This is about retriving the data in form of CSV from Mysql Table : -
Code , I tried :-
<?php
// mysql database connection details
$host = "localhost";
$username = "root";
$password = "hello";
$dbname = "mysql2csv";
// open connection to mysql database
$connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));
// fetch mysql table rows
$sql = "select * from tbl_books";
$result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));
$fp = fopen('books.csv', 'w');
while($row = mysqli_fetch_assoc($result))
{
fputcsv($fp, $row);
}
fclose($fp);
//close the db connection
mysqli_close($connection);
?>
Errors Obtained...
04:12:27.093 The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.1 mysql2csv.php.
your help will be appreciated ...
Add those lines to your html header
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
Edit:
If you are using PHP file:
header('Content-Type: text/html; charset=utf-8');

Categories