php variable on separate page not working - php

Can anyone help on this please. It's driving me crazy!
I have on one page:
foreach($images_not_on_server_unique as $img => $missing){
foreach($test as $m => $n){
foreach($n as $o => $p){
$query1 = "SELECT * FROM $p WHERE adv='$missing'";
$result1 = mysqli_query($conn,$query1) or die(mysqli_error());
$numofrows = mysqli_num_rows($result1);
if($numofrows >= '1'){
$row1 = mysqli_fetch_array($result1);
$errors_images++;
}
}
}
}
echo $errors_images;
which correctly prints out '16'.
On another page I include the page, and then echo the variable from the first page like so:
echo "errors images ".$errors_images;
which should give me '16'. However, I get only 'errors images'.
What am I doing wrong. I have used include many, many times before and it has always worked (but maybe not in a foreach loop). I have tried using $GLOBALS, but to no avail.
Many thanks for any help.
EDIT
The full code for the second page
<?php
include("login/include/session.php");
include("dbconnect/index_new.php");
require("errors/q_errors.php");
include_once("errors/q_missing_images.php");
echo "errors images ".$errors_images;
?>
UPDATE:
I have added
$my_test = '555';
to the first page and echoed it in the second page with
echo "my test ". $my_test;
and it works correctly!
Therefore it must have something to do with the foreach function in the first page.

Either you're including the file incorrectly
include_once 'path/to/file.php';
Or you're calling it incorrectly
echo 'errors images'.$errors_images.'';

Related

Transferring array to next page

I found some similar problems here, but none of them does really fit to my problem.
I want to create an array consisting of entries from my database. My code looks like that:
<?php
include ("../script/db_connect.php");
$select_questions = 'select * from users ';
if (isset($_POST["own"]) && $_POST["own"] == "No") {
$select_questions .= 'where creator != '
. $_SESSION["id"];
}
$select_questions .= ' limit '
. $number;
$questions_result = mysqli_query($con, $select_questions);
while ($row = mysqli_fetch_assoc($questions_result)) {
$questions[] = $row;
}
$_SESSION["questions"] = $questions;
mysqli_close($con);
?>
After that, I want to create an array, which will later be saved as a $_SESSION variable. To test my code, there is another page, where I want to echo my array. On the other page, I use the following php code:
<?php echo var_dump($_SESSION["questions"]); ?>
I tried different versions, but it still does not really work. I'm pretty new to PHP, so any help is highly appreciated!
Don't forget to start session by adding this line at the top of your 2 scripts :
<?php
session_start();

My array won't echo a value for some reason

I have a database that holds thousands of structures. The structures are searchable by choosing the "area" first, then selecting the "block_number". My first page allows the user to select the area, the area is then passed through the url to the next page. The next page uses php to pull up the blocks in that area. I'm trying to echo the "area" and "block_number" in the results. The my query works just fine but, for some reason I can't display the "area" in the results. See the code below.
<?
include("conn.php");
include("pl_header.php");
$area = mysql_real_escape_string($_GET['area']);
$wtf = '$area';
?>
<h3>Choose A Block Number in<br> <?=$area?></h3><br>
<center>
<?php
$tblWidth = 1;
$sql = mysql_query("SELECT DISTINCT block_number FROM platform_locations WHERE area='$area'");
$i = 1;
// Check to see if any results were returned
if(mysql_num_rows($sql) > 0){
echo '<div class="redBox extraIndent">';
// Loop through the results
while($row = mysql_fetch_array($sql)){
echo ''. $row['area'] .''. $row['block_number'] .'';
if($i == $tblWidth){
echo '';
$i = 0;
}
$i++;
}
echo '';
}else{
echo '<br>Sorry No Results';
}
?>
</div>
</body>
</html>
My issue is where you see '. $row['area'] .' displays nothing, but the '. $row['block_number'] .' works just fine.
Your query is only selecting block_number.
Try changing:
$sql = mysql_query("SELECT DISTINCT block_number FROM platform_locations WHERE area='$area'");
To:
$sql = mysql_query("SELECT DISTINCT block_number, area FROM platform_locations WHERE area='$area'");
Edit: If you have this issue in the future try var_dump($row); to see what the array contains. This would show you that you only have access to the block_number and not the area.
Double edit: I didn't notice, but the other answer is right about the $area var- you've already got the $area saved, use that variable instead of the return from the DB as it's already in memory. If this could change per record, it'd be prudent to use the record's area variable to make your code more reusable. However, in this particular case, your SQL statement has the area in the where clause, so it wont vary unless you attempt to use portions of this code elsewhere.
Your SQL query is only selecting block_number, so that's the only field that will be in the $row array. You've already got area as a variable $area so use that, not $row['area'].

Echo each record of a query result

