Show php data on html page - php

I would like to ask how could I display data from php class in HTML table I've tried echo theme out with HTML tags but for some reason my code doesn't recognize html tags.
public function getDataUser()
{
$sql = "SELECT ID, USERNAME , PASSWORD , EMAIL , PERMISSION_LEVEL FROM USERS";
$this->_data = $this->_connection->query($sql);
while ($row = $this->_data->fetch_assoc()) {
echo $row["ID"] .$row["USERNAME"] . $row["PASSWORD"]. $row["EMAIL"] . $row["PERMISSION_LEVEL"];
echo"<br />";
}
return $this->_data;
}
this br tag doesn't work.

As you return the data, maybe you want only to test the received data:
then you can use print_r (between <pre> tags):
public function getDataUser()
{
$sql = "SELECT ID, USERNAME , PASSWORD , EMAIL , PERMISSION_LEVEL FROM USERS";
$this->_data = $this->_connection->query($sql);
while ($row = $this->_data->fetch_assoc()) {
echo "<pre>";
print_r($row);
echo "</pre>";
}
return $this->_data;
}

Try this one
public function getDataUser()
{
$sql = "SELECT ID,USERNAME,PASSWORD,EMAIL,PERMISSION_LEVEL FROM USERS";
$this->_data = $this->_connection->query($sql);
while ($row = $this->_data->fetch_assoc()) {
//do print_r() to check wether data is coming or not
echo '<pre>';
print_r($row);
echo '</pre>';
//or you can print directly value by $row['ID']." ".$row['USERNAME'] by this way
}
return $this->_data;
}

echo $row["ID"] .$row["USERNAME"] . $row["PASSWORD"]. $row["EMAIL"] . row["PERMISSION_LEVEL"]. " <br>";

Related

How do I pass this information to print it out in another document? PHP MySQL

I am having trouble echoing row data within the page I want to print it out to.
My function works, but only echoes the information because it is local (within the same function).
I'm trying to get this function to echo the database's information to another .php file (same program).
public function findByUsername($username) {
$sql = 'SELECT * FROM events WHERE username=? ';
$statement = DatabaseHelper::runQuery($this->connection, $sql, Array($username));
while ($row = $statement->fetch()) {
echo $row['username'] . "<br />";
echo $row['date'] . "<br />";
}
}
Here is the part of the other file I need to print the information from the function to:
<?php
if (isset($_SESSION["username"])) {
$eventDAO = new EventDAO($connection);
$event = $eventDAO->findByUsername($_SESSION["username"]);
foreach((array)$event as $e) {
echo $e->getUsername() . ' ' . $e->getDate() . '<br>';
}
}
?>
I'm trying to output the username/date.
Not 100% on this concept.
If you're simply trying to output username/date from one file to another, try using sessions.
public function findByUsername($username) {
$sql = 'SELECT * FROM events WHERE username=? ';
$statement = DatabaseHelper::runQuery($this->connection, $sql, Array($username));
while ($row = $statement->fetch()) {
echo $row['username'] . "<br />";
echo $row['date'] . "<br />";
$_SESSION['getuname'] = $row['username'];
$_SESSION['getdate'] = $row['date'];
}
}
You can then output the information on another php file simply by echoing it out.
echo "Username is ". $_SESSION['getuname'];

Echo from function php

hello i want to echo a result from functions
code
function AboutUser()
{
global $Connection;
$GetUsers = mysqli_query($Connection, "SELECT * FROM users WHERE username='GentritAbazi'");
while($Show_Users = mysqli_fetch_array($GetUsers))
{
return $SignupDate = $Show_Users['signup_date'];
$Email = $Show_Users['email'];
$Gender = $Show_Users['gender'];
$Country = $Show_Users['country'];
}
}
Now my code not work
AboutUser()
how to do this ?
function AboutUser()
{
global $Connection;
$GetUsers = mysqli_query($Connection, "SELECT * FROM users WHERE username='GentritAbazi'");
while($Show_Users = mysqli_fetch_array($GetUsers))
{
echo $Show_Users['signup_date'];
echo $Show_Users['email'];
echo $Show_Users['gender'];
echo $Show_Users['country'];
}
}
Because you return $SignupDate = $Show_Users['signup_date'];
You want to echo, not return.
Let's use this in the while loop.
echo $Show_Users['signup_date'] ."<br>";
echo $Show_Users['email'] ."<br>";
echo $Show_Users['gender'] ."<br>";
echo $Show_Users['country'] ."<br>";
echo '<hr>'
But that is most elegant, if you collect all the data into a big array, and loop through that array.
return mysqli_fetch_all($GetUsers);
Based on the comments, and after I realized, you probably want to get one users data, here is the updated code:
function AboutUser($userName) {
global $Connection;
$res = mysqli_query($Connection, "SELECT * FROM users WHERE username='". mysqli_real_escape_string($Connection, $userName)."'");
return mysqli_fetch_row($res);
}
$userData = AboutUser('GentritAbazi');
if (!empty($userData)) {
echo $userData['signup_date'] ."<br>";
echo $userData['email'] ."<br>";
echo $userData['gender'] ."<br>";
echo $userData['country'] ."<br>";
}
You can have the function echo the value instead of returning it, like mentioned above.
Or you can use special tags e.g.
<?=
AboutUser();
?>
Hopefully this works

PHP and mysqli to modify CSS

