Unfortunately my code isn't working quite well.
The source code:
<!DOCTYPE html>
<html>
<head>
<title>Query data from News database and display in table</title>
<meta charset="UTF-8">
<meta name="description" content="" />
<meta name="author" content="WRBikAir" />
<meta name="keywords" content="" />
<style>
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<?php
error_reporting(E_ALL);
echo "Test 1";
$con = mysqli_connect("mysql.hostinger.com","u441817146_admin","CBGApp","u441817146_cbg");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM News";
if ($result = mysqli_query($con, $sql)) {
echo "<table>";
while($row = $result->fetch_assoc()) {
$r = json_encode($row);
echo "<tr><td>" . $r['NID'] . "</td><td>" . $r['headline'] . "</td><td>" . $row['text'] . "</td><td>" . $r['timestamp'] . "</td></tr>";
}
echo "</table>";
} else {
echo "no result.";
}
mysqli_close($con);
echo "2";
?>
</body>
</html>
Everything works fine, except of the output of the NID, headline and timestamp. There are all '{'. Does it mean, that there is now value? because if I simply print them out (encoded of course) there are values e.g.:
{"NID":"1","headline":"Testartikel 2","text":"test test test","timestamp":"15.11.2017, 18:13"}
Does somebody knows a solution?
You are using $result 2 times on $result = mysqli_query($con, $sql) and in your foreach loop. I changed the one in the foreach loop to $results instead of $result.
Also try turning on error reporting using:
error_reporting(E_ALL);
Try using:
<!DOCTYPE html>
<html>
<head>
<title>Query data from News database and display in table</title>
<meta charset="UTF-8">
<meta name="description" content="" />
<meta name="author" content="WRBikAir" />
<meta name="keywords" content="" />
<style>
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<?php
echo "Test 1";
$con = mysqli_connect(CENSORED (but working in other classes fine);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM News";
if ($result = mysqli_query($con, $sql)) {
$resultArray = array();
$tempArray = array();
while($row = $result->fetch_object()) {
$tempArray = $row;
array_push($resultArray, $tempArray);
}
echo "<table>";
foreach ($resultArray as $results) {
$r = json_encode($results);
echo "<tr><td>" . $results['headline'] . "</td><td>" . $results['text'] . "</td></tr>";
}
echo "</table>"
}
mysqli_close($con);
echo "2";
?>
</body>
</html>
For everybody who need the working answer.
Thank you to MasterOfCoding, GrumpyCrouton, Paul Spiegel and prodigitalson.
Special thanks to tadman for the insider information.
Now the code:
<!DOCTYPE html>
<html>
<head>
<title>Query data from News database and display in table</title>
<meta charset="UTF-8">
<meta name="description" content="" />
<meta name="author" content="WRBikAir" />
<meta name="keywords" content="" />
<style>
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<?php
error_reporting(E_ALL);
echo "Test 1";
$con = mysqli_connect("...","...","...","...");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM News";
if ($result = mysqli_query($con, $sql)) {
echo "<table>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row['NID'] . "</td><td>" . $row['headline'] . "</td><td>" . $row['text'] . "</td><td>" . $row['timestamp'] . "</td></tr>";
}
echo "</table>";
} else {
echo "no result.";
}
mysqli_close($con);
echo "2";
?>
</body>
</html>
Related
how could i add ajax to my php file ,that when i delete an item of the list the data should automatically reload and my list should be updated to the new list?
I made list of all my data with a button of delete
here is my code for php
<?php
function tableV1 ($row) {
echo '<tr>';
echo '<td>' .$row['id']. '</td>';
echo '<td>' . $row['firstname'] . '</td>';
echo '<td>' . $row['lastname'] . '</td>';
}
$dbPassword = "micr2001";
$dbUserName = "PHPFundamentals";
$dbServer = "localhost";
$dbName = "PHPfundamentals";
// Create connection
$conn = mysqli_connect($dbServer , $dbUserName , $dbPassword,$dbName) or die("unable to connect to host");
// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<style type="text/css">
table {font-size: 27px; }
tbody {}
td {border-bottom: 1px solid bisque;padding:15px;}
th {border-bottom: 1px solid bisque;padding:15px;}
thead {}
tr {}
</style>
</head>
<body>
<center>
<table>
<thead>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Action</th>
<th> </th>
</thead>
<?php
// Takes all the results from the table with genre 5.
$sql = "SELECT id,firstname, lastname FROM persons";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
tableV1($row);
$rowId = $row['id'];
$first_name = $row['firstname'];
$last_name = $row['lastname'];
echo '<td>'
. ' <a class="edit_btn btn" href=edit.php?id='.$rowId.'&lname='.$last_name.'&fname='.$first_name. '>Edit</a>'
. ' </td>';
echo '<td><a class="del_btn btn" href=delete.php?id='.$rowId.'>Delete</a></td>';
echo' </tr>';
}
} else {
echo '<tr><td colspan="3">0 results</td></tr>';
}
?>
</table>
</center>
</body>
</html>
<?php
$conn->close();
?>
here is the code for delete.php
<?php
require 'connection.php';
$id = $_GET['id'];
$sql = "DELETE FROM persons WHERE id = ".$id ;
if(mysqli_query($conn, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
mysqli_close($conn);
?>
i want the item that i delete should get off my list
Why do you even need AJAX? As user clicks Delete, she / he is redirected to the delete.php script. In that script after sucessful deletion you should just redirect the user back to the listing. This can be achieved by HTTP redirect (https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection). Keep it simple!
I have created a multiple choice quiz/questionnaire. The questions and answers are pulling from the database and I have sessions setup on the webpage. At the minute the user can only print the webpage when they get their results.
I am using phpmyadmin and php for the quiz. I am currently pulling from one database table called questions with the fields, questionID, question, ansYes and ansNo.
<?
//Always start this first
session_start();
include ("dbConnect.php");
if ( isset( $_SESSION['user_email'] ) ) {
//Grab user data from the database using user email
} else {
// Redirect them to the login page
header("login.php");
}
$sql = "SELECT * FROM questions";
$dbQuery = $db->prepare($sql);
$dbQuery->execute();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>List</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<style>
/* CSS animation */
#-webkit-keyframes fadeIn {
0% {
opacity:0;
transform: scale(0.6);
}
100% {
opacity:100%;
transform: scale(1);
}
}
#keyframes fadeIn {
0% { opacity:0; }
100% { opacity:100%; }
}
</style>
<?php include("navIn.php"); ?>
<body>
<div class="container-fluid">
<br>
<?php
/*Printing out questions*/
if(!isset($_POST["submitForm"])) {
echo "<form method='post' action='checklist.php'>";
if (isset($_SESSION["first_name"])) {
echo '<span style="color:#000000;position:left; left:1060px;">Hello '.$_SESSION["first_name"].'!<br></span>';
}
while($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
echo"<div class=container-fluid text-center>";
echo "<h4>" . $dbRow["question"] . "</h4>";
echo "<h4><input type='radio' required name='" . $dbRow["questionID"] . "' value='Yes'> Yes <br></h4?";
echo "<h4><input type='radio' required name='" . $dbRow["questionID"] . "' value='No'> No <br><br></h4>";
echo "</div>";
}
echo "<input type='submit' name='submitForm' value='Submit'>";
echo "</form>";
echo"</div>";
} else {
if (isset($_SESSION["first_name"])) {
echo '<span style="color:#000000;position:left; left:1060px;">Hello '.$_SESSION["first_name"].'!<br></span>';
}
/*printing out results from questions */
echo"<div class=container-fluid text-center>";
while($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
if($_POST[$dbRow["questionID"]] == "Yes") {
echo '<h4 span style="color:blue;"> '.$dbRow["question"].'</span>';
/*echo "<h4>" .$dbRow["question"]."<br></h4><br>";*/
echo "<h4>" . $dbRow["ansYes"] . "</h4><br>";
} else {
echo '<h4 span style="color:blue;"> '.$dbRow["question"].'</span>';
/* echo "<h4>" .$dbRow["question"]."<br></h4><br>";*/
echo "<h4>" . $dbRow["ansNo"] . "</h4><br>";
}
}
echo"Click the button bellow to print this page!";
echo'<br><form> <input type=button value="Print me!" onClick="javascript:window.print()"> </form>';
echo"</div>";
}
?>
</div>
</body>
<?php include ("footer.php"); ?>
</html>
I would like to be able to save the users results so anytime they access checklist.php their results are there instead of having to print the page out.
I have a problem, obviosly with php script.
I am trying to get some data from mssql but I am getting an error:
Warning: mssql_query() [function.mssql-query]: message: Unicode data
in a Unicode-only collation or ntext data cannot be sent to clients
using DB-Library (such as ISQL) or ODBC version 3.7 or earlier.
(severity 16) in /var/www/mssql_test_saop.php on line 63
Warning: mssql_query() [function.mssql-query]: Query failed in
/var/www/mssql_test_saop.php on line 63 Rezultati za:
SELECT * FROM VseClaniceISPAPDokument
This is structure of table VseClaniceISPAPDokument
([NazivPU] [nvarchar](20) NULL,
[MesObr] [nvarchar](20) NULL,
[LetoObr] [nvarchar](20) NULL,
[IspapXML] [xml] NULL)
This is php script:
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="windows-1250">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
table * {
font-family: "Arial Narrow";
font-size: 10px;
}
</style>
</head>
<body>
<pre><?
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = '10.10.10.110:51541';
// Connect to MSSQL
$link = mssql_connect($server, 'tinem', '****');
$db = "saopsf"; //skupna
//$db = "saopopn013";
if (!$link || !mssql_select_db($db, $link)) {
die('Unable to connect or select database!' . mssql_get_last_message());
}
// Do a simple query, select the version of
// MSSQL and print it.
$version = mssql_query('SELECT ##VERSION');
$row = mssql_fetch_array($version);
print_r($row);
// Clean up
mssql_free_result($version);
?>
</pre>
<?
function run_sql($sql) {
$query = mssql_query($sql);
echo "Rezultati za: <pre>$sql</pre>";
echo mssql_num_rows($query). " vrstic<br>";
if (mssql_num_rows($query) > 0) {
//$data = mssql_fetch_assoc($data);
?>
<table border="1" style="font-size: 10;font-family: arial;">
<tr>
<?
for ($i = 0; $i < mssql_num_fields($query); ++$i) {
$field = mssql_fetch_field($query, $i);
echo "<th>".$field->name."</th>";
}
?>
</tr>
<?
while ($row = mssql_fetch_assoc($query)) {
echo "<tr>";
foreach($row as $field) {
echo "<td>" . $field . "</td>";
}
echo "</tr>";
}
?>
</table>
<?
}
mssql_free_result($query);
}
run_sql("SELECT * FROM VseClaniceISPAPDokument");
?>
Any help? Thanks in advance
Good Day to you.This is what I have been trying to do.
I have a php page where I have drop down list with options as lecturers and students.
Once I select any option, details regarding lecturers or students will be displayed in a div using ajax.
Code is as follows.(This will be called in onchange event of the drop down list with a javascript.
<?php
session_start();
require "configuration.php";
$q = intval($_GET['q']);
if($q==1)
{
$sql="SELECT * FROM temp_instructor_log";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th></th>
<th><u><B>NIC</u></B></th>
<th><u><B>Name</u></B></th>
<th><u><B>Email</u></B></th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>"?>
<input type="checkbox" name="emplist[]" value="<?php echo $row['nic']?>">
<?php echo "</td>";
echo "<td>" . $row['nic'] . "</td>";
echo "<td>" . $row['firstName'] ." ".$row['lastName']. "</td>";
echo "<td>" . $row['email']."</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
}
?>
Its working and giving me the display with checkboxes at the very beginning.
<?php session_start();
require "configuration.php";
?>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Manage courses</title>
<script type="text/javascript">
</script>
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
if (isset($_POST['btnAccept']))
{
$utype=$_POST['category'];
$arr = $_POST['emplist'];
foreach( $arr as $item)
{
echo $item . "</br>"; /* Will print 1,2 and 3 (mind newlines)*/
}
First I tried to echo the values after checking some of the checkboxes.But it didnt work out.Your kind consideration given on this is highly appreciated.
Thanks in advance.
I can populate correctly the dropbox but I cannot find a way to display the data of the selected item in a table when I click submit. Here is the code:
index.php
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Αναζήτηση Οφειλών Ανά Πολυκατοικία</title>
<link rel="stylesheet" href="tbl_style.css" type ="text/css"/>
</head>
<body>
<form id="form1" name="form1" method="POST" action="search.php">
<?php
include('config.php');
$query = "SELECT DISTINCT odos FROM ofeiles_results ORDER BY odos ASC";
mysql_query("SET CHARACTER SET 'utf8'");
mysql_query("SET NAMES 'utf8'");
$result = mysql_query($query);
echo "<select name='polykatoikia'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['odos'] . "'>" . $row['odos'] . "</option>";
}
echo "</select>";
?>
<input type="submit" name="Submit" value="Select" />
</form>
</html>
</body>
So far so good, the dropbox gets populated. Then in the file search.php I have the following code:
search.php
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Αποτελεσματα Αναζητησης Πολυκατοικιων</title>
<link rel="stylesheet" href="tbl_style.css" type ="text/css"/>
</head>
</html>
<?php
include('config_barcode.php');
if(isset($_POST['select'])){
$odoss = $_POST['select'];
mysql_query("SET CHARACTER SET 'utf8'");
mysql_query("SET NAMES 'utf8'");
$display_query = "SELECT odos FROM ofeiles_results WHERE odos LIKE '" . $odoss . "'";
$result_exoda = mysql_query($display_query) or die(mysql_error());
print $result_exoda;
$odos = $row['odos'];
$app = $row['perigrafh'];
$enoikos = $row['enoikos'];
$mhnas = $row['mhnas'];
$synolo = $row['synolo'];
echo "</br>";
echo "</br>";
echo "</br>";
echo "<table cellpadding='3' cellspacing='0'>";
echo "<tr>";
echo "<th align='center' bgcolor='#FFCC00'><strong>Οδος</strong></th>";
echo "<th align='center' bgcolor='#FFCC00'><strong>Διαμερισμα</strong></th>";
echo "<th align='center' bgcolor='#FFCC00'><strong>Όνομα</strong></th>";
echo "<th align='center' bgcolor='#FFCC00'><strong>Σύνολο</strong></th>";
echo "<th align='center' bgcolor='#FFCC00'><strong>Μήνας</strong></th>";
echo "</tr>";
echo "<td align='center'>".$odos."</td>";
echo " <td align='center'>".$app."</td>";
echo " <td align='center'>".$enoikos."</td>";
echo " <td align='center'>".$mhnas."</td>";
echo " <td align='center'>".$synolo."</td>";
echo "</table></td>";
echo $result_exoda;
}
?>
All I get is a blank page. What am I doing wrong? Thanks.
I use this to display the data in tables, you don't need to use many "echo" use LOOPS
you can add more rows in
$sql = "SELECT your_row , another_row , your_row_again FROM your_table_name";
if you had more rows in you database.
$conn = mysql_connect($dbhost, $dbuser);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT your_row , another_row , your_row_again FROM your_table_name";
mysql_select_db('your_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
echo "<div align = 'center'><table border = '1'>";
echo "<th>Your_row_Header</th><th>Row_Header_again</th><th>Row_Header_again</th>";
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "<tr><td align = center>{$row['Your_row1']}</td>".
"<td align = center>{$row['Your_Row2']}</td></tr>".
"<td align = center>{$row['Your_Row3']}</td>";
}
echo "</table></div><br />";
mysql_close($conn);