MySQL PHP Display Text if Value is 1 - php

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
}
?>

Related

SQL OR statement does not work in PHP

I want to get some elements from a database (phpmyadmin). The database "top" is set up like:
ID || Name
____________
1 || Home
2 || About
3 || Users
4 || Admin
...
I use the following Code to get the information:
<?php
$sql = "SELECT ID
FROM top
WHERE Name='Users' OR Name='Admin'";
$query = mysqli_query($dbconnect, $sql);
$oq = mysqli_fetch_assoc($query);
if(in_array($_GET['ID'], $oq)){
//Execute some Code
}
?>
If I execute the sql-code I get the result I want (ID 3 and 4) but in "$oq" there is only one element (the first one -> 3) left. Therefore the Code I want to execute is only displayed once.
You have to use a while loop:
$oq = array();
while($row = mysqli_fetch_assoc($query)) {
echo $row['ID'];
$oq[] = $row['ID'];
}
As you have done it you're only fetching one row in $oq. You will have to add each value to the array $oq in order to use in_array() for the test.
There are some other techniques you can use here, for instance you could fetch everything (an array of arrays) and loop through the array, depending on your needs.
You will have to use array to use have multiple DB values
$values=array();
while($row = mysql_fetch_array($result)){
$values = array('ID'=>$row['ID']);
}

PHP MySQL Database - All rows into Multi-Dimensional Array

It's been a long while since I touched PHP so, I just need a refresher of sorts.
I have an HTML form that captures several lines of data (dataA, dataB, dataC & dataD) and inserts them into a database table (dataAll). I will be entering rows upon rows of data into "dataAll". What I'm looking to do is create a display.php page, where the code will take all of the data and place each cell into an array, or the row of an array, for example:
new Array = [["dataA", "dataA", "dataA", "dataA", "dataA"],
["dataB", "dataB", "dataB", "dataB", "dataB"],
["dataC", "dataC", "dataC", "dataC", "dataC"],
["dataD", "dataD", "dataD", "dataD", "dataD"]];
But I cannot remember the syntax on how to perform this task. Any help would be greatly appreciated.
The database is named 'compdata', the table is 'dataAll', and each row is 'dataA', 'dataB', 'dataC', 'dataD'.
Please let me know if I need to supply more information.
Since you asked for All rows, so the simple code for query is written below:
<?php
//after connection to mysql db using mysql_connect()
$sql = "Select dataA, dataB, dataC, dataD from `compdata`.`dataAll`" ;
$result = mysql_query($sql) ;
if(mysql_num_rows($result) > 0 ){
while($row = mysql_fetch_array($result)){
$dataArray[] = $row ;
}
}
echo '<pre>';
print_r($dataArray) ;//you got the desired 2D array with all results
?>
Assuming you are used to the old mysql_xxx functions:
$data = array ();
$result = mysql_query ('select * from dataAll');
while ($row = mysql_fetch_array ($restult, MYSQL_ASSOC))
$data [] = $row;
Result:
$data = array (
0=>array ('col_1'=>'dataA', 'col_2'=>dataB...),
1=>array ('col_1'=>'dataA', 'col_2'=>dataB...)
);
If you only want the numbers and not the column names, use MYSQL_NUM.
However, mysql functions are being replaced with the more generic PDO object so you might want to look into that.
$stmt = $pdo->query ('select * from dataAll');
$result = $pdo->fetchAll (PDO::FETCH_ASSOC); // Or PDO::FECTCH_NUM
Same results as above.

PHP MYSQL if field equals checked echo

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

How can I get and display all values of a certain field in a sql table?

I have a table wherein I need to get all the data in one column/field, but I can't seem to make it work with the code I have below:
$con=mysqli_connect("localhost","root","","database");
$result = mysqli_query($con,"select * from client");
$row = mysqli_fetch_array($result111);
echo $row['name'];
With the code above, it only prints one statement, which happens to be the first value in the table. I have 11 more data in the table and they are not printed with this.
You need to loop through the recordsets .. (A while loop will do) Something like this will help
$con=mysqli_connect("localhost","root","","database");
$result = mysqli_query($con,"select * from client");
while($row = mysqli_fetch_array($result))
{
echo $row['name'];
}
The mysqli_fetch_array() function will return the next element from the array, and it will return false when you have ran out of records. This is how you can use while loops to loop through the data, like so:
while ($record = mysqli_fetch_array($result)) {
// do something with the data...
echo $record['column_name'];
}

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