Php database not able to be retrieved - php

I have a problem of data not being retrieved from database. It only echos the sentence that I typed instead of the data in my database. I've tried it for several times and it still does not work. Is there anything wrong with my code? Please help
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "database-username";
$password = "database-password";
$host = "localhost";
$connector = mysqli_connect($host,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysqli_select_db($connector, "test_db")
or die("Unable to connect");
//execute the SQL query and return records
$result = mysqli_query("SELECT * FROM table_one ");
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>Employee_id</th>
<th>Employee_Name</th>
<th>Employee_dob</th>
<th>Employee_Adress</th>
<th>Employee_dept</th>
<td>Employee_salary</td>
</tr>
</thead>
<tbody>
<?php
while( $row = mysqli_fetch_assoc( $result ) ){
echo
"<tr>
<td>{$row\['employee_id'\]}</td>
<td>{$row\['employee_name'\]}</td>
<td>{$row\['employee_dob'\]}</td>
<td>{$row\['employee_addr'\]}</td>
<td>{$row\['employee_dept'\]}</td>
<td>{$row\['employee_sal'\]}</td>
</tr>\n";
}
?>
</tbody>
</table>
<?php mysqli_close($connector); ?>
</body>
</html>

You need below Changes in your Code :
1) Change these line :
$result = mysqli_query("SELECT * FROM table_one ");
To below line :
$result = mysqli_query($connector, "SELECT * FROM table_one ");
2) Change these lines :
<td>{$row\['employee_id'\]}</td>
<td>{$row\['employee_name'\]}</td>
<td>{$row\['employee_dob'\]}</td>
<td>{$row\['employee_addr'\]}</td>
<td>{$row\['employee_dept'\]}</td>
<td>{$row\['employee_sal'\]}</td>
To Below Lines :
<td>{$row['employee_id']}</td>
<td>{$row['employee_name']}</td>
<td>{$row['employee_dob']}</td>
<td>{$row['employee_addr']}</td>
<td>{$row['employee_dept']}</td>
<td>{$row['employee_sal']}</td>
That's It.

Related

Aligning data in a table according to each characteristics in php

I get data from a mssql database that I want to display in a table.
The table has three categories:
ORDER NUMBER
PAYMENT STATUS
ORDER STATUS
The problem is as follows:
I would like to align my order numbers with the payment status and the order status in order to have a nice table that is well aligned.
Here is my code in php and the result it gives me :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/clientsS.css">
<title></title>
</head>
<body>
<div class="BLOC1">
<div class="NUMBER">
<center><table>
<tr>
<th>NUMERO COMMANDE</th>
<th>STATUT PAIEMENT</th>
<th>STATUT COMMANDE</th>
</tr>
<tr>
<td><?php
//CONNEXION ODBC SERVER//
$dsn="";
$user="";
$password="";
$conn=odbc_connect($dsn
,$user, $password);
//REQUETES
$sql = <<<EOF
SELECT top 10 [enc_cmd_num]
FROM [encaissement]
WHERE enc_date= '20221220'
EOF;
$results = odbc_exec($conn,$sql);
while($resultrow = odbc_fetch_array($results)){
echo $resultrow["enc_cmd_num"]."<br/>" ; }
?>
</td>
</div>
<div class="TITRE">
<td><?php
//CONNEXION ODBC SERVER//
$dsn="";
$user="";
$password="";
$conn=odbc_connect($dsn,$user, $password);
//REQUETES
$sql = <<<EOF
SELECT top 10 [enc_paye]
FROM [encaissement]
WHERE enc_date= '20221220'
EOF;
$results = odbc_exec($conn,$sql);
//CONDITION
while($resultrow = odbc_fetch_array($results)) {
switch($resultrow['enc_paye']){
case 0:
echo "<p>En attente paiement</p> \r\n";
break;
case 1:
echo "<p class='green'>Commande payée<p/>\r\n";
break;
}
}
?>
</td>
<td><?php
//CONNEXION ODBC SERVER//
$dsn="";
$user="";
$password="";
$conn=odbc_connect($dsn,$user, $password);
//REQUETES
$sql = <<<EOF
SELECT top 10 [enc_prepared]
FROM [encaissement]
WHERE enc_date= '20221220'
EOF;
$results = odbc_exec($conn,$sql);
//CONDITION
while($resultrow = odbc_fetch_array($results)) {
switch($resultrow['enc_prepared']){
case 0:
echo "<p>Commande en attente</p> \r\n";
break;
case 1:
echo "<p class='yellow'>Commande en cours de préparation<p/>\r\n";
break;
}
}
?>
</td>
</tr>
</table>
</div>
It is preferable that you use the same tag <p> even for the first <td>:
Test it like this and tell me if it works for you:
echo "<p>".$resultrow["enc_cmd_num"]."<p/>" ;
Instead of:
echo $resultrow["enc_cmd_num"]."<br/>" ;

