PHP not reaching the function - php

This code should return an array containing data from a WAMP server containing some entries about followers. Unfortunately it doesn't. Any help please?
<?php
include('ConnectionManager.php');
function getfollowers()
{
print "check";
//String query
$query="SELECT * FROM `followers` order by 'followerid'";
//execute query
$result=mysql_query($query);
//3mel array isma followers
$followers=array();
//jeeb number of rows
if(mysql_num_rows($result))
{
while($followers=mysql_fetch_assoc($result))
{
$followers[]=$followers;
}
return $followers;
}
}
?>

You're re-declaring $followers inside your loop. Change it to something else
while($row = mysql_fetch_assoc($result)) {
$followers[]=$row;
}

your issue is:
$followers[]=$followers;
you are overriding it.
while($row=mysql_fetch_assoc($result))
{
$followers[]=$row;
}
should fix the issue

Related

How to display SQL result separately

I'm currently making a webpage which is meant to show all it's content from the database. So I made an SQL command which selects the data needed for only 1 particular field on the webpage.
Is it possible to make the SQL command so that it get's all the content for the page at once and that ill still be able to display it separately?
If so, how? Thanks
function dbGet() {
global $conn;
global $return;
$sql = SELECT * FROM testTable;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$return = $row["text"];
return $return;
}
}
else {
echo "0 results";
}
// $conn->close();
}
You can use in this way through which you can identify records has been there or not.
function dbGet() {
global $conn;
// I am not sure what is the datatype of $return here.
// if it's having a predefined format,
// please push the records into $return instead of creating as an array,
// which will be taken care by framework if any you are using.
// global $return;
$return = array('status' => false, records => []);
$sql = "SELECT text FROM testTable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$return['status'] = true;
while($row = $result->fetch_assoc()) {
$return['records'][] = $row["text"];
}
}
// $conn->close();
return json_encode($return);
}
Just echo the results inside while loop instead of returning in whatever format you wish
If you are using php, you can try something like
echo $row["text"];
Hope it helps
Let me know if you require any further help
First you should fix your return code as #AbraCadaver mentioned :
$return[] = $row["text"];
Then you can use foreach in your html :
<?php
foreach($return as $r){
echo $r['text'];
}
?>
I think a Json encoding : json_encode will work well.
Try this: http://php.net/manual/pt_BR/function.json-encode.php

display data from phpmyadmin

I'm a beginner in PHP5 and I'would like to have some help to get the code correct. Check the code and I'will be grateful for your support.
index.php:
if ($tag == 'AfficherMesAnnonce')
{
$Id_Utilisateur= $_GET['Id_Utilisateur'];
$annonce = $db->MesAnnonces($Id_Utilisateur);
if ($annonce)
{
$response["success"] = 1;
$response["annonce"]["Departure_place"] =$annonce["Departure_place"];
$response["annonce"]["Departure_date"] = $annonce["Departure_date"];
$response["annonce"]["Arrival_place"] = $annonce["Arrival_place"];
$response["annonce"]["Path_type"] = $annonce["Path_type"];
echo json_encode($response);
// annonce stored successfully
}else
{
$response["error_msg"] ="No annonce found";
return false;
echo json_encode($response);
}
}
DB_Function.php:
public function MesAnnonces($Id_Utilisateur)
{
$result = mysql_query("SELECT * FROM annonce WHERE Utilisateur_Id_Utilisateur = $Id_Utilisateur") or die(mysql_error());
// check for result
while($row = mysql_fetch_assoc($result))
{
$dd[]=$row;
}
$response=array('annonce'=> $dd);
return $response;
}
and thank you in advance.
You DB function is creating the dd array as a collection of rows, and then nesting that as an element of what's returned. But in your calling script you're assigning
$response["annonce"]["Departure_place"] = $annonce["Departure_place"]
--but the right side of that doesn't exist. You could instead assign:
$response["annonce"]["Departure_place"] = $annonce[0][0]["Departure_place"]
which would be the first row. You're not iterating through the rows in the client script, or using any kind of index to select a row.

sorting list of data in php?

hi i have an list of countries on table so i am sorting with country name then i get A-Z locations like this Afghanistan, Bangladesh,Cape Verde, Denmark,Equatorial Guinea,Falkland Islands,USA, Yemen , Zambiabut actually i need USA on top (i mean first element) like this order USA,Afghanistan, Bangladesh,Cape Verde, Denmark,Equatorial Guinea,Falkland Islands, Yemen , Zambiahere is my code
<?php
require 'dbconnect.php';
$empty=array();
$c=0;
$query="select * from phpfox_country";
$result=mysql_query($query);
if($result)
{
while($row=mysql_fetch_row($result))
{
if($row[1]=="United States")
{
$message=array("country_iso"=>$row[0],"name"=>$row[1])
}
else
{
$message[$c]=array("country_iso"=>$row[0],"name"=>$row[1]);
}
$c++;
}
$message=($message==null)?$empty:$message;
}
else
{
$message=array("message"=>"error in code");
}
echo json_encode($message);
?>
thanks for advance.
Why not just do with mysql query
SELECT * FROM phpfox_country
ORDER BY IF (countryName !='USA',countryName,'' ) ASC
Looks like there are some errors in your code. First, by setting $c to 0 by default and using that as an array index you are assigning the alphabetically first country to that slot, not USA. Worse still, when you actually reach the row with USA on it, you don't add it to the existing array, instead you overwrite the array by assigning a new array.
I suggest that you don't use $c at all. Instead start with an empty array, add USA to the front of the array, and all other countries to the end of the array. As for arranging the records, that is easiest to do with the SQL query. The below code assumes the column with the country name is called name - if it is not, you can fix this yourself.
<?php
require 'dbconnect.php';
// Create empty array
$message=array();
$query="SELECT * FROM phpfox_country ORDER BY name ASC";
$result=mysql_query($query);
if($result)
{
while($row=mysql_fetch_row($result))
{
if($row[1]=="United States")
{
// Add to start of the array
array_unshift($message, array("country_iso"=>$row[0],"name"=>$row[1]));
}
else
{
// Add to end of the array
$message[]=array("country_iso"=>$row[0],"name"=>$row[1]);
}
}
}
else
{
$message=array("message"=>"error in code");
}
echo json_encode($message);
?>
One way is to reserve a place in the beginning of the array:
<?php
require 'dbconnect.php';
$message=array(0 => 'placeholder');
$query="select * from phpfox_country";
$result=mysql_query($query);
if($result)
{
while($row=mysql_fetch_row($result))
{
if($row[1]=="United States")
{
$message[0]=array("country_iso"=>$row[0],"name"=>$row[1])
}
else
{
$message[]=array("country_iso"=>$row[0],"name"=>$row[1]);
}
$c++;
}
}
else
{
$message=array("message"=>"error in code");
}
echo json_encode($message);
?>
Modify your query and code like this:
<?php
require 'dbconnect.php';
$query = "select * from phpfox_country order by <country_name>='USA' DESC,<country_name>";
$result=mysql_query($query);
if($result)
{
$message = array();
while( false!==($row=mysql_fetch_row($result)) )
{
$message[] = array("country_iso"=>$row[0],"name"=>$row[1])
}
}
else
{
$message=array("message"=>"error in code");
}
echo json_encode($message);
?>
But not forget to replace <country_name> with country's name column

