if (isset($_GET["val"])) {
$val = $_GET["val"];
$sql1 = "select count(*) as count from staff_log l where l.time_in is null and l.time_out is not null and l.date_today = curdate() and l.staff_id = ".$val.";";
$result1 = mysql_num_rows($sql1);
}
add this line...
$result = mysql_query( $sql1);
$result1 = mysql_num_rows($result);
you must define query in mysql_query() function;
mysql_num_rows() returns, as shown in the PHP manual:
The number of rows in a result set on success or FALSE on failure.
This means that there was an error executing it, which is apparently because you didn't run any query (mysql_query()).
Note that the php manual also has the following warning at the top of the page:
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
first execute the query and then num_rows
The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MYSQLi or PDO_MySQL extensions.
try this
if (isset($_GET["val"]))
{
$val = $_GET["val"];
$sql1 = "select count(*) as count from staff_log l where l.time_in is null and l.time_out is not null and l.date_today = curdate() and l.staff_id = ".$val.";";
$result = mysql_query($sql1);
$count = mysql_num_rows($result);
}
You are almost right, please view the below code. You need to use mysql_query(), The mysql_query() is used to execute query of the default database.
if (isset($_GET["val"]))
{
$val = $_GET["val"];
$sql1 = "select count(*) as count from staff_log l where l.time_in is null and l.time_out is not null and l.date_today = curdate() and l.staff_id ='$val'";
$sql2 = mysql_query($sql1); // Execute the query first
$result1 = mysql_num_rows($sql2); // Get the number of rows from executed query result.
echo "The count is $result1";
}
Related
I am fairly new to PHP.
What I want is not much. I just want to place a check on my page which goes to database and check for value 1 or 0. 1 means "enable" so page continues ; and 0 means "disable" and page dies.
someone suggested the following but it didn't work
$sql = mysql_query("SELECT * FROM members WHERE access= '1'");
$user = mysql_query($sql);
echo $user;
if ($user !=="1") {
echo "You are not the proper user type to view this page";
die();
}
Thank you so much for your time.
Firstly, you're using mysql_query() twice and that alone will cause a syntax error.
You're also not looping over results, so use the following to achieve what you want to do.
$sql = "SELECT * FROM members WHERE access= '1'";
$user = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($user)){
if ($row['access'] !=="1") {
echo "You are not the proper user type to view this page";
die();
}
else {
echo "You are the right type.";
}
}
If your query requires a db connection to the query, you will need to do:
$user = mysql_query($sql, $connection);
This assuming a mysql_ successful connection. However, that API is deprecated as of PHP 5.5 and deleted as of PHP 7.0 and note that different MySQL APIs do not intermix.
It's time to step into the 21st century and use either the MySQLi_ or PDO API.
http://php.net/manual/en/book.mysqli.php
http://php.net/manual/en/book.pdo.php
You may also have to select an actual row (or rows) instead of SELECT * FROM members
I.e.:
SELECT column_1, column_2 FROM members
Consult these following links http://php.net/manual/en/function.mysql-error.php and http://php.net/manual/en/function.error-reporting.php
and apply that to your code.
You can also achieve this with mysql_num_rows():
$result = mysql_query("SELECT * FROM members WHERE access= '1'");
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
// do something
}
else {
// do something else
}
You might also want to add to the WHERE clause:
$result = mysql_query("SELECT * FROM members WHERE access= '1' AND column='x' ");
Trying to get the unread messages from my database with this function, but I end in resource id errors like this one: "Resource id #45"
function unread_message_count() {
$sql = "SELECT conversations_messages.message_id, conversations_members.user_id, conversations_members.conversation_last_view, conversations_messages.message_date
FROM conversations_members
INNER JOIN conversations_messages ON conversations_messages.user_id = conversations_members.user_id
WHERE conversations_members.conversation_last_view < conversations_messages.message_date AND conversations_members.user_id = {$_SESSION['user_id']}";
$result = mysql_query($sql);
return $result;
}
I call the function this way, but to nothing prints:
$count = unread_message_count();
echo $count;
mysql_query() returns a resource on success, or FALSE on error, you need to fetch the data from results, like:
...
$result = mysql_query($sql);
$rows = mysql_fetch_array($result);
return $rows;
Btw, mysql_ is deprecated, MySQLi or PDO_MySQL extension should be used.
mysql_query() returns a resource. The to string (implicitly triggered by using echo to output it) of that is Resource ID # followed by the id.
A resource in PHP is only supposed to be used with other PHP functions. This includes but is not limited to file, curl, ftp handles, etc.
use mysql_fetch_array() (or similar)
Just Try this
function unread_message_count() {
$sql = "SELECT conversations_messages.message_id, conversations_members.user_id, conversations_members.conversation_last_view, conversations_messages.message_date
FROM conversations_members
INNER JOIN conversations_messages ON conversations_messages.user_id = conversations_members.user_id
WHERE conversations_members.conversation_last_view < conversations_messages.message_date AND conversations_members.user_id = {$_SESSION['user_id']}";
$result = mysql_query($sql);
$data = mysql_num_rows($result);
return $data;
}
I have a table in my database named visitor_table . Within table i have a column named visitor_affiliate. I want to get the count of the rows when visitor_affiliate = "someurer" .
I want to get the count as number. I already have this code but i don't know how to get the count only for the rows containing the string. I currently get the number of all rows,
$result = mysql_query("SELECT * FROM visitor_table");
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
You can ask MySQL to return the count:
SELECT COUNT(*) FROM visitor_table WHERE visitor_affiliate = 'someurer'
You shouldn't be using the ancient (deprecated as of PHP v5.5.0, soon to be removed entirely) MySQL extension for writing new codeāuse instead the improved MySQLi extension or the PDO abstraction layer, both of which enable you to pass variables to the database in a safe, parameterised, fashion that ensures they are not evaluated for SQL (and therefore prevents SQL injection attacks):
$dbh = new PDO("mysql:dbname=$dbname", $username, $password);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
$qry = $dbh->prepare('
SELECT COUNT(*) FROM visitor_table WHERE visitor_affiliate = ?
');
$qry->execute(['someurer']);
echo $qry->fetchColumn(), ' rows';
$result = mysql_query("SELECT * FROM visitor_table WHERE `visitor_affiliate` = 'someurer'");
$num_rows = mysql_num_rows($result);
echo $num_rows . " Rows\n";
With what little information you have given just try this-
$result = mysql_query("SELECT * FROM visitor_table Where visitor_affiliate like '%someurer%'");
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
The deprecated MySQL:
$res = mysql_query('SELECT * FROM visitor_table');
echo 'Number of rows:'.mysql_num_rows($res);
Procedural MySQLi:
$tu = mysqli_prepare($DBH,'SELECT * FROM visitor_table');
mysqli_execute($tu);
$num_rows = mysqli_num_rows($tu);
echo $num_rows.' rows\n';
mysql has been deprecated. Please use mysqli.
Using MySQLi bind_param to get your result:
$someuser = 'Dave';
$DBH = mysqli_connect('localhost','user','pass','database');
$query = mysqli_prepare($DBH,'SELECT * FROM visitor_table WHERE user = ?');
mysqli_bind_param($query,'s',$someuser);
mysqli_execute($query);
mysqli_bind_result($id,$user,$tabCol1,$tabCol2);
while(mysqli_fetch_result){
$row = $row +1;
echo $user.': was found in the record with ID of: '.$id;
}
echo 'total records found:'.$row;
That's one way of doing it as I'm not completely 100% sure about using a mysqli_num_row() with the query above.
for some reason i can not understand im having troubles with mysql_num_rows.
Heres the script:
$notquery = 'SELECT * FROM notification WHERE uid = 1 AND read = 0
AND tipo = post
OR tipo = subpost OR tipo = logros';
$notQuery = (mysql_query($notquery));
$num_rows = mysql_num_rows($notQuery);
error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
This is probably because you have an error in the query execution.
Try adding or die(mysql_error()) to debug what's going wrong...
update the code to :
$notQuery = mysql_query($notquery) or die(mysql_error());
The sql is full with syntax error, try this
SELECT * FROM notification
WHERE
uid = 1 AND
`read` = 0 AND
tipo in('post', 'subpost', 'logros');
You input a string into mysql_num_rows =)
You use $notquery and $notQuery mixed... That must be the silliest thing I've ever seen.
Try this:
$sql = 'SELECT .....';
$result = mysql_query($sql);
echo mysql_error()."\n";
$numRows = mysql_num_rows($result);
Obviously, this might produce the same error, since you don't check for errors :) $result might not be a MySQL resource (it might be FALSE).
I'm running the following query, expecting it to return an INTEGER, but it returns "Resource id #3"
What am I doing wrong?
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
echo $queryPlans;
There are actually 15 rows in this table, and I would like to return the number 15.
Any suggestions?
mysql_query will return a php resource(see: http://www.php.net/manual/en/language.types.resource.php).
The returned resource should then be passed to mysql_fetch_assoc or similar.
Since you are only getting the count, you can use the following:
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
$count = mysql_result($queryPlans,0,0);
echo $count;
You need:
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
$row = mysql_fetch_array($queryPlans);
echo $row[0];
mysql_query() isn't returning the result. It's returning a resource you can loop across and interrogate for rows (as above).
This is actually expected behavior according to the documentation:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
It's a regular select that returns one row with one column and should be treated as such. You can call mysql_fetch_array on the result:
$row = mysql_fetch_array($resource);
$count = $row[0];
mysql_query() returns a result resource. You need another function the get "valuable information" from that resource. In this case mysql_fetch_array()/mysql_fetch_row()/mysql_fetch_object as cletus pointed out. Or (since it's only a single value) mysql_result().
Any sql query may fail for various reasons. You should always check the return value of mysql_query(). If it's FALSE something went wrong and mysql_error() can tell you more about it.
$mysql = mysql_connect(...) or die(mysql_error());
mysql_selecT_db(.., $mysql) or die(mysql_error($mysql));
$query = "SELECT count(*) FROM infostash.rooms";
$queryPlans = mysql_query($query, $mysql) or die(mysql_error($mysql));
$cRows = mysql_result($queryPlans, 0);
echo $cRows;
If you are planning on using the full query later (e.g. select , rather than count()), you can save yourself a database hit by using mysql_num_rows() on the full query. Example:
$queryPlans = mysql_query("SELECT * FROM infostash.rooms");
$results = mysql_fetch_array($queryPlans);
echo "There were " . mysql_num_rows($queryPlans) . " results";
while($row = mysql_fetch_assoc($queryPlans)){
// Do stuff here
}
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
mysql_num_rows($queryPlans);
http://us.php.net/manual/en/function.mysql-num-rows.php