How to use the value of a link in search query

Could someone please help me how to do this?
When I click a link, it will be directed to a new page containing the contents related to the value of the link. Like for example, after clicking the title of an article, it will be directed to a new page containing its content. So the value of the link will be used in search.
search.php
<?php
if(isset ($_POST['valueTosearch']))
{
$valueToSearch = $_POST['valueToSearch'];
$query = "SELECT * FROM `events` WHERE CONCAT(`title`) LIKE
'%".$valueToSearch ."%'";
$search_result = filterTable($query);
}
else {
$query="SELECT * FROM `events`";
$search_result = filterTable($query);
}
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "silangagri");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP HTML TABLE DATA SEARCH</title>
<style>
table,tr,th,td
{
border: 1px solid black ;
}
</style>
</head>
<body>
<table style="padding-right: 2; padding-left: 2; padding-top: 4; padding-
bottom: :4">
<tr>
<th>title</th>
<th>location</th>
</tr>
<?php while($row = mysqli_fetch_array($search_result)): ?>
<tr>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['location']; ?></td>
</tr>
<?php endwhile;?>
</table>
</body>
</html>
index.php
<?php
$host = "localhost";
$username="root";
$password="";
$database="silangagri";
$connect = new
PDO("mysql:host=$host;dbname=$database",$username,$password);
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT * FROM news ORDER BY date DESC";
$data = $connect->query($query);
?>
<!DOCTYPE html>
<html>
<body>
<form action = "search.php" method="post">
<?php foreach($data as $row) {
echo '<a name="valueTosearch" href="search.php">
<h5>'.$row["title"].'</h5></a>';
echo '<br><br>';?>
<?php
}
?>
</form>
</body>
</html>

Get specific data from database with PHP code when a user login

I want to get data from my database with PHP code. In brief when a user log in with id & password i want to show his data only in user panel, but i got some error: Here is my user admin panel code:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>User Panel</title>
<link rel="stylesheet" href="assets/demo.css">
<link rel="stylesheet" href="assets/form-labels-on-top.css">
</head>
<body>
<header>
<h1 align="center"><b>User Information Details</b></h1></br>
</header>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "xyz";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
include('user_login_check.php');
$result= mysql_query("SELECT * FROM `user_information` WHERE `user_id` = '".$_SESSION['id']."' ")or die(mysql_error());
// $result = mysql_query("SELECT * FROM user_information WHERE user_name='" . $_POST["user_name"] . "' and user_password = '". $_POST["user_password"]."'");
?>
<form>
<table border="5" style= "background-color: #333333; color: #FFF; margin: 0 auto; padding:100px" >
<thead>
<tr>
<th>User ID</th>
<th>User Type</th>
<th>User Name</th>
<th>User Country</th>
<th>User Email</th>
<th>User Phone</th>
<td>User Address</td>
<td>User Password</td>
<td>User Status</td>
<td>Edit</td>
</tr>
</thead>
<tbody>
<?php
while( $row = mysqli_fetch_assoc( $result ) ){
echo
"<tr>
<td>{$row['user_id']}</td>
<td>{$row['user_type']}</td>
<td>{$row['user_name']}</td>
<td>{$row['user_country']}</td>
<td>{$row['user_email']}</td>
<td>{$row['user_phone']}</td>
<td>{$row['user_address']}</td>
<td>{$row['user_password']}</td>
<td>{$row['user_status']}</td>
<td><img src='image/editicon.jpeg'/></td>;
</tr>\n";
}
?>
</tbody>
<div align="left" style="background:#333333">
<h1><input type="button" value="Log Out" onClick="window.location.href='logout_user.php'" /> </h1>
</div>
</table>
</form>
</body>
</html>
problem is in specially this section
$result= mysql_query("SELECT * FROM `user_information` WHERE `user_id` = '".$_SESSION['id']."' ")or die(mysql_error());
here is my login check code:
<?php
//connect the database
$hostName="localhost";
$dbUsername="root";
$dbPassword="";
$dbName="xyz";
mysql_connect($hostName,$dbUsername,$dbPassword) or die("Connection failed");
mysql_select_db($dbName) or die("Database name doesn't exist");
//start session
#session_start();
if(isset($_POST["user_name"] , $_POST["user_password"]))
{
$User_name = $_POST["user_name"];
// echo "$User_name";
$User_password = $_POST["user_password"];
// echo "$User_password";
$sql = "SELECT * FROM `user_information` WHERE `user_name`='".$User_name."' AND `user_password`='".$User_password."' ";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql);
// $result = mysql_query("SELECT * FROM `user_info` WHERE `User_name`='".$username."' AND `User_password``='".$password."'");
$my_arary = array();
while($row = mysql_fetch_assoc($result))
{
$_SESSION['id']= $row["Id"];
// echo $_SESSION['login_user_id'];
$_SESSION['user_password']= $row["user_password"];
//echo $_SESSION['login_user_password'];
$_SESSION['user_name']= $row["user_name"];
//echo $_SESSION['login_user_name'];
$my_arary[] = $row;
print_r($row);
if($User_name == $_SESSION['user_name'] && $User_password == $_SESSION['user_password']){
header("Location: userpanel.php");
//echo "successfully logged in";
}
else{
echo "no matches";
header("Location: user_login_basic.php");
}
}
}
?>
You set your $_SESSION['id']= $row["Id"] . Are you sure there column Id in your table? Because you using user_id instead of Id in your query in user admin panel code