php mysql check previous row

I have output from a select query as below
id price valid
1000368 69.95 1
1000369 69.94 0
1000370 69.95 0
now in php I am trying to pass the id 1000369 in function. the funciton can execute only if the valid =1 for id 1000368. if it's not 1 then it will throw error. so if the id passed is 1000370, it will check if valid =1 for 1000369.
how can i check this? I think it is logically possible to do but I am not able to code it i tried using foreach but at the end it always checks the last record 1000370 and so it throws error.
regards
Use a boolean variable:
<?php
$lastValid=false;
while($row = mysql_fetch_array($result))
{
if ($lastValid) {
myFunction();
}
$lastValid = $row['valid'];
}
?>
(Excuse possible errors, have no access to a console at the moment.)
If I understand correctly you want to check the if the previous id is valid.
$prev['valid'] = 0;
foreach($input as $i){
if($prev['valid']){
// Execute function
}
$prev = $i;
}
<?php
$sql = "SELECT * FROM tablename";
$qry = mysql_query($sql);
while($row = mysql_fetch_array($qry))
{
if ($row['valid'] == 1)
{
// do some actions
}
}
?>
I really really recommend walking through some tutorials. This is basic stuff man.
Here is how to request a specific record:
//This is to inspect a specific record
$id = '1000369'; //**some specified value**
$sql = "SELECT * FROM data_tbl WHERE id = $id";
$data = mysql_fetch_assoc(mysql_query($sql));
$valid = $data['valid'];
if ($valid == 1)
//Do this
else
//Do that
And here is how to loop through all the records and check each.
//This is to loop through all of it.
$sql = "SELECT * FROM data_tbl";
$res = mysql_query($sql);
$previous_row = null;
while ($row = mysql_fetch_assoc($res))
{
some_action($row, $previous_row);
$previous_row = $row; //At the end of the call the current row becomes the previous one. This way you can refer to it in the next iteration through the loop
}
function some_action($data, $previous_data)
{
if (!empty($previous_data) && $condition_is_met)
{
//Use previous data
$valid = $previous_data['valid'];
}
else
{
//Use data
$valid = $data['valid'];
}
if ($valid == 1)
{
//Do the valid thing
}
else
{
//Do the not valid thing
}
//Do whatever
}
Here are some links to some good tutorials:
http://www.phpfreaks.com/tutorials
http://php.net/manual/en/tutorial.php

function and class to show image not working in php

i am trying to get get the following working nothing is happen when i use the function i am trying to get it to display images
class ItemRes {
//items DB
var $img="";
}
function ShowItemImage($index,$res_a){
if(sizeof($res_a) > $index){
if($res_a[$index] != NULL) {
$cimg = $res_a[$index]->img;
return "<img src='$cimg' width='70' height='70' style='cursor:pointer'></img>";
}
}else{
return "<center class='whitetxt'><strong>Empty</strong></center>";
}
}
$res_array = array();
$idx=0;
$result21 = mysql_query("SELECT * FROM photos WHERE eid='$eid' ORDER BY id DESC") or die (mysql_error());
while ($row21 = mysql_fetch_array($result21)) {
$img_path = $row21['path'];
$obj = new ItemRes();
$obj->img = $img_path;
$res_array[$idx] = $obj;
$idx++;
}
ShowItemImage(0,$res_array)
ShowItemImage(1,$res_array)
Thhere are many issues with this code, so I'll just take them one at the time.
Assuming you initiate the database connection somewhere else the first issue is
the line:
$result21 = mysql_query("SELECT * FROM photos WHERE eid='$eid' ORDER BY id DESC") or die (mysql_error());
Prior to this line you do not set the variable $eid and thus this will only fetch items with eid = ''
Second:
You do not end the last two rows with a semicolon, thus this will produce a fatal error.
Third:
Probably the reason to why you get 'nothing is happen' (wich I interpet as a blank page)
In your function ShowItemImage you return strings but you do nothing with them.
You need to change this:
ShowItemImage(0,$res_array)
ShowItemImage(1,$res_array)
to:
echo ShowItemImage(0,$res_array);
echo ShowItemImage(1,$res_array);
and you will probably start noticing some things on the screen.

Categories