I got two files name func.inc.php and profile.php
I'm trying to fetch the data from the mysql by creating a function get_data($id) in func.inc.php and display in the profile page(profile.php).But the data is not displaying.
<-- func.inc.php file -->
<?php session_start();
function get_data($id){
$query_in="SELECT * FROM user WHERE id ='$id'";
$query=mysql_query($query_in);
while($row=mysql_fetch_assoc($query)){
$name=$row['name'];
$book=$row['book'];
$mobile=$row['mobile'];
$computer=$row['computer'];
}
}
?>
<-- profile.php -->
<?
include'func.inc.php';
echo "Name: ".$name;
echo "Book: ".$book;?>
The function should return some value then only the values from the function will be got
function get_data($id){
$query_in= sprintf("SELECT * FROM user WHERE id ='%d'", mysql_real_escape_string($id));
$query=mysql_query($query_in);
$result = array();
while($row=mysql_fetch_assoc($query)){
$result[]['name'] = $row['name'];
}
return $result;
}
<-- profile.php -->
$values = get_data($id);
print_R($values);
You should pass the id from the function call that is: here you should call like this:
$values = get_data($id);
while($row=mysql_fetch_array($values)){
echo $name=$row['name']."<br>";
echo $book=$row['book']."<br>";
echo $mobile=$row['mobile']."<br>";
echo $computer=$row['computer']."<br>";
}
in func.inc.php use this code:
function get_data($id){
$query_in="SELECT * FROM user WHERE id ='$id'";
$query=mysql_query($query_in);
return $query;
}
Related
I am trying to send two array over session and then combined them to display together but nothing displayed.This is my code--->
<?php
session_start();
function tee($uid){
include 'connect.php';
$uid=$uid;
$parent=array();
$child=array();
$_SESSION["child"]=array();
$_SESSION["parent"]=array();
$sql="select id from relation2 where parentID=$uid";
$result=mysql_query($sql,$link);
while($row=mysql_fetch_assoc($result))
{
$parent[]=$uid;
$child[]=$row["id"];
$_SESSION["child"][]=$child;
$_SESSION["parent"][]=$parent;
tee($row["id"]);
}
}
tee(2);
foreach(array_combine($_SESSION["child"],$_SESSION["parent"]) as $child=>$parent)
{
echo $child.'----------->'.$parent;
echo '<br>';
}
?>
I replaced the the query output with something custom and this code worked for me
<?php
session_start();
function tee($uid){
include 'connect.php';
$parent=array();
$child=array();
$_SESSION["child"]=array();
$_SESSION["parent"]=array();
$sql="select id from relation2 where parentID=$uid";
$result=mysqli_query($sql,$link);
while($row=mysqli_fetch_assoc($result))
{
$_SESSION["child"][]=$row["id"];
$_SESSION["parent"][]=$uid;
}
tee(2);
foreach(array_combine($_SESSION["child"],$_SESSION["parent"]) as $child=>$parent)
{
echo $child.'----------->'.$parent;
echo '<br>';
}
?>
Also check the extract() function
If you pass $uid from user input use PDO
system/article.php
<?php
$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["articleTitle"];
echo $row["articleSummary"];
echo $row["articleContent"];
}
} else {
echo "0 results";
}
include 'template/homepage.php';
retrieves articles from the article table.
I have included the homepage.php which is supposed to act as a template.
template/homepage.php
<?php include 'template/common/header.php'; ?>
<h1>Article Title here</h1>
<p>articleSummary</p>
<?php include 'template/common/footer.php'; ?>
How do I now pass the retrieved data to the homepage.php to display it on the browser ?
Edit
smarber pointed me to
In the first file:
global $variable;
$variable = "apple";
include('second.php');
In the second file:
echo $variable;
which works. But how do I implement the same with my problem up top?
You may do that via GET, Session or Post; But why don't you simply and efficiently define a function and pass those variables to it, just for example:
function displayArticle($title, $summary, $content) {
displayHeader(); // maybe some concepts you've used in template/common/header.php
echo "<h1>$title</h1><p>$summary</p><div>$content</div>";
displayFooter(); // again, what you've provided in footer.php
}
Well then, you may do the following:
change the template/homepage.php file to:
<?php
include 'template/common/header.php';
echo "<h1>$articleName</h1>";
echo "<p>$articleSummary</p>";
include 'template/common/footer.php';
?>
and change the system/article.php to:
<?php
global $articleName;
global $articleSummary;
global $articleContents;
$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$articleName = $row["articleTitle"];
$articleSummary = $row["articleSummary"];
$articleContents = $row["articleContent"];
include 'template/homepage.php';
}
} else {
echo "0 results";
}
However, It's so better to create a cleaner and more reusable code using some facilities you have in the programming language, like using functions and classes :)
Hello at the moment I'm using a PHP file called select_Type.php that contain a MySQL query to create a <select></select> Box into HTML. I'm using include_once to link my admin.php and ./Includes/select_Type.php. I already created a file called ./Includes/functions.php where all my querys should be set in PHP functions.
I would like to learn more about functions.
Admin page admin.php where the <select></select> is :
<?php
session_start();
include_once './Includes/functions.php';
CheckLogIn('Superadmin');
SelectType();
//require_once './Includes/select_Type.php';
require_once './Includes/register.php';
?>
select_type.php :
<?php
require_once 'functions.php';
$querySQL = "SELECT * FROM tbltype";
$queryResult = GetQuery($querySQL);
while ($row = mysqli_fetch_assoc($queryResult)){
$dataArrayType[] = $row;
}
?>
What I tried:
<?php
function SelectType() {
GLOBAL $_POST;
$querySQL = "SELECT * FROM tblType";
$queryResult=GetQuery($querySQL);
while ($row = mysqli_fetch_assoc($queryResult)) {
$dataArrayType[] = $row;
}
}
?>
What am I doing wrong ?
Thanks in advance :)
Thank you all especially Déjà vu !!!!!
You were all very helpfull.
The problem was, how most of you told me: I didn't returned the value.
Before:
<?php
function SelectType() {
GLOBAL $_POST;
$querySQL = "SELECT * FROM tblType";
$queryResult=GetQuery($querySQL);
while ($row = mysqli_fetch_assoc($queryResult)) {
$dataArrayType[] = $row;
}
}
?>
Now:
I deleted the GLOBAL $_POST becauce $_POST is already GLOBAL.(Thanks to ɴ ᴀ ᴛ ʜ)
<?php
function SelectType() {
$querySQL = "SELECT * FROM tblType";
$queryResult=GetQuery($querySQL);
while ($row = mysqli_fetch_assoc($queryResult)) {
$dataArrayType[] = $row;
}
return $dataArrayType;
}
?>
admin.php
I put my function SelectType() in my foreach. Et voila!
<select type="text" id="register_type" name="register_type" required>
<?php
foreach (SelectType() as $row) {
echo "<option value='" . $row['idType'] . "'>" . $row['dtType'] . '</option>';
}
?>
</select>
You can use this:
function sel($table,$field="*", $condition="1",$sort="" ){
if($sort!='') $sort="order by $sort ";
//echo "select $field from $table where $condition $sort ";
$sel_query=mysql_query("select $field from $table where $condition $sort ");
//$sel_result=array();
while($temp_res=#mysql_fetch_array($sel_query))
{
$sel_result[]=$temp_res;
}
return isset($sel_result)?$sel_result: 0;
}
And get result:
$temp_res=sel("post","*"," userid ='".$frnd['friend']."' ORDER BY id DESC");
if($temp_res)foreach($temp_res as $row){
echo $row['content'];
}
I used the following code to to get list of users facebook friends and ccheck it against users in an app database. This code would return the users of the app, who are Facebook friends of the user.
$friends_set = '(';
foreach($friends["data"] as $value) {
$friends_set .= $value['id'].',';
}
$new_set = preg_replace('/,$/',')',$friends_set);
$res = mysql_query("SELECT * from user AS u, upload AS up WHERE u.fb_id IN $new_set AND u.fb_id=up.user_id") or die(mysql_error());
while($row = mysql_fetch_array($res)) {
echo $row['fb_id']. "". $row['first_name'];
echo "<br>";
}
$data['top_friends']=$res;
$this->load->view('myview');
This code works. It is in a controller of my codeigniter application and it successfully echos the correct data onto the page.
However now I need to print the result of the query in a for each statement in my view like this:
<?php foreach ($top_friends as $me) : ?>
<div >
<p><?php echo $me['first_name']; ?></p>
<?php endforeach; ?>
However when I try getting the query results in the view using the for each it doesn't work.
How do i fix this?
You could try it the codeignitor way, Create a model function say get_top_friends and i assume that you are passing a comma separated string as argument like $fb_id = '45,65,78,89'. Say facebook_model is the name of the model then :
class Facebook_model extends CI_Model{
//other functions and constrcutor here
//function to get the top friends
function get_top_friends($fb_id){
$fbId = explode(",",$fb_id)
$this->db->select('*');
$this->db->where_in('fb_id',$fbId);
$this->db->order_by('points','desc');
$this->db->limit(10);
$query = $this->db->get('user');
if ($query->num_rows() < 1) return FALSE;
return $query->result_array();
}
}
And make change in your code as below:
$friends_set = '';
foreach($friends["data"] as $value) {
$friends_set .= $value['id'].',';
}
$new_set = preg_replace('/,$/',')',$friends_set);
$res = $this->facebook_model->get_top_friends($new_set);
$data['top_friends']=$res;
$this->load->view('myview',$data);
And now in view you can try
foreach ($top_friends as $me){
echo $me['first_name'];
}
[Updated for user ]
If you want to do it as in your question : then try,
$result = array();
while($row = mysql_fetch_array($res)) {
$result[] = $row;
}
$data['top_friends']=$result;
$this->load->view('myview',$data);//pass data to view
EDIT:
PROBLEM IS FOLLOWING:
$pageTitle cannot be used outside the function.
Here is what I got so far:
function getMetaData($table, $rows){
echo $table;
echo $rows;
$selectTitle = "select * from $table";
$getTitle = mysql_query($selectTitle);
while ($showTitle = mysql_fetch_assoc($getTitle)){
$pageTitle = $showTitle[$rows];
}
}
getMetaData('metadata', 'Pagetitle');
My output
<?php echo $pageTitle ?>
--> this is undefined
Thank you
Your function does not return any value nor does it prints any value. based on your EDIT use
global $pageTitle;
at the beginning of your function and before using the variable.
You should initialize the variable $pageTitle first like,
function getMetaData($table, $rows){
echo $table;
echo $rows;
$selectTitle = "select * from $table";
$getTitle = mysql_query($selectTitle);
$pageTitle='';
while ($showTitle = mysql_fetch_assoc($getTitle)){
$pageTitle = $showTitle[$rows];
}
return $pageTitle; // returning the variable will work here
}
echo getMetaData('metadata', 'Pagetitle');
Just TRY with mysql_free_result() like
$selectTitle = "SELECT * FROM '".$table."'";
$getTitle = mysql_query($selectTitle);
$pageTitle = '';
while ($showTitle = mysql_fetch_assoc($getTitle)){
$pageTitle = $showTitle[$rows];
}
return $pageTitle; //Return even the pageTitle.
mysql_free_result() will free all memory associated with the result.
And as per your EDIT try like :
$pageTitle = getMetaData('metadata', 'Pagetitle');
echo $pageTitle;