PHP MYSQL if field equals checked echo - php

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

Related

How to pass an random variable between php pages using $_SESSION variable?

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"];

Selecting 4 columns from 3 Random Data From PHP & MySQL

so I'm using the RAND() LIMIT 3, but I use it in a loop to get 3 random rows.
I need to use each column in a different place in my site. How can I grab the data without running a different query for each column?
The data I need shouldn't be in order, so it'll be for example:
$Title, will be the title of the page.
$price, will be the price of the service/product.
$description, will be the description of the service/product.
Clearly, they're not close one to another in the php file.
Both answers sound logical, but I'm totally new to mysql and I can't make it work.
This is what I have in my php file:
<?php
mysql_connect("localhost", "test", "test") or die (mysql_error ());
mysql_select_db("test") or die(mysql_error());
$strSQL = "SELECT name FROM UserId ORDER BY RAND() LIMIT 3";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['name'] . "<br />";
}
mysql_close();
?>
What it does is return random 'name' column of 3 rows.
The table has 'id', 'name' and 'Age'.
Your help is really appreciated!
Store them in $_SESSION only if they are not already there, and always access them from $_SESSION.
// On every page that uses these values:
session_start();
if (empty($_SESSION['rand_rows'])) {
// DB connection code omitted...
// This block will execute once per session only...
// Get 3 random rows
$strSQL = "SELECT name, id, age FROM UserId ORDER BY RAND() LIMIT 3";
$rs = mysql_query($strSQL);
if ($rs) {
// Fetch all 3 rows and store them in $_SESSION:
while ($row = mysql_fetch_assoc($rs)) {
$_SESSION['rand_rows'][] = $row;
}
}
}
// Later, get them $_SESSION
// To get the name for example:
foreach ($_SESSION['rand_rows'] as $r) {
echo $r['name'] . "<br />";
}
// Use the same looping pattern to get the id or age where you need them.
It still isn't clear if you actually need this to persist across page loads. If you only need these rows on one single page and can get 3 different rows on other pages or subsequent page loads, there's no need to store into $_SESSION and instead just store them into an array with:
// Array to hold all results
$results = array();
while ($row = mysql_fetch_assoc($rs)) {
$results[] = $row;
}
... and use the same foreach pattern to iterate over $results.
If you put the values into variables, such as $title, $price and $description their values will be remembered across the same file, even when using includes.
If you are trying to save the values across different pages, there are different ways of achieving this, although I would probably recommand using $_SESSION to store such information across pages.
If you are doing as first suggested, but without luck, I will need more information to answer your question properly. A small code sample could help.
Edit:
While the answer by #michael-berkowski is fully functional, you don't necessarily have to use $_SESSION to achieve what you want. Since you stated that you're just learning PHP, I've added a different approach. While not as elegant as the other answer, it is faster, and I have edited variables a little (it is a good habit, to use lowercase table names, the same for variables):
<?php
//Insert DB-connection code
$sql = "SELECT `name` FROM `user` ORDER BY RAND() LIMIT 3";
$rs = mysql_query($sql);
//We should check, if there are actually 3 rows, before doing anything else.
if (mysql_num_rows($rs) == 3) {
$title = mysql_result($rs,0,0);
$price = mysql_result($rs,1,0);
$description = mysql_result($rs,2,0);
}
echo("Title: $title <br/>Price: $price <br/>Description: $description");
?>
Good luck learning PHP.

MySQL PHP Display Text if Value is 1

