I have a column called favid. I am trying to pull and compare the data in that column to an existing value:
<?php $query = mysql_query("SELECT * FROM ajaxfavourites WHERE favid=$favid");
while ($row = mysql_fetch_assoc($query)) {
echo $row['favid']; };?>
I also have an existing value:
$x
But when I do something like this it doesn't work:
<?php if($row['favid'] == $x){?>
Do this...
<?php } else { ?>
Do nothing...
<?php}?>
I realize the data in the column somehow isn't pulled out. What should be done for this to work?
Try this, I assume you already connected to DB.
<?php
$x = 1;
$query = mysql_query("SELECT * FROM ajaxfavourites WHERE favid='$favid'") or die(mysql_error());
if (mysql_num_rows($query) > 0)
{
while ($row = mysql_fetch_assoc($query))
{
if ($row["existing_column_name"] == $x)
{
echo "Yes";
} else
{
echo "No";
}
}
} else
{
echo "Nothing was found";
}
?>
<?php
$x = 100500; // integer for example
$CID = mysql_connect("host","user","pass") or die(mysql_error());
mysql_select_db("db_name");
$query = mysql_query("SELECT * FROM ajaxfavourites WHERE favid='{$favid}'", $CID);
while ($row = mysql_fetch_assoc($query)) {
if (intval($row["some_existing_column_name"])==$x){
print "Is equals!";
} else {
print "Is different!";
}
}
?>
Please be informed that mysql_connect and other functions with the prefix of mysql_ is deprecated and can be removed in the next versions of PHP.
Related
I have the following code to check if a row exists in MySQL:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT 1 FROM files WHERE id='$code' LIMIT 1");
if (mysql_fetch_row($result)) {
echo 'Exists';
} else {
echo 'Does not exist';
}
}
?>
This works fine. But I need to change it a bit. I have the following fields:
id, title, url, type. When someone uses the code above ^ to check if a row exists, I need a variable to get the url from the same row, so I can redirect the user to there.
Do you have any idea how I can do that?
Thanks in advance! :)
Try this:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT * FROM files WHERE id=" . $code . " LIMIT 1");
if (mysql_num_rows($result) > 0) {
while($rows = mysql_fetch_array($result)) {
echo 'Exists';
$url = $rows['url'];
}
} else {
echo 'Does not exist';
}
}
?>
It is quite simple. I think you don't show any effort to find the solution by yourself.
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT url FROM files WHERE id='$code' LIMIT 1");
if ($result) {
$url = mysql_fetch_row($resultado);
} else {
echo 'Does not exist';
}
}
<?php
$sql_query = "SELECT * FROM test WHERE userid ='$userid'";
$result1 =mysql_query($sql_query);
if(mysql_num_rows($result1)>0){
while($post = mysql_fetch_array($result1))
{
$url = $post['url'];
}
}
?>
If mysql_num_rows($result1)>0 it means row is existed fir the given user id
Im trying to make a code where it displays all names in database where "clanwars" is set to 1 (int) and if all of them is 0 echo an message like: "Noone has signed up yet!"
This is some of the code i have:
$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
while($row = mysqli_fetch_array($result))
{
if ($row['clanwars'] != '0') {
echo $row['mantra']."<br>";
} else {
echo 'Noone has signed up yet!';
}
}
mysqli_close($con);
First, in your example row['clanwars'] will never equal to 0 because you already specified WHERE clanwars = 1 in your query, so MySQL will return only those that have clanwars=1. If I understand well, you need to do something like:
<?
$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
if (mysqli_num_rows($result)==0) echo 'Noone has signed up yet';
else {
while ($row = mysqli_fetch_array($result)) {
//do what you need
}
}
?>
So basically, you retrieve everyone who has CLANWARS set to 1 in the database. If there are records, process them, if there are no records, it means that nobody has signed up.
Is this what you need?
Try this:
$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
if (mysqli_num_rows($result) == 0)
{
echo 'Noone has signed up yet!';
}
else
{
while ($row = mysqli_fetch_array($result))
{
echo $row['mantra']."<br>";
}
}
mysqli_close($con);
Code:
$Username = $_SESSION['VALID_USER_ID'];
$q = mysql_query("SELECT * FROM `article_table`
WHERE `Username` = '$Username'
ORDER BY `id` DESC");
while($db = mysql_fetch_array($q)) { ?>
<?php if(!isset($db['article'] && $db['subject'])) {
echo "Your articles";
} else {
echo "You have no articles added!";
} ?>
<?php } ?>
So I want the rows for example(db['article'] and $db['subject']) from a specific username (see: $Username = $_SESSION['VALID_USER_ID'];) to echo the information if is not empty else if is empty to echo for example "You have no articles added!"
If is some information in the rows the code works, echo the information BUT if the rows is empty don't echo nothing, the code should echo "You have no articles added!" but this line don't appear, where is the mistake?
I tried for if !isset, !empty, !is_null but don't work.
I think what you're trying to achieve is:
$Username = $_SESSION['VALID_USER_ID'];
$q = mysql_query("SELECT * FROM `article_table` WHERE `Username` = '$Username' ORDER BY `id` DESC");
if(mysql_num_rows($q) > 0)
{
echo "Your articles:\n";
while($db = mysql_fetch_array($q)) {
echo $db['subject']." ".$db['article']."\n";
}
}
else
{
echo "You have no articles added!";
}
?>
I don't understand. Do you have article rows with username, but without article, i.e.:
| id | user | article |
-------------------------------------
| 1 | X | NULL |
If so, you can test with:
if($db['article'] == NULL) { .... } else { .... }
Otherwise, if you don't have a row with user=x, when there are no record, mysql will return an empty result.
So, basicly, if no rows are found on selection: SELECT * FROM article_table WHERE Username = 'X';, you can test
if(mysql_num_rows($q) > 0) { .... } else { .... }
However, mysql_ functions are not recommended anymore. Look at prepared statements.
You have a logic error in your if statement -- what you want is to check if both the article and subject are set.
With your current code, you compare $db['article'] with $db['subject'], and check if the result is set. You need to change it a bit :
Instead of :
if(!isset($db['article'] && $db['subject'])) {
Try:
if(isset($db['article']) && isset($db['subject'])) ...
I would do something like this:
$articles='';
$Username = $_SESSION['VALID_USER_ID'];
$q = mysql_query("SELECT * FROM `article_table` WHERE `Username` = '$Username' ORDER BY `id` DESC");
while($db = mysql_fetch_array($q)) {
if(isset($db['article']) && isset($db['subject'])) {
$articles .= $db['article']."<br/>";
}
}
if($articles != ''){
echo $articles;
}
else{
echo "No articles";
}
?>
fastest way to achieve what you want is by adding a variable that will verify if the query returned any rows:
<?php $Username = $_SESSION['VALID_USER_ID'];
$i = 0;
$q = mysql_query("SELECT * FROM `article_table` WHERE `Username` = '$Username' ORDER BY `id` DESC");
while($db = mysql_fetch_array($q)) {
$i = 1;
if(!isset($db['article'] && $db['subject'])) { echo "Your articles"; } ?>
<?php }
if ($i == 0) echo "You have no articles";
?>
You tried to echo "no articles" in the while loop, you get there only if the query returns information, that is why if it returns 1 or more rows, $i will become 1 else it will remain 0.
In your case:
$numArticles = mysql_num_rows($q);
if($numArticles > 0)
echo 'Your articles';
else
echo 'No articles :((((';
I recommend tough moving on to PDO to communicate with DB.
I am trying to echo out all of the user rows in my database as a select in a form.
It is only showing a blank space. Nothing else.
Here is my code.
<?php
session_start();
require('../../config.php');
$user = $_SESSION['user'];
$qry=("SELECT `rank`, `uname` FROM users WHERE `uname` = '$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row['rank'];
$logged = $_SESSION['loggedin'];
if ($logged == true) {
if ($rank >= 3) {
echo "Succesful, $user.<br />
<form method='POST' action='delete.php'>
<select><option>Please select</option>";
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
}
echo "<option>$lol</option>";
echo "</select>
</form>";
} else {
echo "Your not an admin. $user";
}
} else {
echo "Please login.";
}
?>
First, please stop using mysql_ functions as they are being deprecated. Look into mysqli_ or PDO. Be aware that your script is vulnerable to SQL injection.
The reason your script is not working is because it appears you are calling mysql_fetch_assoc twice. When calling it the second time, there won't be any output if your query only returns a single row.
$qry=("SELECT `rank`, `uname` FROM users WHERE `uname` = '$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row['rank'];
You will need to resubmit a query (something like below) and call that result separately to populate the drop down, or store the result in an array.
$qry=("SELECT `uname` FROM users");
$result=mysql_query($qry);
while ($row = mysql_fetch_assoc($result)) {
echo '<option>' . ucwords($row['uname']) . '</option>';
}
It looks like your while loop is set up badly, I think you need to change
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
}
echo "<option>$lol</option>";
to this
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
echo "<option>$lol</option>";
}
UPDATE:
I reread your question and it seems that you want to display all unames from your table.
The original query was to check if the user is an admin.
Here's how it will look like. But it's just a duplicate of njk's answer.
<?php
session_start();
require('../../config.php');
$user = $_SESSION['user'];
$qry=("SELECT `rank`, `uname` FROM users WHERE `uname` = '$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row['rank'];
$logged = $_SESSION['loggedin'];
if ($logged == true) {
if ($rank >= 3) {
echo "Succesful, $user.<br />
<form method='POST' action='delete.php'>
<select><option>Please select</option>";
// added these 2 lines
$qry=("SELECT `rank`, `uname` FROM users");
$result=mysql_query($qry);
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
echo "<option>$lol</option>";
}
echo "</select>
</form>";
} else {
echo "Your not an admin. $user";
}
} else {
echo "Please login.";
}
?>
Are you sure your database has more than 1 row? You are calling the fetch 1 time so the second time it will try to fetch the second row.
Try also running the query directly into mysql and see if you get the rows you're expecting back, may be its not returning what you expected
I am trying to echo out all of the user rows in my database as a select in a form.
It is only showing a blank space. Nothing else.
Here is my code.
<?php
session_start();
require('../../config.php');
$user = $_SESSION['user'];
$qry=("SELECT `rank`, `uname` FROM users WHERE `uname` = '$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row['rank'];
$logged = $_SESSION['loggedin'];
if ($logged == true) {
if ($rank >= 3) {
echo "Succesful, $user.<br />
<form method='POST' action='delete.php'>
<select><option>Please select</option>";
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
}
echo "<option>$lol</option>";
echo "</select>
</form>";
} else {
echo "Your not an admin. $user";
}
} else {
echo "Please login.";
}
?>
First, please stop using mysql_ functions as they are being deprecated. Look into mysqli_ or PDO. Be aware that your script is vulnerable to SQL injection.
The reason your script is not working is because it appears you are calling mysql_fetch_assoc twice. When calling it the second time, there won't be any output if your query only returns a single row.
$qry=("SELECT `rank`, `uname` FROM users WHERE `uname` = '$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row['rank'];
You will need to resubmit a query (something like below) and call that result separately to populate the drop down, or store the result in an array.
$qry=("SELECT `uname` FROM users");
$result=mysql_query($qry);
while ($row = mysql_fetch_assoc($result)) {
echo '<option>' . ucwords($row['uname']) . '</option>';
}
It looks like your while loop is set up badly, I think you need to change
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
}
echo "<option>$lol</option>";
to this
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
echo "<option>$lol</option>";
}
UPDATE:
I reread your question and it seems that you want to display all unames from your table.
The original query was to check if the user is an admin.
Here's how it will look like. But it's just a duplicate of njk's answer.
<?php
session_start();
require('../../config.php');
$user = $_SESSION['user'];
$qry=("SELECT `rank`, `uname` FROM users WHERE `uname` = '$user'");
$result=mysql_query($qry);
$row = mysql_fetch_assoc($result);
$rank = $row['rank'];
$logged = $_SESSION['loggedin'];
if ($logged == true) {
if ($rank >= 3) {
echo "Succesful, $user.<br />
<form method='POST' action='delete.php'>
<select><option>Please select</option>";
// added these 2 lines
$qry=("SELECT `rank`, `uname` FROM users");
$result=mysql_query($qry);
while ($row = mysql_fetch_assoc($result)) {
$users = $row['uname'];
$lol = ucwords($users);
echo "<option>$lol</option>";
}
echo "</select>
</form>";
} else {
echo "Your not an admin. $user";
}
} else {
echo "Please login.";
}
?>
Are you sure your database has more than 1 row? You are calling the fetch 1 time so the second time it will try to fetch the second row.
Try also running the query directly into mysql and see if you get the rows you're expecting back, may be its not returning what you expected