php mysql query works in phpmyadmin but not on webpage - php

i wrote the following query
SELECT COUNT(userID) From statistics WHERE userID = ""
this query displays the number of unathunticated visit to the website.
the query works in phpmyadmin when i use double quotes however it doesnt when i use single quotes like below it just gives me the number of record stored in the table
$queryB = "SELECT COUNT(userID) From statistics WHERE userID = ''";
$resultB =mysql_query($queryA, $con) or die(mysql_error());
$authB = mysql_result($resultB, "COUNT(userID)");
echo "the number of authenticated visits were $authB<br />\n";
i've no idea why it breaks, any ideas?

you store your query in $queryB but you use $queryA

not sure if it will work...its just first think that came to mind:
how about when u use escaped double quotes?
$queryB = "SELECT COUNT(userID) From statistics WHERE userID = \"\""

Try this:
$queryB = "SELECT COUNT(userID) AS total From statistics WHERE userID = ''";
$resultB =mysql_query($queryB, $con) or die(mysql_error());
$authB = mysql_fetch_assoc($resultB);
echo "the number of authenticated visits were ".$authB['total']."<br />\n";

Does userID have a default value? If the default value is NULL, then change your query to
$queryB = "SELECT COUNT(userID) From statistics WHERE userID IS NULL";

you should change a little to your code
$queryB = "SELECT COUNT(userID) From statistics WHERE userID = ''";
$resultB =mysql_query($queryB, $con) or die(mysql_error());
$authB = mysql_result($resultB, 0, 0);
echo "the number of authenticated visits were $authB<br />\n";

Related

PHP user account activation check not working

