sql/php query to set the value of a variable [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a table called "users" which has columns 'username', 'password' and 'permission'. In the permission column is either 'browse' or 'edit'.
Say I have a user logged into my site, I want to select select their permission using their username (which I have stored in a session variable). I want to then set a variable equal to either 'browse' or 'edit' based on their permission, to then use in further logic.
Assuming I have connected to and selected the appropriate database I am pretty sure the php code and query should go something like:
$u = $_SESSION['username'] ;
$sql = "SELECT permission FROM users WHERE username = '$u' " ;
$result = mysqli_query($sql);
But Im unsure how to then set a variable equal to 'browse' or 'edit' accordingly.
Any ideas?

Say you have a connection $con, for using session you have to start your session.
$u = $_SESSION['username'] ;
$sql = "SELECT `permission` FROM `users` WHERE username='$u'";
$result = mysqli_query($con, $sql);
$rows = mysqli_fetch_object($result);
//now its time to set the permission to the variable
echo $permission = $rows->permission;
mysqli_close($con);
you can also set the $permission to a $_SESSION.
$_SESSION['permission'] = $permission;

You have to do it like this:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$u = $_SESSION['username'] ;
$sql = "SELECT `permission` FROM `users` WHERE username='$u'";
$result = mysqli_query($conn, $sql);
$rows = mysqli_fetch_object($result);
//now its time to set the permission to the variable
echo $permission = $rows->permission;
mysqli_close($conn);

Related

MYSQL select query does not give any output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
My SQL query is not working. Below is my code and note that in that table query worked fine and gives output. But in PHP by using mysqli_num_rows(), mysqli_fetch_assoc() and mysqli_fetch_array() all doesn't works for me.
My DB connection is :
$conn = mysqli_connect($servername, $username, $password, $db);
Note : My DB Connectivity is fine.
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
$query = mysqli_query($conn, "SELECT * FROM `admin` WHERE `username`='$username'");
This query results true in PHPMyAdmin and returns false in PHP with the above functions. Can anyone answer is I made a mistake?
And I am tried that query to execute in following methods :
$row = mysqli_fetch_assoc(); // Results No Data
$data = mysqli_fetch_array($query); // Results No Data
$num = mysqli_num_rows($query); // Results 0 Data
you may execute the query before use it :
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect("localhost", "my_user", "my_password", "world");
$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC";
$result = mysqli_query($conn , $query);
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
You mast to run mysqli_query function for receive result from DB:
$username = mysqli_real_escape_string($conn, $_POST['username']);
$query = "SELECT * FROM `admin` WHERE `username`='$username'";
$result = mysqli_query($mysqli, $query);
$row = mysqli_fetch_assoc($result);
print_r($row);
PHP MySQL sandbox here

How to PHP MySQL query a STRING? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a string called $searchquery. $searchquery contains this SQL command:
SELECT * FROM `videos` WHERE (`Title` LIKE "%query%" OR `Tags` LIKE "%query%")
How can I make MySQL execute this command from a PHP page then display the results on PHP page??
I have tried
$sql = ($searchquery)
But dosent seem to work?
Firstly you need to create database connection
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// then execute your query
$sql = "SELECT * FROM `videos` WHERE (`Title` LIKE '%query%' OR `Tags` LIKE '%query%')";
$result = mysqli_query($conn, $sql);
// and fetch result set
while($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
?>

Update points of a user in mysql database using php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to take a string username from my android app and use that username to add 5 points to that specific users account.
Example:
My database now: user_id name username password points
1 test test test 0
What I want: user_id name username password points
1 test test test 5
Here is the php code I'm using right now, something must be wrong with it:
<?php
$con = mysqli_connect("localhost", "id177667_root", "***", "id177667_loginb");
$username = $_POST["username"];
$sql = "UPDATE user ". "SET points = points + 5 ". "WHERE username = $username" ;
$response = mysqli_query($sql, $con);
?>
You confused the parameters for mysqli_query. It should be mysqli_query($con, $sql); instead. Also there are a couple of other problems - this should work:
<?php
$con = mysqli_connect("localhost", "id177667_root", "***", "id177667_loginb");
$username = mysqli_real_escape_string($con, $_POST["username"]);
$sql = "UPDATE user SET points = points + 5 WHERE username = '$username'" ;
$response = mysqli_query($con, $sql);
?>
As it was suggested, prepared statements are the preferred way to go. So you could do this... tested it now, and it works for me:
<?php
$points = 5;
// Connect to database (credentials should not be stored in code...)
$con = new mysqli("localhost", "id177667_root", "***", "id177667_loginb");
// Check if connection succeeded
if ($con->connect_error)
die("Connection error: " . $con->connect_error);
// Prepare statement
if ($st = $con->prepare("UPDATE user SET points = points + ? WHERE username = ?")) {
// Bind parameters (i for integer value, s for string)
$st->bind_param("is", $points, $_POST["username"]);
// Execute statement
$st->execute();
// Close statement
$st->close();
} else {
// Prepare failed: report error
die("Prepare failed: " . $con->error);
}
// Close DB connection
$con->close();
?>

Converting MySQL PHP code / query to DB2 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
here's a part of my code that AFTER you submit your login credentials checks your username, password, etc...:
mysql_select_db("robur_mike") or die ("Could not find DB!");
$query = mysql_query("SELECT * FROM Bx1_Users WHERE Username='$username'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
while ($row =mysql_fetch_assoc($query))
{
$dbusername = $row['Username'];
$dbpassword = $row['Password'];
$dbfirstname = $row['FirstName'];
$dblastname = $row['LastName'];
}
.....
I now need to "translate" that to run under a DB2 database in BlueMix. I am already connected to the database using the code provided here:
How to connect to a SQL Database-s2 from a .php application in BlueMix
The query should be OK since it is basic SQL. What you should change is the way you run it, since in your old code you are using the mysql library.
Looking at the other question, I assume that you are able to connect doing something like:
$conn = db2_connect($conn_string, '', '');
Now to execute the query you can use db2_exec 'translating' your code to something like:
$sql = "SELECT * FROM <schemaName>.Bx1_Users WHERE Username='$username'";
if ($conn) {
$stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE));
while ($row = db2_fetch_assoc($stmt)) {
$dbusername = $row['Username'];
$dbpassword = $row['Password'];
$dbfirstname = $row['FirstName'];
$dblastname = $row['LastName'];
}
}
db2_close($conn);
As you can see I've added a placeholder for the schema name in the SQL query. You can retrieve it within your SQL Database dashboard (Manage/Work with tables).

Printing last 10 entries in database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
So I'm working on a website that has a list of novels in a database with some basic info about them. I'd like to make a table of the most recent additions to the database. I'm using PHP and SQL and this is what I've got so far.
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$database = "novels";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Select ten most recent entries
SELECT `N_ID`, `NAME`, `DATE_RELEASED`, `GENRES` FROM basic_info ORDER BY N_ID DESC LIMIT 10
?>
I"m new to PHP And SQL so what I can gather is that I've made a connection to the database and have pulled the information from the latest 10 entries. Now I'm just not sure how to print them.
Any help is appreciated!
Try to use PDO if you can. Also you could use lower case for your columns to avoid case sensitivity issues.
You have to "wrap" your SELECT query in a variable (e.g. $sql) to be able to pass it in your php code.
error_reporting(E_ALL);
ini_set("display_errors", 1);
$servername = "localhost";
$username = "root";
$password = "password";
$database = "novels";
try {
//Make your connection handler to your database
$conn = new PDO("mysql:host=".$servername.";dbname=".$database, $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
$sql = "SELECT `N_ID`, `NAME`, `DATE_RELEASED`, `GENRES` FROM basic_info ORDER BY N_ID DESC LIMIT 10";
$stmt = $conn->prepare($sql);
//Execute the query
$stmt->execute();
$result = $stmt->fetchAll();
//Fetch the results
foreach ($result as $row) {
echo '<p>'.$row['NAME'].'</p>';
}
} catch(PDOException $e) {
echo $e->getMessage();
die();
}

Categories