PDA

View Full Version : Retrieving and displaying data from a database



Snav3
21 Mar 2012, 9:08 PM
Hi have been trying for the past few days to retrieve data from my database so far the database connections working perfectly as i can post data and store it in the database but displaying that data back has been giving be a great deal of trouble.
Am using php for the server end made a connection class. made a query on o home.php file that request for the display of items from a particular table. The problem is calling calling that particular variable from php to my app.js file here's my example code please help me out;
this is the home.php code
<?php
include_once "connect.php";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>home</title>
</head>

<body>

<?php
$sql="SELECT url FROM products WHERE id=1";
$db= new dbClass($sql);
$db->executeSQL($sql);

$db->getNextRecord();
$url=$db->getField("url");
?>
//have been trying to assign the php variable to a javascript variable so as to call in in the app.js

<script language="javascript" type="text/javascript">
var image = "<img src='<?php echo $url ?>'/>";
</script>

</body>
</html>

Here's the app.js code that holds the home page
// This is the home page, just some simple html

{

title: 'Home',
iconCls: 'home',
cls: 'home',
url:'home.php',


items: [

{
xtype: 'image',
url:'home.php',
style: 'background-color: #5E99CC',

// As seen from above i included the home.php page to try and get the variable i created based on a success function, can this work or am i completely off track, if so please help me out?
success: function() {

document.write(image);
}

},

]
}

sachintaware
21 Mar 2012, 10:59 PM
As a fore word I would request you to post your code wrapped inside code tag in the editor.These would help the forum members to understand your problem better.

As far as your problem goes(I am not sure if this would help),if you want to fetch data from database,you will need to build a JSON or XML for you store to get it.
This is a simple example of it


<?php
$host="localhost";
$uname="";
$pass="";
$connection= mysql_connect ($host, $uname, $pass); //Function to connect to the database,takes in 3 parameters(hostname,username,password)
//echo($host);echo($uname);echo($pass);
//exit;
$selectDB=mysql_select_db(user_register,$connection); // Function to select database name.
//echo $selectDB;

function FetchAll($rowObj) //function to fetch all the rows from the database.
{
$result= array();
$i= 0;
while($rows = mysql_fetch_object($rowObj))
{
foreach($rows as $k => $v)
{
$result[$i][$k]= $v;
}
$i++;
}
return $result;
}


?>

<?php
/*echo "<pre>";
print_r($_POST);
echo "</pre>";*/

$username = $_POST['username'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$state = $_POST['state'];
$city = $_POST['city'];
$country = $_POST['country'];

$sql1="Insert into register(username,password,firstname,lastname,email,state,city,country) values ('$username','$password','$firstname','$lastname','$email','$state','$city','$country')";

$result=mysql_query($sql1);

//echo $result;
//exit;
echo "<h1><center><div style='color:red;'>Form Submitted sucessfully</div></center></h1> "
//echo "<a href='grid.html'>View Grid</a>";

?>

<?php
$sql2="select * from register";
$result2=mysql_query($sql2);

//$output=mysql_fetch_assoc($result2);
//$output=FetchAll($result2);
/*echo "<pre>";
print_r($output);
exit;*/

for ($i=0; $row = mysql_fetch_array($result2); $i++)
{
$return[$i]['username'] = $row['username'];
$return[$i]['firstname'] = $row['firstname'];
$return[$i]['lastname'] = $row['lastname'];
$return[$i]['email'] = $row['email'];
$return[$i]['state'] = $row['state'];
$return[$i]['city'] = $row['city'];
$return[$i]['country'] = $row['country'];
}

echo json_encode($return); //This returns a JSON array to your js.In your case app.js i suppose

?>


Also,the documentation has few good examples.
this (http://docs.sencha.com/ext-js/4-0/#%21/guide/direct_grid_pt1) is one of them with DB implementation.

Happy Coding