I am trying to use variables(dates) queried in a php page then placed in $_SESSION, in another page to perform another query.
I will only use one date from that session array. which is that one i clicked on its link tag.
Is there an action that should be performed onclick of that link?
Here is my php, first page it creates the $_session variables and create the links.
The second page it is supposed to print the value i clicked. but it does not.
oldentries.php:
<?php
$query = "SELECT DISTINCT Tdate FROM titletable";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0 )
{
while($row = mysqli_fetch_assoc($result))
{
$_SESSION[$row["Tdate"]]= $row["Tdate"];
echo ''.$_SESSION[$row["Tdate"]].'<br>';
}
}
else
{
echo "No Results in the database!";
}
?>
content.php
<?php
$datetable = $_SESSION[$row["Tdate"]];
echo "$datetable";
$query = "SELECT table_name FROM information_schema.tables where table_schema='council_db'";
?>
Your $_SESSION array is keyed by the result of the SQL query. In your content.php the $row variable is not available. In cases like this it's probably better to pass variables between pages with a query parameter rather than the session.
...
while($row = mysqli_fetch_assoc($result))
{
echo ''.$row["Tdate"].'<br>';
}
...
Then in content.php you can access it with $_GET
$datetable = $_GET["Tdate"];
Related
I am trying to store the result from a MySQL statement for later use in PHP. I have this code which gets me the result:
// Get the categories from the db.
$categories = array();
$catSql = "SELECT id, name FROM categories";
if ($catStmt = mysqli_prepare($db, $catSql))
{
$catStmt->execute();
$result = $catStmt->get_result();
// Fetch the result variables.
while ($row = $result->fetch_assoc())
{
// Store the results for later use.
}
}
So I know i will have the results in $row["id"] and $row["name"] and I want to save all of the rows so whenever i need them i can loop through them and for example echo them. I have searched for structures and arrays for keeping them in PHP but I cannot seem to find any information about that or maybe I am not searching in the right direction. Can anyone point me where i should read about this to find out how to do this efficiently and if possible post a small example?
Use sessions:
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Get the categories from the db.
$categories = array();
$catSql = "SELECT id, name FROM categories";
if ($catStmt = mysqli_prepare($db, $catSql))
{
$catStmt->execute();
$result = $catStmt->get_result();
// Fetch the result variables.
while ($row = $result->fetch_assoc())
{
// Store the results for later use.
$_SESSION['category_' . $row['id']] = $row['name'];
}
}
Then access it later from a different page
$_SESSION['session_variable_name']
You can also create an array of the information and store the entire array in a single session variable.
Just make sure you add the session_start function at the beginning of each page. The if statement prevents you from trying to start it multiple times.
$categories = array();
$catSql = "SELECT id, name FROM categories";
if ($catStmt = mysqli_prepare($db, $catSql))
{
$catStmt->execute();
$result = $catStmt->get_result();
while ($row = $result->fetch_assoc())
{
$categories[$row['id']]=$row['name'];
}
}
And If you want the name anywhere use below :
$categories[$id]
I was facing like id overwriting problem when I tried to pass:
$_session['ads_id'] = $row['ads_id'];
to the next page using:
echo $_session['ads_id'];
I then define an array like:
$session['ads_id] = array();
and
$session['ads_id'][] = $row['ads_id'];
When I echo it like:
$ads_id = implode($_SESSION["ads"]);
so it was showing all the ads_id like 112,113,114 and not specific linked with user_id.
Then I defined a foreach loop like:
foreach($_SESSION['ads'] as $ads_id) {
}
Now I don't know how to pass it to the next page? If someone clicks ads_id 113 from the table and it shows only 113 and not other ids.
$sql = "SELECT * FROM ads where user_id ='{$_SESSION[ "user_id" ]}'";
$result = $conn->query($sql);
$ads_id = array();
if ($result->num_rows > 0) {
$_SESSION['ads'] = array();
while($row = $result->fetch_assoc()) {
$_SESSION['ads'][]= $row['ads_id'];
foreach($_SESSION['ads'] as $ads_id) {
}
echo $ads_id //// Here I don't want to echo it, I want to pass to the next page
I want to pass the $ads_id to update.php from here.
I tried like this on update.php which is not working.
SESSION_START();
echo $ads_id ;
echo $_SESSION['ads_id'];
Your $_SESSION variable will be accessible in all your other scripts as long as all other scripts starts with session_start().
<?php
session_start();
//...rest of the codes
Okay so I am trying to create a custom function that will echo a site url inside an iframe for the end user.
The script has to check whether or not the user has already seen the site and if they have seen it don't display it any more, but take another site url from the database etc.
Here's what I have come up with so far:
function get_urls() {
require 'config.php';
global $con;
global $currentUsername;
$con = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname);
$query = "SELECT site_url FROM sites WHERE site_url IS NOT NULL";
$result = mysqli_query($con, $query);
// Get all the site urls into one array
$siteUrls = array();
$index = 0;
while($row = mysqli_fetch_assoc($result)) {
$siteUrls[$index] = $row;
$index++;
}
$query2 = "SELECT site_url FROM views WHERE user = '$currentUsername' AND site_url IS NOT NULL";
$result2 = mysqli_query($con, $query2);
// Get urls the user has already seen into another array
$seenUrls = array();
$index = 0;
while($row2 = mysqli_fetch_assoc($result2)) {
$seenUrls[$index] = $row2;
$index++;
}
// Compare the two arrays and create yet another array of urls to actually show
$urlsToShow = array_diff($siteUrls, $seenUrls);
if (!empty($urlsToShow)) {
// Echo the url to show for the iframe within browse.php and add an entry to the database that the user has seen this site
foreach ($urlsToShow as $urlToShow) {
echo $urlToShow;
$query = "INSERT INTO views VALUES ('', '$currentUsername', '$urlToShow')";
mysqli_query($con, $query);
break;
}
}
// Show the allSeen file when all the ads are seen
else {echo 'includes/allSeen.php';}
mysqli_free_result($result);
mysqli_close($con);
}
I have currently found two errors with this. First the $siteUrls and $seenUrls are all okay, but when I compare the two using array_diff then it returns an empty array.
Secondly the script doesn't write the site url into the database because the $urlToShow is an array not a single url?
I think the problem is in your code is at the place where you are creating your $siteUrls, $seenUrls arrays. mysqli_fetch_assoc() function will give you a result row as an associative array. So if you want to change some of your code in the while loops.
Please chnage this
while($row = mysqli_fetch_assoc($result)) {
$siteUrls[$index] = $row;
$index++;
}
To
while($row = mysqli_fetch_assoc($result)) {
$siteUrls[$index] = $row['site_url'];
$index++;
}
And in the second while loop also. Change this
while($row2 = mysqli_fetch_assoc($result2)) {
$seenUrls[$index] = $row2;
$index++;
}
To
while($row2 = mysqli_fetch_assoc($result2)) {
$seenUrls[$index] = $row2['site_url'];
$index++;
}
and try
i would not run two queries and try to merge the result array. you can use mysql itself to just return the sites that have not been seen yet:
SELECT sites.site_url FROM sites
LEFT JOIN views ON views.site_url=sites.site_url AND views.user='$currentUsername'
WHERE sites.site_url IS NOT NULL AND views.site_url IS NULL
This will return only site_urls from sites, that have no entry in the views table. The LEFT JOIN will join the two tables and for every non-matching row there will be NULL values in the views.site_url, so that is why i am checking for IS NULL.
Your saving of $urlToShow should work, if you set to $row field content and not $row itself as suggested, but if you want to check what is in the variable, don't use echo use this:
print_r($urlToShow);
If the variable is an array, you will see it's content then.
#Azeez Kallayi - you don't need to index array manually.
$seenUrls[] = $row2['site_url'];
In addition, you can fetch all result
$rows = mysqli_fetch_all($result2,MYSQLI_ASSOC);
foreach($rows as $row){
echo $row['site_url'];
}
Each user in the databased is assigned a level, their is a table for levels and each level has permissions eg "view-clients" at login the user's level is made into $_SESSION['level'] i need to make an if statement that will echo code if the user's level "view-clients" is "checked".
This is the code is have but it wont echo it even when it is checked.
<?php
$level = mysql_real_escape_string($_SESSION['level']);
include("include/config.php");
$con=mysql_connect($db_host,$db_uname,$db_pwd,$db_name);
mysql_select_db($db_name);
$query = "SELECT * FROM levels WHERE name='$level'";
$result=mysql_query($query);
while($rows = mysql_fetch_array($result)){
$a1=$rows['0'];
$a2=$rows['1'];
$a3=$rows['2'];
$a4=$rows['3'];
$a5=$rows['4'];
$a6=$rows['5'];
$a7=$rows['6'];
$a8=$rows['7'];
if ($a5=="checked") {
echo "Clients";
}
}
?>
I have tried a few things such as not using a variable as the session.
I am thankful for any help.
I guess you forgot to start session.
try that
<?php
session_start();
$level = mysql_real_escape_string($_SESSION['level']);
....
your query will not find and result while $level is not defined.
also chanage this
mysql_fetch_array
to
mysql_fetch_row
Look here the difference betwen fetch row and fetch array and fetch assoc
I want to pass a variable which contains a query, to another php page.
I have variable as follows.
$search_type = $_POST['search_type'];
$search_brand = $_POST['search_brand'];
$result = "SELECT * FROM vehicle_info WHERE type='".$search_type."' AND brand='".$search_brand;
Then I create a link to a different page, I want to pass the $result variable to that page.
<?php echo "<a href='$secondpage.php?data=$result'>Click Here</a>"; ?>
But when I click this link, the whole data doesn't carry out with the URL. When I click the link, my URL was like following
.../secondpage.php?data=SELECT%20*%20FROM%20vehicle_info%20WHERE%20type=
The URL doesn't contain the variables ($search_type and $search_brand)
So how can I make this possible? Please help me !
You can use $_SESSION to store the query or you can encode the string using json-encode function and pass it in link
$search_type = $_POST['search_type'];
$search_brand = $_POST['search_brand'];
$result = mysql_query("SELECT * FROM vehicle_info WHERE type='".$search_type."' AND brand='".$search_brand) or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
extract($row);
echo "<a href='secondpage.php?searchtype=$type&searchbrand=$brand'>Click Here</a>";
}
All of that is assuming your secondpage.php takes in the values searchtype and searchbrand to display the results. The problem was, you did not fetch the results, what you were displaying was the query itself.