Can't echo data from mysql database php -


for reason can't echo , data tables.

this connect database "functions.php"

<?php  function connect() {      mysql_connect("localhost","username","password");     mysql_select_db("dbname"); }  function protect($string) {     return mysql_real_escape_string(strip_tags(addslashes($string))); }  ?> 

i include in header

<?php  include("functions.php"); connect();  ?> <?php  if(isset($_session['uid'])) {     include("safe.php");     ?>      &raquo; <a href="main.php">your stats</a>     &raquo; <a href="rankings.php">battle</a>     &raquo; <a href="explore.php">explore</a>     &raquo; <a href="gym.php">gym</a>     &raquo; <a href="logout.php">logout</a>           <?php  } else {      ?>     <form action="login.php" method="post">     username <input type="text" name="username">     password <input type="password" name="password">     <input type="submit" name="login" value="login">      </form>     <?php  }  ?> 

and main.php i'm trying echo data.

<?php   session_start(); include ("header.php");   if(!isset($_session['uid'])) {      echo "you must logged in view page";  } else {      ?>      <center><h1>your stats</h1></center>     <br /> <br />     <table cellpadding="3" cellspacing="5">         <tr>             <td>username:</td>             <td><i><?php echo $user['username']; ?></i></td>         </tr>          <tr>             <td>level:</td>             <td><i><?php echo $stats['level']; ?></i></td>         </tr>           <tr>             <td>attack:</td>             <td><i><?php echo $stats['attack']; ?></i></td>         </tr>            <tr>             <td>defence:</td>             <td><i><?php echo $stats['defence']; ?></i></td>         </tr>         <tr>             <td>strength:</td>             <td><i><?php echo $stats['strength']; ?></i></td>         </tr>         <tr>             <td>will:</td>             <td><i><?php echo $stats['will']; ?></i></td>         </tr>         <tr>             <td>cash:</td>             <td><i><?php echo $stats['cash']; ?></i></td>         </tr>      </table>      <?php      }  ?> 

for reason echo parts won't work , have no idea why.

try using pdo following code connect database:

<?php try  {     $pdo=new pdo('mysql:host=localhostorhostname;dbname=yourdatabasename','yourusername','yourpassword');     $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception); } catch(pdoexception $e) {     echo $e->getmessage(); } ?> 

then can use above connection query data database this:

<?php     $ab = "select * users id='".$_session['uid']."'";     $rs=$pdo->prepare($ab);     $rs->execute();     $result = $rs->fetch(pdo::fetch_assoc); ?> 

the results fetched in $result can accessed follows

<?php      echo $result['column_name1'];      echo $result['column_name2']; // , on... ?> 

Comments