I'm attempting to extract some data from a database and echo each result. The code below is code that I took from a textbook and then tried to modify to fit my own website that is hosted locally. I cannot see where I'm going wrong, no error messages are shown, just a blank screen when I run the scrip.
<?php #script 9.4 view top 5 recipients
// This script exctracts data from db and then displays each record in a table
DEFINE('SYSPATH','FOO');
require '../application/config/database.php';
require 'mysqli_connect.php';
$q = "SELECT alert_recipient as NAME
FROM alert
LIMIT 5;
";
$r = mysqli_query($dbc,$q);
// $dbc database connection comes from required mysqli_connect.php
if($r)
{
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo $row['name'];
}
}
else {
echo "<p>ERROR</p>".mysqli_error($dbc);
}
?>
The code looks okay except for your echo $row['name'];, note that you are selecting NAME, uppercase.
Change your echo statement to be:
echo $row['NAME'];
because field names quoted within $row array are case sensitive.
(Can't comment yet)
Maybe the script works but there is no results to display. Check your database.

mysql_fetch_array() not spiting anything

Here is my code:
$campagin_id = $_SESSION['campagin_id_for_camp'];
$query = "SELECT * FROM survey_result where campagin_id = ".$campagin_id;
$conn=mysql_connect($dbconfig['db_hostname'],$dbconfig['db_username'],$dbconfig['db_password']) or die(mysql_error());
mysql_select_db($dbconfig['db_name'],$conn);
$exec_query =mysql_query($query) or die(mysql_error());
$row=mysql_fetch_array($exec_query);
echo "<br> row = ".$row;
while ($row=mysql_fetch_array($exec_query)){
echo "I am In";
}
The Problem is that I am not getting anything in $row I cant get into the while loop, nothing shows up when I try to echo the value of $row, No error Nothing. Can you help me to find a problem in my code ?
Ps : The database is their. I have checked for the query for the corresponding value of $campagin_id. and also when i tried to echo $exec_query it echoed this : Resource id #8
PPS : The database have more than 7 record for each id so it doesn't matter if I call mysql_fetch_array($exec_query) more than once before going in to the while loop. and for the $campagin_id in the session their are many records present in the database.
You have written $row=mysql_fetch_array($exec_query) and then you are echoing something. and you are using the same in while.
Instead of:
$row=mysql_fetch_array($exec_query);
echo "<br> row = ".$row;
while ($row=mysql_fetch_array($exec_query)){
echo "I am In";
}
Use this (as per my knowledge you should not use $row=mysql_fetch_array() once you have used before while):
while ($row=mysql_fetch_array($exec_query)){
echo "I am In";
}
If the query returns Resource id #8 then that means it was successful - ie there were no errors. There were probably no rows returned by that query, so no rows in your table that match the given campagin_id.
You are also calling mysql_fetch_array() twice separately, you shouldn't do that because your while loop will skip the first row because calling this moves the pointer in the result set forward by one.
Also you can't echo an array as you are trying to, if you want to see the contents of an array use print_r() or var_dump().
I suggest adding some code to handle no rows found:
if($exec_query && mysql_num_rows($exec_query) > 0)
{
while ($row=mysql_fetch_array($exec_query)){
echo "Row: " . print_r($row, true);
}
}
else
{
echo 'None found';
}
Try this code.
<?
$campagin_id = $_SESSION['campagin_id_for_camp'];
$query = "SELECT * FROM survey_result where campagin_id = ".$campagin_id;
mysql_connect($dbconfig['db_hostname'],$dbconfig['db_username'],$dbconfig['db_password']) or die(mysql_error());
mysql_select_db($dbconfig['db_name']);
$exec_query =mysql_query($query) or die(mysql_error());
while ($row=mysql_fetch_assoc($exec_query)) {
echo "<br/> row = <pre>".print_r($row)."</pre><br/>";
}
?>

not getting this header to work correctly

I am still working on this and I been trying to add in different things but its not working. When i go to the web site the side is all messed up and I don't know why.Here is my code:
<?php
// Get all the categories and
// link them to category.php.
// Define and execute the query:
$q = 'SELECT category_id, category FROM categories ORDER BY category';
$r = mysqli_query($dbc, $q);
// Fetch the results:
while (list($fcid, $fcat) = mysqli_fetch_array($r, MYSQLI_NUM)) {
// Print as a list item.
echo "<li>$fcat</li>\n";
if($_SERVER['PHP_SELF']!="CART FILE"){
echo "<h1>Cart Contents</h1>";
echo "<div class=\"p2\">";
$itemCount = count($_SESSION['cart']);
foreach($_SESSION['cart'] as X=>X){
for($i=0;$i<count(X);$i++){
$itemCount+=X;
}
}
echo "You have ".$itemCount." total items in your cart.";
echo "</div>\n";
}
} // End of while loop.
When I change the x=>x to $k=>$v nothing happens. I don't understand this at all the count comes up but the sides is all out of whack. Here is the website http://www.elinkswap.com/snorris/header.html I am sure it is something small but I am still a newbie at this.
ok I am editing this maybe for you guys to understand what i am trying to do here it is:
add in how many items are in the cart on the right side that is what this code is suppose to do..
Change this:
foreach($_SESSION['cart'] as X=>X){
for($i=0;$i<count(X);$i++){
$itemCount+=X;
}
}
to this:
foreach($_SESSION['cart'] as $key=>$X)
{
for($i=0;$i<count($X);$i++){
$itemCount+= $X;
}
}
It should work.
Are you sure that your page is interpreted as a php script ? Default extension of the page to be taken into account by the interpreter is .php, maybe .php3 .php5 depending on the version.
Or maybe you're using some url rewriting I don't see.

Categories