Fetch from mySQL to HTML TABLE

I have used this code but its not fetching anything other than displaying the table on the web.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "root";
$password = " ";
$host = "localhost";
$connector = mysql_connect($host,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("ticad", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM users ");
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>id</th>
<th>Names</th>
<th>Company</th>
<th>Position</th>
<th>Email</th>
<th>Event</th>
<th>Comments</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr>
<td>{$row\['id'\]}</td>
<td>{$row\['Names'\]}</td>
<td>{$row\['Company'\]}</td>
<td>{$row\['Position'\]}</td>
<td>{$row\['Email'\]}</td>
<td>{$row\['Event'\]}</td>
<td>{$row\['Comments'\]}</td>
</tr>\n";
}
?>
</tbody>
</table>
<?php mysql_close($connector); ?>
</body>
</html>
This is my Table and dabase
This is what am getting when I run the code as a web. I want all the records to be displayed on the table as we fetch from the table users
try this and don't escape... And as I saw your picture. You need to upload your file onto webserver with support of PHP. Rename your file to .php extension
echo
"<tr>
<td>".$row['id']."</td>
<td>".$row['Names']."</td>
<td>".$row['Company']."</td>
<td>".$row['Position']."</td>
<td>".$row['Email']."</td>
<td>".$row['Event']."</td>
<td>".$row['Comments']."</td>
</tr>\n";
Don't escape the brackets:
<td>{$row['id']}</td>

Modify php script to populate SQL Server 2012

I have this php script that parses data from a regular url in the form of "http://example.com/board.php?username=John&score=15&session=976837465", places that in a simple MySQL db, then sorts the score to form a top 10.
Can someone help me with the code to do the same for a SQL Server 2012 db? Please assume that the db has already been created.
Note : I know the method above is insecure and not recommended, but at present it is the only way I am able to pass data over - trust me on this.
Please find the full php code below.
Thanks in advance,
John
<?php
$dbhost = 'localhost'; // Database Host Name - usually 'localhost'
$dbname = 'mydb'; // Database Name
$dbuser = 'myusername'; // Database User Name
$dbpass = 'mypassword'; // Database User Password
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname,$conn);
$game = 'My Game'; // The Name of your game
{
$name = urldecode(mysql_real_escape_string($_GET['username']));
$score = intval(mysql_real_escape_string($_GET['score']));
$gamesession = mysql_real_escape_string($_GET['session']);
global $tlb;
if (!empty($game) && !empty($name) && !empty($score) && !empty($gamesession)) {
$query = "INSERT INTO game_leaderboard (game, name, score, session)
VALUES('$game', '$name', '$score', '$gamesession')";
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
}
}
?>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Sansita+One'
rel='stylesheet' type='text/css'>
<link href="table.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<div role="main">
<header>
<h2 class="toptitle">My Game</h2>
</header>
<?php
$lboard = "SELECT * FROM game_leaderboard WHERE game = '$game' ORDER BY score DESC LIMIT 10";
$leaderboard = mysql_query($lboard) or trigger_error(mysql_error()." in ".$lboard);
?>
<table class="th">
<thead style="background:#f7f7f7;">
<tr>
<td style="font-size: 18px; width:30%;">Name</td>
<td style="font-size: 18px; width:70%;">Score</td>
</tr>
</thead>
<tbody style="background:#ffffff;border:1px solid #bfbfbf;">
<?php
$i=0;
while ($i < mysql_numrows($leaderboard)) { ?>
<tr style="border-bottom:1px solid #cccccc;">
<td><?php echo ucwords(mysql_result($leaderboard,$i,"name")); ?></td>
<td><?php echo mysql_result($leaderboard,$i,"score"); ?></td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
</div>
</body>
</html>
PHP has made it easy to access data from any database, you can follow this guide i found here for ms sql servers http://www.easysoft.com/developer/languages/php/sql_server_unix_tutorial.html

Categories