I'm using PHP to build a login form, and one part of the form validation is checking whether or not the user is "active," which is an integer in the mysql table users that will be equal to 0 or 1(1 being an activated). In my current setup, the user can't login if they don't have an active account. My first attempt to implement this was:
//This code won't work because the query is an object, not an integer.
elseif(mysqli_query($connection, "SELECT COUNT('ID') FROM users WHERE username='$usernamelogin' AND 'active'='1'") < 1) {
echo('Account not activated. ');
}
But that was not working, because the result is an object, not an int. So, I searched for how to solve this problem, and was given the following solution.
//if 1, account is active, if 0, account is inactive
$result = mysqli_query($connection, "SELECT COUNT('ID') FROM users WHERE username='$usernamelogin' AND 'active'='1'");
$row = mysqli_fetch_assoc($result);
$booly = ($row != 1) ? 1 : 0;
echo $booly;
This code, however, only gives an answer of 1, even when I would expect it to give a 0. Where have I gone wrong? What other methods could I use to implement this feature?
UPDATE:
I was indeed incorrectly using single quotes instead of backticks, so I tried implementing all of the following variations (I believe the first one to be correct, but tried all the others when it didn't work). None of them have changed my results.
"SELECT COUNT(`ID`) AS count FROM users WHERE username='$usernamelogin' AND 'active'='1'"
"SELECT COUNT(`ID`) FROM users WHERE username='$usernamelogin' AND active='1'")
"SELECT COUNT(`ID`) AS count FROM users WHERE 'username'='$usernamelogin' AND 'active'='1'"
"SELECT COUNT(`ID`) FROM users WHERE 'username'='$usernamelogin' AND 'active'='1'")
"SELECT COUNT(`ID`) AS `count` FROM `users` WHERE `username`='$usernamelogin' AND `active`='1'"
"SELECT COUNT(`ID`) FROM `users` WHERE `username`='$usernamelogin' AND `active`='1'")
I tried these as well, to no avail:
mysqli_query($connection, "SELECT COUNT(`ID`) FROM users WHERE username=$usernamelogin AND `active`='1'");
mysqli_query($connection, "SELECT COUNT(`ID`) FROM users WHERE username=$usernamelogin AND active='1'");
SOLUTION:
The solution implemented into the code:
$result = mysqli_query($connection, "SELECT COUNT(`ID`) AS count FROM users WHERE username='$usernamelogin' AND `active`='1'");
$row = mysqli_fetch_assoc($result);
if($row['count'] == true) {
echo('account not active'); }
Try this:
$result = mysqli_query($connection, "SELECT COUNT(`ID`) AS count FROM users WHERE username='$usernamelogin' AND `active`='1'");
$row = mysqli_fetch_assoc($result);
var_dump($row['count'] < 1);
Edit:
Changed single quotes to back quotes.
You have single quotes around ID field - so it's a string.
Try removing them or using ` marks around field identifier like this:
SELECT COUNT(`ID`) FROM users
You also have single quotes around the variable inserted into SQL so final fix would be:
"SELECT COUNT(`ID`) FROM users WHERE username=$usernamelogin AND `active`='1'"

Do Something When MYSQL Column Value Increments by 100

I need to alert members of a site when their post count has reached a multiple of 100. Is it possible to run a function or echo something when a value in a mysql table reaches 100, 200, 300, etc?
Table structure:
username | password | email | posts
I'm currently displaying their post count with:
$sql = mysql_query("SELECT `posts` FROM `user` WHERE username='$username'") or die(mysql_error());
$row = mysql_fetch_array($sql);
echo "<br /><h4>Your posts:</h4>" . " " . $row['posts'];
RESOLVED: I went with Marc's suggestion of using modulo, which suited my needs.
$sql = mysql_query("SELECT `posts` AS posts FROM `user` WHERE username='$username'") or die(mysql_error());
$row = mysql_fetch_array($sql);
$post = $row['posts'];
if($post % 100 == 0) {
mysql_query("INSERT INTO `posts` (name, message, message_raw) VALUES ('System', '$name has reached $post posts!', '$name has reached $post posts!')");
}
You need a modulo
Modulo operation. Returns the remainder of N divided by M.
Like this
SELECT username
FROM `user`
WHERE username='$username' AND MOD(posts,100) = 0
And you can check the condition every time a user do a new post
You can use triggers:
http://dev.mysql.com/doc/refman/5.5/en/triggers.html
or
Just create a cron job

Update table based on condition (While Loop)

So I am trying to update my table based on a singe parameter:
The dateEntered field must be blank.
And I want to randomly select 50 rows, and update the blank ownerID fields to "Tester"
Here is what I have:
<?php
include("includes/constants.php");
include("includes/opendb.php");
$query = "SELECT * FROM contacts WHERE dateEntered='' ORDER BY RAND() LIMIT 50";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$firstid = $row['id'];
$query2 = mysql_query("UPDATE contacts
SET ownerID = 'Tester'
WHERE id = '$firstid'");
$result2 = mysql_query($query2) or die(mysql_error());
}
?>
It will update a single record, then quit and give me:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
The first part that selects the records works fine, its query2 that won't update all 50 records, just one. Maybe I am writing this wrong.
mysql_query needs only one time
$query2 = mysql_query("UPDATE contacts
SET ownerID = 'Tester'
WHERE id = '$firstid'");
$result2 = mysql_query($query2) or die(mysql_error());
to
$result2 = mysql_query("UPDATE contacts
SET ownerID = 'Tester'
WHERE id = '$firstid'");
These answers are spot on, so I will only add some additional information, and a suggestion. When you are querying mysql the first time, $query1 is being set to the result resource, which for
$query1 = mysql_query("UPDATE contacts SET ownerID = 'Tester' WHERE id = '$firstid'");
returns a result of 1 (Boolean TRUE), which is why your second query failed, cause "1" isn't a valid mysql query string. As Greg P stated, you can fix your current script by eliminating the secondary mysql query.
However, you could improve the script entirely, and make fewer sql calls, by using this.
<?php
include("includes/constants.php");
include("includes/opendb.php");
$query = "UPDATE contacts SET owenerID='Tester' WHERE dateEntered='' ORDER BY RAND() LIMIT 50";
$result = mysql_query($query) or die(mysql_error());

PHP - MySql Join -

I was trying to get some details from MySql database, but i was working so long and tried so many ways that i am completely lost now :s
What i need to GET is two details which depend on information from 3 tables. I need to get $title and $siteurl (from table_sites) where current user did not click it before, AND siteurl owner still have remaining credits...
Here is my database:
USER:
id
username
password
credits
active
clicks
SITES:
id
userid
title
siteurl
clicks
active
weight
CLICKS:
id
siteid
byuserid
i tried with this MySql query:
include('db_con.php');
mysql_connect("$dbhost", "$dbusername", "$dbpassword")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$qrym = mysql_query("SELECT * FROM `users` WHERE username='$username' LIMIT 1") or die (mysql_error());
while($mem = mysql_fetch_object($qrym)){
$uid = $row->id;
}
$qrys = mysql_query("SELECT * FROM sites, clicks WHERE clicks.byuserid != '$uid' and sites.userid != '$uid' and sites.active = '1' ORDER BY sites.weight DESC, sites.id DESC LIMIT 1") or die (mysql_error());
if(mysql_num_rows($qrys)!=0){
while($row = mysql_fetch_object($qrys)){
$title = $row->title;
$siteurl = $row->siteurl;
echo "$title $siteurl";
}
} else {
echo "No more sites";
}
No errors, but whatever i try result is No more sites! How to JOIN this query correctly?
Maybe do
while($row = mysql_fetch_object($qrym)){
$uid = $row->id;
instead of
while($mem = mysql_fetch_object($qrym)){
$uid = $row->id;
You probably want a query like this:
SELECT [the columns you need]
FROM sites
LEFT JOIN clicks ON clicks.siteid = sites.id
AND clicks.byuserid = [current user id]
WHERE sites.active = 1 AND clicks.id IS NULL
ORDER BY sites.weight DESC, sites.id DESC
LIMIT 1
As gpojd noted above, you must must MUST sanitize your inputs before using them in a query. Fix your first query's code:
$qrym = mysql_query("SELECT * FROM `users`
WHERE username='" . mysql_real_escape_string($username) . "' LIMIT 1");
When fetching only a single row, as your first query does, there is absolutely NO need for a while() loop to retrieve the data.
That, and observe the comments in the code block:
$qrym = mysql_query("SELECT * FROM `users` WHERE username='$username' LIMIT 1") or die (mysql_error());
while($mem = mysql_fetch_object($qrym)){
^^^--- you fetch into $mem
$uid = $row->id;
^^^--- but try to retrieve from $row?
}
Try this instead:
$sql = "SELECT ...";
$qrym = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($qrym);
$uid = $row['uid'];

Setting variable equal to count of a field in database table

For the query below, I would like to set a variable called $totalcount equal to the count of all loginids in table called login. How can I do this?
Thanks in advance,
John
$sqlStrcount = "SELECT loginid FROM login";
$sqlQueryStr = "SELECT loginid FROM login";
$sqlQuery = mysql_query($sqlQueryStr);
$totalCount = mysql_num_rows($sqlQuery);
If you only need to count your records in login use this instead
for performance reasons.
$sqlQueryStr = "SELECT COUNT(loginid) as totalCount FROM login";
$sqlQuery = mysql_query($sqlQueryStr);
$row = mysql_fetch_assoc($sqlQuery);
$totalCount = $row['totalCount'];

Categories