I am trying to produce an outcome if both rows in two different tables match, but I am having a hard time trying to make it work. Can someone please tell me if I am missing something.. Thank you in advance
**Note: my connections are in an included file and both tables are in the same database
<html>
<p>Pending Documents</p>
<?php
$sql ="SELECT * FROM `forms`";
if ($_SESSION['user_name'] == $row["username"]){ ?>
<p>SUTA Document</p>
<? }else { ?>
<p>No Pending Documents</p>
<? } ?>
</html>
You should solve it with mysqli. Here is a working snippet:
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . mysqli->connect_error;
}
if ($result = $mysqli->query("SELECT * FROM `forms`")) {
while ($obj = $result->fetch_object()) {
if ($_SESSION['user_name'] == $obj->username){ ?>
<p>SUTA Document</p>
<? }else { ?>
<p>No Pending Documents</p>
<? }
}
$result->close();
}
$mysqli->close();
?>
Related
Working on a small project with some simple sql query injection in my php file. I have created a functions.php file with a function called function displayimage(). I include my function file in my index file and use the function like so
index.php
<div class="col-lg-2">
<?php displayimage(); ?>
</div>
Functions.php
function displayimage()
{
$dbCon = mysqli_connect("localhost", "root", "root", "testdb");
if (mysqli_connect_errno()) {
echo "Failed to connect: " . mysqli_connect_error();
}
$sql= "SELECT * FROM `images` ORDER BY `images`.`id` DESC ";
$query=mysqli_query($dbCon, $sql);
if ($row = mysqli_fetch_array($query))
{
echo '<img class="img-responsive" style="margin-top: 10px;" src="data:image;base64,'.$row[2].' "> ';
}
mysqli_close($dbCon);
}
?>
So it works fine but.. I tried to clean my code by putting the database connection in a seperate file, and including it like include('connection.php');. Unfortunately my code doesn't work anymore, and the content won't show up at my index file. My PHPStorm says that $dbCon is a undefinable variable now. What am I doing wrong here?
new functions.php
function displayimage()
{
include('connection.php');
$sql= "SELECT * FROM `images` ORDER BY `images`.`id` DESC ";
$query=mysqli_query($dbCon, $sql);
if ($row = mysqli_fetch_array($query))
{
echo '<img class="img-responsive" style="margin-top: 10px;" src="data:image;base64,'.$row[2].' "> ';
}
mysqli_close($dbCon);
}
?>
connection.php
$dbCon = mysqli_connect("localhost", "root", "root", "testdb");
if (mysqli_connect_errno()) {
echo "Failed to connect: " . mysqli_connect_error();
}
You should include connections.php on the top on your page if you want to make a connection to a database. However if you're using mysqli I would recommend using the object orientated syntax over the procedural. That way you don't have to parse the $connection variable each time you query.
require_once 'connection.php';
function displayimage(){
global $dbCon;
$sql= "SELECT * FROM `images` ORDER BY `images`.`id` DESC ";
if($qry= mysqli_query($dbCon, $sql) != false){
// query ran successfully, here you should actually continue the code..
while($row = mysqli_fetch_array($query)){
echo '<img class="img-responsive" style="margin-top: 10px;" src="data:image;base64,'.$row[2].' "> ';
}
} else {
echo 'failed to retrieve images from the database.';
}
}
Also, you don't have to close the connection every time when you're done querying. Its done automatically at the end of the script and without it it can continue to use the already opened connection.
However it is bad practice to use global variables in functions, just make sure you never overwrite the $dbCon variable, it might happen when using code from somebody else.
Here is the code which I am trying to block a user from my website. I am using MySQL database and I insert a Banned column in my user table and set their value 1 by default so tell me what I have to do. Table name of this section in MySQL database is Lectures So please help me as soon as possible.
<?php
session_start();
include("header.php");
include("conection.php");
//echo "lec name".$_SESSION["lecname"];
?>
<?php
if($banned == "1")
{
header("Location:lectureaccount.php");
}
else
{
header("Location:banned.php");
}?>
<?php
//if(isset($_SESSION["userid"]))
{
$lec_id = $_SESSION["userid"];
?>
you are doing good.
I am assuming you need to know what your SQL should be to get user data from the database.
<?php
session_start();
include("header.php");
include("conection.php");
//echo "lec name".$_SESSION["lecname"];
if(isset($_SESSION["userid"]))
{
$user_id = $_SESSION["userid"];
$con=mysqli_connect("example.com","username","password","database_name");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT banned FROM Lectures where user_id = ".$user_id);
while($row = mysqli_fetch_array($result)){
$banned= $row['banned'];
}
mysqli_close($con);
if($banned == "1"){
header("Location:lectureaccount.php");
}
else{
header("Location:banned.php");
}
}
?>
<?php
//if(isset($_SESSION["userid"]))
{
$lec_id = $_SESSION["userid"];
Heres my code. Simple.
<?php
echo 'start<br>';
//Do the conntection
$checkconnection = mysql_connect('localhost', 'root', 'rootpass');
//Check if it's valid
if(!$checkconnection) {
echo 'CheckFailed';
} else{
echo 'CheckSucess';
}
echo 'end'; ?>
but I only can see 'start'. There is no 'CheckFailed', 'CheckSucess', 'end'
What should I do?
I already install mysql, create database, create tables, of course.
<?php
// Create connection
$con=mysqli_connect("localhost","root","root","database");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return false;
}
$result = mysqli_query($con, "SELECT * FROM table;");
?>
I am new to mysql and php. I am working on database programming with php with mysql but getting "no data base selected" error continuously. I found this error quite famous on internet. I tried every answer which were given to others having the same problem but nothing worked.
Here is my code:
if(!#mysql_connect('localhost','root','') || !#mysql_select_db ('a_database') ){
die ('Connection Error !');
}
$query = "SELECT `food`,`calories` FROM `food` ORDER BY `id`";
if($query_run=mysql_query($query)){
while($query_row = mysql_fetch_assoc($query_run))
{
$food = $query_row['food'];
$calories = $query_row['calories'];
echo $food.' has '.$calories.' Calories'.'<br>';
}
} else {
echo mysql_error();
}
This is the code that gives error. After some search on net. I made some bit of changes, but the result was same.
Changes that I made to first 3 to 4 lines:
$link = mysql_connect('localhost','root','');
if(!$link || !mysql_select_db ('a_database', $link) ){
die ('Connection Error !');
}
Please tell me what should I do to get rid of this problem, Thank you.
Try with this
<?php
// Create connection
$con=mysqli_connect("localhost","root","","a_database");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try this
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n"; // if everything is successful
And yes don't use mysql_* as it's deprecated, use mysqli_ or PDO.
I have an issue with php. The following code generates the error "PHP Warning: mysqli_close() expects parameter 1 to be mysqli, null given[...]" on the line containing the mysqli_query
<html>
<head>
<?php
$table = "prjsuggestions";
/$mysqli = NULL;
if(!empty($_POST['posttext'])){
$pnameempty = empty($_POST['postname']);
$ynameempty = empty($_POST['name']);
if($pnameempty || $ynameempty){
}
else{
$mysqli = new mysqli("localhost", "progclub", "", "progclub");
if(mysqli_connect_errno()){
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//successful query normally occurs here but code fails w/ or /wo it.
}
}
else{
printf("No information posted.");
}
?>
<title>Bucknell Programming Club</title>
</head>
<body>
<span id="posts">
<?php
$offset = 0;
$query = "SELECT * FROM {$table}";
$result = mysqli_query($mysqli, $query);
if($result !== FALSE){
//while(($post = mysqli_fetch_assoc($result)) !== NULL){
echo mysqli_num_rows($result);
$post = mysqli_fetch_assoc($result);
$author = $post['name'];
printf("Author: %s\n", $author);
echo "<br />";
printf("Post title: %s\n", $post['title']);
echo "<br />";
printf("%s\n", $post['text']);
echo "<hr />";
//}
}
else printf("oh nooo!");
mysqli_free_result($result);
mysqli_close($mysqli);
?>
</span>
</body>
</html>
Note that all queries have been checked out and are working correctly in phpmy, and that the original code contains an earlier query that adds data to the 'base, which also definitely works.
I have tried various combinations of static and global, and I have taken a thoroughish look at PHP's page on variable scope, alas I do not entirely understand it in this context (esp. given my inability to make my code work). Can somebody enlighten me as to the differing scopes here? I didn't think there should be any!!
Take a look at the documentation (specifically Example 1). There are a few example similar to yours.
The following should work, and it is similar to your code:
Note how the first argument of mysqli_query() is in fact your variable $mysqli. You were probably trying to put something else there.
Also, be sure to check your connection, like in the code below:
<html>
<head>
<?php
$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");
?>
</head>
<body>
<?php
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* Use $mysqli */
if (mysqli_query($mysqli, "/* ... MySQL goes here... */") === TRUE) {
/* Success! */
}
mysqli_close($mysqli);
?>
</body>
</html>
OOOOOOOOOOoooooooooooooooooohh. D'oh. Yeah. Scope. Perhaps I should have though more about the fact that the variable is only initialized when data is written. Oops. Thanks guys.