Reputation: 91
Hi I am trying to Select table based upon directory structure but query is not working properly. Pets is main folder inside which there are folders Dogs , Cats , fishes, Horses http://animalswecare.com/Pets/Dogs/
http://animalswecare.com/Pets/Cats/
http://animalswecare.com/Pets/Horses/
I am selecting tables based upon dirctory structure like if page is under Pets>Dogs then table dogs_DB , if same code or page is put in Pets>Cats then table cats_DB I have used echo for checking and output is at the end of code.
<?php
include('../../connectuser.php');
echo $ab=$_SERVER['PHP_SELF'];
$show=explode ("/",$ab);
echo $show[0]."<br />";
echo $show[1]."<br />";
echo $show[2]."<br />";
echo $pagename= $show[1].$show[2];
echo "<br />";
switch ( $pagename ) {
case 'PetsDogs':
$tbl_name = 'dogs_DB';
break;
case 'PetsCats':
$tbl_name = 'cats_DB';
break;
case 'PetsFishes':
$tbl_name = 'fishes_DB';
break;
case 'PetsHorses':
$tbl_name = 'horses_DB';
break;
}
echo $tbl_name;
echo "<br />";
echo $sel = "SELECT * FROM $tbl_name" or die('error in query');
$fetch = mysqli_query( $dbc, $sel );
while ( $row = mysqli_fetch_array( $fetch ) ) {
echo $title;
$title = $row['title'];
}
echo "<br />";
echo $title."hiii";
?>
http://animalswecare.com/Pets/Dogs/get_links.php
Output when page is put under Pets>Dogs folder /Pets/Dogs/get_links.php
Pets
Dogs
PetsDogs
dogs_DB
1
hiii
Upvotes: 0
Views: 176
Reputation: 173
$title
seems to be null at the end of the loop while ( $row = mysqli_fetch_array( $fetch ) )
Upvotes: 0
Reputation: 390
shouldn't these two lines be the other way around?
echo $title;
$title = $row['title'];
so:
while ( $row = mysqli_fetch_array( $fetch ) ) {
$title = $row['title'];
echo $title;
}
echo "<br />";
echo $title."hiii";
Upvotes: 0
Reputation: 353
Initiate a variable like
$pagename = $show[1].$show[2];
NOT
echo $pagename= $show[1].$show[2];
The same for the $title variable inside the while-loop
echo $title;
$title = $row['title'];
Do instead
$title = $row['title'];
echo $title;
Since it looks like you want to use the $title variable outside the loop, you will also need to declare it outside.
$title = "";
while ( $row = mysqli_fetch_array( $fetch ) ) {
$title = $row['title'];
echo $title;
}
echo "<br />";
echo $title."hiii";
Upvotes: 0
Reputation: 2817
It seems that the query row with $row['title'] set to empty string. If it's null, the last echo won't output anything. Try var_dump() to get more information about variable.
Upvotes: 1