Alx Tioltisan Alx Tioltisan - 2 months ago 12
HTML Question

How to make PHP go fine with HTML?

I have a index.php page Picture : http://i.imgur.com/UBorPdE.png

Website : http://www.vestigedayz.com/sala/clienti/index.php (User: Test123, pass: test)

<?php include("auth.php"); //include auth.php file on all secure pages )

$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'pass';
$conn = mysql_connect("host", "user", "pass");

$conn = mysql_connect($dbhost, $dbuser, $dbpass);


if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}

if (!mysql_select_db("vestiged_sala")) {
echo "Unable to select vestiged_sala: " . mysql_error();
exit;
}

mysql_select_db("vestiged_sala");

$result = mysql_query("select * from users");

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}

while ($row = mysql_fetch_assoc($result)) {
echo $row["Antrenor"];
echo $row["PrimulContract"];
echo $row["ExpiraContract"];
}


mysql_free_result($result);

?>


ok, after that it comes HTML codes. I have this code

<h4>Bun venit, <?php echo $_SESSION['username']; ?> !
<hr>
Abonamentul tau expira pe data de: <?php echo $row["ExpiraContract"]; ?>
<hr>
Primul tau contract a fost facut pe data de : <?php echo $row["PrimulContract"]; ?>
<hr>
Pe data de <?php echo $row["antrenor"]; ?> va trebui sa platesti instructorul !
</h4>


But php won't show those rows. Only [user] row. The others it appears to be blank. When I add while ($row = mysql_fetch_assoc($result)) above of "Abonamentul tau" it gives me errors. What is the solution to my problem ? Because I know is something easy but I'm beginner and I can't figure it out.

Answer

in place of this line

$result = mysql_query("select * from users");

put these lines

$result = mysql_query("SELECT * FROM users WHERE username='$user' LIMIT 1");
while($row = mysql_fetch_array($result)){
            $expire = $row['ExpiraContract'];
            $primul = $row['PrimulContract'];
            $antrenor = $row['Antrenor'];
    }

But remember you have to declare empty variables before these line

$user = $_SESSION['username'];
$expire = '';
$primul = '';
$antrenor = '';

So, your full code is like this

//First define empty variables
$user = $_SESSION['username'];
$expire = '';
$primul = '';
$antrenor = '';

// add these lines in place of $result query
$result = mysql_query("SELECT * FROM users WHERE username='$user' LIMIT 1");
while($row = mysql_fetch_array($result)){
            $expire = $row['ExpiraContract'];
            $primul = $row['PrimulContract'];
            $antrenor = $row['Antrenor'];
    }

This is you html now

Abonamentul tau expira pe data de: <?php echo $expire; ?> 
    <hr>
    Primul tau contract a fost facut pe data de : <?php echo $primul;  ?>
    <hr>
    Pe data de <?php echo $antrenor;  ?>  va trebui sa platesti instructorul !
    </h4>

Try this. If you have any problem then comment it. :)

Remove these lines if you try this first

while ($row = mysql_fetch_assoc($result)) {
    echo $row["Antrenor"];
    echo $row["PrimulContract"];
    echo $row["ExpiraContract"];
}