I'm working on a symfony2 project. when i insert data with french accents like (é), it is inserted as shown in this image:
when i try to retrieve this data using a php file to use it in android app, i use this code:
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','adproject');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect to db');
$sql = "select * from actualite";
$result = array();
$res = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'login'=>$row[1],
'password'=>$row[2]
));
}
echo json_encode($result);
mysqli_close($con);
?>
I get nothing because of the accents in the database. Any suggestion?
I suspect you'll need to go through this Configuring the Database section and setting up for UTF8:
http://symfony.com/doc/current/doctrine.html#configuring-the-database
Let us know if you've already done that.
Related
I have a PHP web service in which I am retrieving data from mysql database. In database the data is stored in Marathi language, but when I am running my web service in postman, data is coming but many question mark symbol are displayed. How to solve this issue? Below is my PHP web service code:
<?php
//include config_db.php
include_once('config_db.php');
$qr = "SELECT * FROM `recipe_breakfast`";
$qrResult = mysqli_query($db, $qr);
$count = mysqli_num_rows($qrResult);
if($count > 0)
{
while($data = mysqli_fetch_assoc($qrResult))
{
$result[] = $data;
}
$response = array('msg' => 'Success','details' => $result);
}
else
{
$response = array('msg' => 'No details found');
}
#mysqli_close($db);
/* Output header */
header('Content-type: application/json');
echo json_encode($response);
?>
You have a charset issue.
In your code, after this line:
include_once('config_db.php');
Add this:
mysqli_query($db, 'set names utf8mb4');
If somehow your database doesn't support utf8mb4, use this instead:
mysqli_query($db, 'set names utf8');
This should solve the problem.
If it doesn't, you also have an issue in your table.
The solution is to set the collation of every varchar or text column to
utf8mb4_general_ci.
This means that you have to redo the data entry.
I'm a bit newbi in PHP. I implemented http://www.sanwebe.com/2013/03/loading-more-results-from-database solution in my new website: http://do2go.pl/do2go-nowa/
its working well - except encoding. My DB and all things are in UTF8. When fetch taking data from DB UTF8 seems not working.
Heres configuration and fetch code:
Config:
<?php
$db_username = 'kamio2_do2gonowa';
$db_password = 'JeremiasZ1!';
$db_name = 'kamio2_do2gonowa';
$db_host = 'localhost';
$item_per_page = 2;
mysqli_query ("SET NAMES 'utf8'"); mysqli_set_charset('utf8');
$connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name) or die('could not connect to database');
?>
And fetch:
<?php
include("config.inc.php"); //include config file
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
header('Content-Type: text/html; charset=UTF-8');
//throw HTTP error if page number is not valid
if(!is_numeric($page_number)){
header('HTTP/1.1 500 Invalid page number!');
exit();
}
//get current starting point of records
$position = ($page_number * $item_per_page);
//Limit our results within a specified range.
$results = mysqli_query($connecDB, "SELECT id,introtext FROM w7wst_content ORDER BY id DESC LIMIT $position, $item_per_page");
//output results from database
echo '<ul class="page_result">';
while($row = mysqli_fetch_array($results))
{
echo '<li id="item_'.$row["id"].'"><span class="page_message">'.$row["introtext"].'</span></li>';
}
echo '</ul>';
?>
I added mysql_query ("SET NAMES 'utf8'"); mysql_set_charset('utf8'); but this does nothing. Still getting � symbols and ? instead of " All next 3 was changed directly in DB tables for html symbols which isn't a solution.
Any help appreciated! :)
(To close the question, since this was the solution to the OP's problem)
Add $connecDB->set_charset("utf8"); just before $results = mysqli_query($connecDB...
You are using mysql and mysqli in one script. Only use mysqli as mysql has been deprecated. That said try this:
mysqli_character_set_name($connectDB);
mysqli_set_char_set($connectDB, 'UTF8');
Set these after your connection script.
Did you check if they are properly stored in the table? Try typing this in your database(not in PHP):
SELECT * FROM w7wst_content;
and see if it's displaying properly. Alternatively you can check table contents with some tool like phpmyadmin. If it's not you have to set collation to something like utf8_general_ci.
EDIT: Also, how about setting the charset AFTER connecting to the database, hm? :)
Hi I know this is a little general but its something I cant seem to work out by reading online.
Im trying to connnect to a database using php / mysqli using a wamp server and a database which is local host on php admin.
No matter what I try i keep getting the error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given when i try to output the contents of the database.
the code im using is:
if (isset($_POST["submit"]))
{
$con = mysqli_connect("localhost");
if ($con == true)
{
echo "Database connection established";
}
else
{
die("Unable to connect to database");
}
$result = mysqli_query($con,"SELECT *");
while($row = mysqli_fetch_array($result))
{
echo $row['login'];
}
}
I will be good if you have a look at the standard mysqli_connect here
I will dont seem to see where you have selected any data base before attempting to dump it contents.
<?php
//set up basic connection :
$con = mysqli_connect("host","user","passw","db") or die("Error " . mysqli_error($con));
?>
Following this basic standard will also help you know where prob is.
you have to select from table . or mysqli dont know what table are you selecting from.
change this
$result = mysqli_query($con,"SELECT *");
to
$result = mysqli_query($con,"SELECT * FROM table_name ");
table_name is the name of your table
and your connection is tottally wrong.
use this
$con = mysqli_connect("hostname","username","password","database_name");
you have to learn here how to connect and use mysqli
Can anyone see what the problem with my code is / where im going wrong?
I know i have the correct host,database,user and password.
This is the code in the php file, it should get all the details available on the players from my sql database, however if i go on the page it just gives me a white page. Im using go daddy as a host and my database is also on there.
Any ideas? thanks
<?php
$host = "abc12345"; //Your database host server
$db = "abc12345"; //Your database name
$user = "abc12345"; //Your database user
$pass = "abc12345"; //Your password
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if (!$connection) {
die("Database server connection failed.");
} else {
//Attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//Check to see if we could select the database
if (!$dbconnect) {
die("Unable to connect to the specified database!");
} else {
$query = "SELECT * FROM Player";
$resultset = mysql_query($query);
$records = array();
//Loop through all our records and add them to our array
while ($r = mysql_fetch_assoc($resultset)) {
$records[] = $r;
}
//Output the data as JSON
echo json_encode($records);
}
}
?>
The script is all right, I checked it with a different query.
Assuming that the table Player is not empty, the error can be either with your raw sql query (as pointed by #Sharikov in comments), otherwise the error is with the way you have configured your godaddy server.
For debugging that, I would suggest inserting dummy print_r or echo statements before you execute the query, and going through your apache logs at /var/log/apache2/access.log.
Also make sure that you don't have any core php package missing on your server (like php5-mysql if you use mysql).
I'm new to SQLite3 and PHP and was wondering whether and how I could connect to a SQLite3 database with PHP.
How would I get the data from the db and would it be possible to output them on a browser screen?
I've been searching the web for a while now, but couldn't find anything.
Thank you.
<?php
$db = new SQLite3('mysqlitedb.db');
$results = $db->query('SELECT bar FROM foo');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
Taken from here: PHP: SQLite3::query - Manual
SQLite is enabled by default with PHP. You have to use the built-in class SQLite3 (you will find some examples on this page).
This is the best way I have found and I got it directly from the sqlite website:
<?php
$db = new SQLite3('sqlite3db.db');
$results = $db->query('select * from db');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
Also if you're looking to include the php results into html like a table cell or something, go as such:
$results = $db->query('select * from db');
?>
<?php while ($row = $results2->fetchArray()) {?>
<td><h4><?php echo $row['id'];?></h4></td>
<?php } ?>