Spanish Text in PHP WHILE loop from MySQL database - php

Hey: I'm trying to display data from a MySQL database in an HTML table. However, some of the text is in Spanish and the special characters are showing up as �. Does anyone know how to fix this?
Database import script db.php
<?php
header('Content-type: text/html; charset=UTF-8');
// Enter your Host, username, password, database below.
// I left password empty because i do not set password on localhost.
$con = mysqli_connect("localhost","root","","yamanagolddb");
// Check connection
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Table display script table.php:
<?php
header('Content-type: text/html; charset=UTF-8');
require('db.php');
include("auth.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content Type" content="text/html; charset=utf-8"/>
<title>View Records</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div accept-charset="UTF-8" class="form">
<p>Home
| Insert New Record
| Logout</p>
<h2>View Records</h2>
<table width="100%" border="1" style="border-collapse:collapse;">
<thead>
<tr>
<th><strong>S.No</strong></th>
<th><strong>Hecho Por</strong></th>
<th><strong>Nombre</strong></th>
<th><strong>CÓDIGO</strong></th>
<th><strong>INFLUENCIA</strong>
(1) Sin influencia a (5) Influencia maxima</th>
<th><strong>POSTURA</strong>
(1) Apoyo activo a (5) Oposicion activa</th>
<th><strong>CREDIBILIDAD</strong>
(1) Sin credibilidad a (5) Credibilidad Maxima</th>
<th><strong>ACCESIBILIDAD</strong>
(1) Alta disposicon a dialogar a (5) Sin disposicion a dialogar</th>
<th><strong>Fecha</strong></th>
<th><strong>Edit</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
<?php
$count=1;
$sel_query="Select * from surveydata ORDER BY 'jectid' desc;";
$result = mysqli_query($con,$sel_query);
while($row = mysqli_fetch_assoc($result)) { ?>
<tr><td align="center"><?php echo $count; ?></td>
<td align="center"><?php echo html_entity_decode($row["hecho_por"]); ?></td>
<td align="center"><?php echo $row["pi_nbre"]; ?></td>
<td align="center"><?php echo $row["id_nombre"]; ?></td>
<td align="center"><?php echo $row["pi_influenc"]; ?></td>
<td align="center"><?php echo $row["pi_posicio"]; ?></td>
<td align="center"><?php echo $row["pi_credib"]; ?></td>
<td align="center"><?php echo $row["pi_acces"]; ?></td>
<td align="center"><?php echo $row["fecha_actu"]; ?></td>
<td align="center">
Edit
</td>
<td align="center">
Delete
</td>
</tr>
<?php $count++; } ?>
</tbody>
</table>
</div>
</body>
</html>
In the html parts, if I add a Spanish word with special characters they display correctly. For example, CÓDIGO displays correctly. However, the data pulled from the database shows special characters as �.

Related

how do i display information from 3 tables into 1 table with php. i'm using mysql workbench

this is my code
<?php
$tilkobling = mysqli_connect("localhost","root","","oppgave_normalisering");
$sql = "SELECT person_table.personr, person_table.navn, person_table.adresse, person_table.mobilnr, person_table.postnr, person_table.bilde, mobil_table.model, sted_table.sted FROM mobil_table, person_table, sted_table";
$datasett = $tilkobling->query($sql)
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="test.css">
</head>
<body>
<table >
<tr>
<td style="text-align:center"><b>Nr<b></td>
<td style="text-align:center"><b>Navn<b></td>
<td style="text-align:center"><b>Adresse<b></td>
<td style="text-align:center"><b>Postnr</b></td>
<td style="text-align:center"><b>Sted</b></td>
<td style="text-align:center"><b>Mobilnr</b></td>
<td style="text-align:center"><b>Modell</b></td>
<td style="text-align:center"><b>Bilde</b></td>
</tr>
<?php while ($rad = mysqli_fetch_array($datasett)) { ?>
<tr>
<td style="text-align:center"><?php echo $rad["personr"]; ?></td>
<td style="text-align:center"><?php echo $rad["navn"]; ?></td>
<td style="text-align:center"><?php echo $rad["adresse"]; ?></td>
<td style="text-align:center"><?php echo $rad["postnr"]; ?></td>
<td style="text-align:center"><?php echo $rad["sted"]; ?></td>
<td style="text-align:center"><?php echo $rad["mobilnr"]; ?></td>
<td style="text-align:center"><?php echo $rad["model"]; ?></td>
<td style="text-align:center"><?php echo $rad["bilde"]; ?></td>
</tr>
</tr>
<?php } ?>
</table>
</body>
</html>
this is the result in google chrome.
This is what my tables look like in mysql workbench
as you can see under the Nr colum the numbers replicate themselves. i want it to only show 1, 2, 3 and not the replicated colums.
If you have a specific value that you`re looking for, you should be using WHERE, otherwise you can use SELECT DISTINCT at the beginning of your statement
Thanks for all the help!
The problem was solved with using "WHERE" + "AND". As you can see in the code i wrote
WHERE person_table.personr = sted_table.stednr AND person_table.personr = mobil_table.modelnr"
which were the colums that were interfering with eachother. This
solved the problem where the information was duplicating for each value in the Mobil_table.
End php code <
$sql = "SELECT person_table.personr, person_table.navn, person_table.adresse, person_table.mobilnr, person_table.postnr, person_table.bilde, mobil_table.model, sted_table.sted FROM mobil_table, person_table, sted_table WHERE person_table.personr = sted_table.stednr AND person_table.personr = mobil_table.modelnr";

The First Data Do Not Show When Getting Data From MySQL Table to PHP

I am trying to fetch data from an sql database into a php table. Yet the problem is that the first data from my database doesn't appear into the table. Did i commit a mistake ? The following is my code....
<?php
include "koneksi.php";
$sql = mysqli_query ($link,
"SELECT * FROM absen");
$data = mysqli_fetch_array($sql);
?>
<html>
<head>
<title>Data Mahasiswa</title>
</head>
<body>
<p><h2><b><center>DATA MAHASISWA</center></b></h2></p>
<table border="2" style="1000px;" align="center">
<tr bgcolor="blue">
<th>No</th>
<th>Nama</th>
<th>NIM</th>
<th>Jenis Kelamin</th>
</tr>
<?php
while($data)
while($data = mysqli_fetch_array($sql)){
?>
<tr>
<td><?php echo $data['no']; ?></td>
<td><?php echo $data['nama']; ?></</td>
<td><?php echo $data['nim']; ?></</td>
<td><?php echo $data['jenis_kelamin']; ?></</td>
</tr>
<?php } ?>
</table>
<center><b><h3><a href="Website.html"><img src="Capture.jpg" width="100px">
</a></h3></b></center>
</p>
</body>
</html>
You are doing mysqli_fetch_array() twice. Fixed code:
<?php
include "koneksi.php";
$sql = mysqli_query ($link,
"SELECT * FROM absen");
?>
<html>
<head>
<title>Data Mahasiswa</title>
</head>
<body>
<p><h2><b><center>DATA MAHASISWA</center></b></h2></p>
<table border="2" style="1000px;" align="center">
<tr bgcolor="blue">
<th>No</th>
<th>Nama</th>
<th>NIM</th>
<th>Jenis Kelamin</th>
</tr>
<?php
while($data = mysqli_fetch_array($sql)){
?>
<tr>
<td><?php echo $data['no']; ?></td>
<td><?php echo $data['nama']; ?></</td>
<td><?php echo $data['nim']; ?></</td>
<td><?php echo $data['jenis_kelamin']; ?></</td>
</tr>
<?php } ?>
</table>
<center><b><h3><a href="Website.html"><img src="Capture.jpg" width="100px">
</a></h3></b></center>
</p>
</body>
</html>

Row with button submitting wrong data to server

Ive been working on this issue for some time now, basically, i have a table and at the end of row is a button that submits that rows batchID and Priority value to my .php that handles post requests. the problem comes when i hit submit it takes the batchID column from the last row regardless of which button is pressed. i have this exact same code running on another site which works as intended. when i inspect the network data being sent it shows that all the data in column batchID is being sent to upload.php, but i only need the corresponding batchID to be sent.
<?php
$con = mysqli_connect("localhost", "root", "", "production_management");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
require('databaseDetails.php');
session_start();
$userNameTmp = $_SESSION["usrName"];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Received Request Form</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<p><button class="button button2">Dashboard</button><p>
<form name="form" method="post" action="upload.php">
<div class="form">
<h1>Batch's</h1>
<p>Hello <?php echo $userNameTmp;?> </p>
<table class="responsive-table" width="100%" border="1" style="border-
collapse:collapse;">
<thead>
<tr>
<th><strong>Started By</strong></th>
<th><strong>Batch Type</strong></th>
<th><strong>Date</strong></th>
<th><strong>Batch ID</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Progress</strong></th>
<th><strong>Priority</strong></th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM batch";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_assoc($result)) { ?>
<td align="center"><?php echo $row["startedBy"]; ?></td>
<td align="center"><?php echo $row["batchType"]; ?></td>
<td align="center"><?php echo $row["date"]; ?></td>
<td align="center"><?php echo $row["batchID"]; ?></td>
<td align="center"><?php echo $row["quantity"]; ?></td>
<td align="center"><?php echo $row["progress"]; ?></td>
<td align="center"><?php echo $row["priority"]; ?></td>
<td align="center"><input type="hidden" name="postMethod" value="3"></td>
<td align="center"><input type="hidden" name="batchID" value="<?php echo
$row["batchID"]; ?>"></td>
<td align="center"><input type="submit" value="test"></td></tr>
<?php
}
?>
</tbody>
</table>
</div>
</form>
</body>
</html>

How use session variable in sql using php

I have a problem with displaying files added by the logged user.
I do not know how to pass the variable correctly to the sql query.
Can anyone help me with this?
Currently, the code looks like this:
<?php
include_once 'dbconnect.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploading With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>File Uploading With PHP and MySql</label>
</div>
<div id="body">
<table width="80%" border="1">
<tr>
<th colspan="4">your uploads...<label>upload new files...</label></th>
</tr>
<tr>
<td>File Name</td>
<td>File Type</td>
<td>File Size(KB)</td>
<td>View</td>
</tr>
<?php
$sql="SELECT * FROM files";
$result_set=mysql_query($sql);
while($row=mysql_fetch_array($result_set))
{
?>
<tr>
<td><?php echo $row['file'] ?></td>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['size'] ?></td>
<td>view file</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
I am trying to change this record :
$sql="SELECT * FROM files";
to
$sql="SELECT file, type, size FROM files WHERE userId ='$_SESSION[userId]'";
but I still do not get the correct result. Can anyone help?
It looks like the issue with that line is in how you are including the $_SESSION variable. You should have quotes around userId like $_SESSION['userId'] or {$_SESSION['userId']}.
More importantly you should avoid entering variables directly into MySQL queries. I would recommend using MySQLi or PDO instead of MySQL, and look into prepared statements (here or here, for example).

trying to print database in a web page but can't get a counter for table

I am trying to create a page that displays rpg ranks from a mysql database on a webpage
i went into the database and organized the data by level, so the person with the highest level is shown first and so on, however the database does not contain a table for ranks
I am trying to do that manually in dreamweaver but I don't know how, can I create a counter and have it display in the table?
I managed to get the following:
http://slayersgaming.com/rpgranks2.php
<?php
$dbh=mysql_connect("********", "**********", "*********") or die('Cannot connect to the database because: '. mysql_error());
mysql_select_db("C368969_thcrpgCSGO");
$rpgranks_sql = "SELECT * FROM `thc_rpg` ORDER BY `thc_rpg`.`level` DESC LIMIT 0, 30 ";
$rpgranks_query = mysql_query($rpgranks_sql) or die(mysql_error());
$rsrpgranks = mysql_fetch_assoc($rpgranks_query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p>
<table width="501" height="58" border="0">
<tr>
<td width="240"><h2><strong>Name</strong></h2></td>
<td width="70"><strong>Level</strong></td>
<td width="70"><strong>XP</strong></td>
<td width="70"><strong>Credits</strong></td>
<td width="70"><strong>Rank</strong></td>
</tr>
</table>
<?php do{ ?>
<table width="501" height="58" border="0">
<tr>
<td width="240"><?php echo $rsrpgranks['name'];?></td>
<td width="70"><?php echo $rsrpgranks['level'];?></td>
<td width="70"><?php echo $rsrpgranks['xp'];?></td>
<td width="70"><?php echo $rsrpgranks['credits'];?></td>
<td width="70"> </td>
</tr>
</table>
<?php } while ($rsrpgranks = mysql_fetch_assoc($rpgranks_query)) ?>
</p>
<p> </p>
</body>
</html>
screen cap here: http://oi47.tinypic.com/21exdfs.jpg
This is actually really easy to do. You just need a counter in your do-while loop like this:
<?php
$counter = 1;
do{
?>
<table width="501" height="58" border="0">
<tr>
<td><?php echo $counter; ?></td>
<td width="240"><?php echo $rsrpgranks['name'];?></td>
<td width="70"><?php echo $rsrpgranks['level'];?></td>
<td width="70"><?php echo $rsrpgranks['xp'];?></td>
<td width="70"><?php echo $rsrpgranks['credits'];?></td>
<td width="70"> </td>
</tr>
</table>
<?php
$counter++;
} while ($rsrpgranks = mysql_fetch_assoc($rpgranks_query))
?>

Categories