I was experimenting if I could use a mySQL database to store CSS settings. I set up a simple database "colors" with one table "color" that had simple structure tag and color columns. In that, one row is h1 => red.
<?php
//function to dynamically change CSS
$tag = 'h1';
$q = "SELECT * FROM `colors` WHERE `tag`='" . $tag . "'" ;
echo $q . "<br>";
$query = mysqli_query($link, $q);
if ($row = mysqli_fetch_assoc($query))
{
echo $row['color'];
} else
{
echo "error - no such tag";
}
?>
When I tried to convert to a function, the code does not work at all.
<?php
//function to dynamically change CSS
function getCSS($tag)
{
$tag = 'h1';
$q = "SELECT * FROM `colors` WHERE `tag`='" . $tag . "'" ;
echo $q . "<br>";
$query = mysqli_query($link, $q);
if ($row = mysqli_fetch_assoc($query))
{
echo $row['color'];
} else
{
echo "error - no such tag";
}
}
getCSS('h1');
?>
Help please?
My guess is that in
$query = mysqli_query($link, $q);
$link goes out of scope and is empty. You should pass it to the function as well.
For the record: using $tag without escaping could be an sql injection attack possibility.
in function, there is no $link, you shoud define it as a global variable.
At the start of your function add a reference to your global DB link:
function getCSS($tag) {
global $link;
...
This should work:
<?php
$link = mysqli_connect('server_host', 'user', 'password', 'database') OR die('Could not connect because: '.mysqli_connect_error());
//function to dynamically change CSS
function getCSS($link, $tag){
$q = 'SELECT * FROM colors WHERE tag = "' . $tag . '"' ;
$r = mysqli_query($link, $q);
if(mysqli_num_rows($r)>0){ // check if there are results
while($row = mysqli_fetch_assoc($r)){
//echo '<pre>';
//print_r($row); // print the result array for debugging
//echo '</pre>';
echo $row['color'] . '<br />';
}
return $row;
} else { // if no result is found
echo 'No such tag';
}
}
// test it:
echo '<br />if tag is h1<br />';
getCSS($link, 'h1');
echo '<br />if tag is h2<br />';
getCSS($link, 'h2');
?>

How to loop the $row = mysql_fetch_array($rs);

I have a table
Now.i have a function in my JS
function add()
{
<?php
include('conn.php');
$rs = mysql_query("select * from position");
$row = mysql_fetch_array($rs);
$ss=$row['Name'];
$sss=$row['nowb'];
$ssss=$row['totalb'];
$sssss=$row['nowc'];
$ssssss=$row['totalc'];
echo "add2()";
?>}
function add2(){
AddAddress("<?php echo $ss;?>","<?php echo $sss;?>/<?php echo $ssss;?><br /><?php echo $sssss;?>/<?php echo $ssssss;?>");
}
How to get the every date from my table?
Something like this?
function add() {
<?php
include('conn.php');
$rs = mysql_query("select * from position");
while ( $row = mysql_fetch_array($rs) ) {
$ss=$row['Name'];
$sss=$row['nowb'];
$ssss=$row['totalb'];
$sssss=$row['nowc'];
$ssssss=$row['totalc'];
echo 'AddAddress("' . $ss . '","' . $sss . '/' . $ssss . '<br />' . $sssss . '/' . $ssssss . '");';
}
?>
}
Didn't text the echo 'AddAddress....' line so I hop eI got all the single and double quotes in the right place??
Performing POST requests using Ajax here is an example of sending data from js to php.
also stop naming your vars s,ss,sss,ssss you will have no idea what they mean when you read your code tomorrow..
and try not to use mysql_* functions they have been deprecated switch to mysqli or pdo
I got what would you like to do. In your PHP file:
function add(){
<?php
include('conn.php');
$rs = mysql_query("select * from position");
echo "var data = [] ; "
while($row = mysql_fetch_assoc($rs)){
echo "
data.push({
name: '{$row['Name']}',
nowb: '{$row['nowb']}',
totalb: '{$row['totalb']}',
nowc: '{$row['nowc']}',
totalc: '{$row['totalc']}'
}); \n\r " ;
}
?>
add2(data);
}
function add2(data){
for (var i in data){
var row = data[i] ;
AddAddress(row.name, row.nowb, row.totalb, row.nowc, row.totalc);
}
}
If I understand the question correctly you want to know how to loop through an array in php.
$row = mysql_fetch_array($rs);
foreach($row as $value){
//Do something
}
Read up on it in the docs
http://php.net/manual/en/control-structures.foreach.php

Checking querystring values in PHP

http://localhost/?area=characters&name=Michal+Stroganof
$result = mysql_query("SELECT * from players WHERE name = '$_GET[name]'");
while ($row = mysql_fetch_assoc($result)) {
echo "Name: " .$row['name']. "<br>";
echo "Level: " .$row['level']. "<br>";
}
This is all code of my characters.php
If the get variable "name" is not included in the URL i want to show a search form that searches the table players. How would I do this?
Do you mean just to change your SQL string like so?
$sql = 'SELECT * from players';
if (isset($_GET['name'])) {
$safename = mysql_real_escape_string($_GET['name']);
$sql .= " WHERE name='$safename'";
}
$result = mysql_query($sql);
Be sure to sanitize your SQL!
Use isset():
if (isset($_GET['name'])) {
// your above code
} else {
// display form
}
Quick and dirty:
<?php
if (!isset($_GET['name']))
{
echo '<form action="'. $_SERVER['PHP_SELF'] .'" method="GET">'
.'<input type="text" name="name" />'
.'</form>';
}
else
{
// your current code that queries your database here
}
?>

Categories