I am trying to create a PHP script where text is displayed if the MySQL row id=1 has an entry of 1 in the value field and no text is shown if it has a different value. The script I wrote so far doesn't seem to work:
<?php
// connect to the database
include('connect-db.php');
?>
<?php $result = mysql_query("SELECT * FROM Cups WHERE id='1'"); if
($row_queryname['value'] == '1') {?>
Display text here.
<?php }?>
I am quite new at this so any help would be greatly appreciated!
Thanks.
You need to use one of the "fetch" functions to actually get a row after you make a query. Since you haven't specified any field names, I'm going to use mysql_fetch_assoc():
<?php
// connect to the database
include('connect-db.php');
$result = mysql_query("SELECT * FROM Cups WHERE id='1'");
$ar = mysql_fetch_assoc($result);
if ($ar['fieldname'] == '1') // Whatever your field name is
{
?>
Display text here.
<?php
}
?>
mysql_fetch_assoc() gets an associative array that represents a result row, where the array keys are the field names.
Note: This example does not include any error checking.
mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both.
See More about mysql_fetch_array()
<?php
// connect to the database
include('connect-db.php');
$query = mysql_query("SELECT * FROM Cups WHERE id='1'");
$result = mysql_fetch_array($query);
if ($result['value'] == '1')
{
echo "your text here";
}
?>
You're just missing a step or two. Here's a very simple way to do it. Don't forget to check if your query returned any rows, and also remember that mysql_fetch_assoc() returns an array of rows, not just a single row (even if that array only contains a single row).
<?php
// connect to the database
include('connect-db.php');
?>
<?php
$result = mysql_query("SELECT * FROM Cups WHERE id='1'");
if (mysql_num_rows($result) > 0) {
$rows = mysql_fetch_assoc($result);
// $rows is an array of associative arrays
if ($rows[0]['value'] == '1') {
?>
Display text here.
<?php
}
?>

Why does this simple mysql_query/SELECT/echo get "Array" as output?

I have written the code below, which should check the session variable against the database and give me the username which = user id which is in the session.
If I echo $session_name; I get a user id e.g. 30
If I echo $row2; I get Array
What am I doing wrong?
<?php
connect();
$session_name = $_SESSION[id];
$sql2 = "SELECT username FROM members WHERE member_id = '$session_name'";
$result2=mysql_query($sql2);
$row2 = mysql_fetch_array($result2);
echo $row2;
?>
try
echo $row2[0];
or even
print_r($row2);
to see what you are getting from db.
Try echoing $row2[0]. mysql_fetch_array returns one result row as an indexed array, which in this case has only one element/column. Also don't forget to test if $row2 === FALSE, which indicates that there were no results for the query.
<?php
connect();
$session_name = $_SESSION[id];
$sql2 = "SELECT username FROM members WHERE member_id = '$session_name' LIMIT 1";
$result2=mysql_query($sql2);
while($row2 = mysql_fetch_assoc($result2))
echo $row2['username'];
?>
The result of a fetch is always an array. In this case it's a one-dimensional array. Use $row2[0] (assuming your fetchmode is an array).
If your fetchmode is associative or object, it'd be $row2['username'].
Check here for more good info: http://php.net/manual/en/function.mysql-fetch-array.php
$row2 is in fact an array that represents the entire row returned. Try $row2[0], I think that will give you what you expected (the single field in the returned row).
That's because $row2 is an array. You used mysql_fetch_array to obtain it, and it returns an array, as the name of the function implies.
The array returned by mysql_fetch_array uses the column names from your SQL query as the keys. Thus
echo $row2['username'];
will return the data you expect.
As a sidenote: SELECT is technically a SQL clause, not a PHP function.

Mysql database retrieve multiple rows

I use a mysql database. When I run my query I want to be able to put each row that is returned into a new variable. I dont know how to do this.
my current code:
<?php
$result=mysql_query("SELECT * FROM table WHERE var='$var'");
$check_num_rows=mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result))
{
$solution=$row['solution'];
}
?>
The thing is that check num rows can return a row of an integer 0-infinity. If there are more solutions in the database how can I assign them all a variable. The above code works fine for 1 solution, but what if there are more? Thanks.
You can't give each variable a different name, but you can put them all in an array ... if you don't know how this works I suggest looking at a basic tutorial such as http://www.w3schools.com/php/php_arrays.asp as well as my code.
A very simple way (obviously I haven't included mysql_num_rows etc):
$solutions = array()
while($row = mysql_fetch_assoc($result)) {
$solutions[] = $row['solution'];
}
If you have three in your result solutions will be:
$solutions[0] -> first result
$solutions[1] -> second
$solutions[2] -> third
<?php
$result=mysql_query("SELECT * FROM table WHERE var='$var'");
$solution = array();
$check_num_rows=mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result))
{
$solution[]=$row['solution'];
}
?>

Categories