I am trying to know if there is an existing data in the database, we I try to use existing username, based on my coding it should be "exists already", but the result is "proceed". Can you point out where I am wrong?
$query = mysql_query("SELECT * FROM `membership` WHERE `username` = '$username'");
if (mysql_num_rows($query) > 0){
echo "exists already";
}
else {
echo "proceed";
}
this should work
$str = "select SQL_CALC_FOUND_ROWS * from table where username=".$your_value;
$con=db_connect();
$result1 = mysql_query($str,$con);
$str1 = "SELECT FOUND_ROWS() as totalRecord";
$result2 = mysql_query($str1,$con);
$totalRecords = mysql_fetch_array($result2);
if ($totalRecords[0]>0){
echo "exists already";
}
else{echo "proceed";}
also try using mysqli or pdo as mysql is depricated...
Related
I have been trying to compare value in a table with another value in another table but only the else part is executing.
<?php
$sql = "SELECT * FROM user WHERE user_id=$row[user_id]";
$result = $conn -> query ($sql);
if ($result -> num_rows > 0) {
while ($row = $result ->fetch_assoc()) {
$sql1 = "SELECT * FROM jobpost WHERE jobpost_id=$_GET[id] ";
$result1 = $conn -> query ($sql1);
$row_count = mysqli_num_rows($result);
$row1_count = mysqli_num_rows($result1);
$remaining_rows = min($row_count, $row1_count);
$row = mysqli_fetch_assoc($result);
$row1 = mysqli_fetch_assoc($result1);
if($row["experience"] > $row1["experience"])
{
//some code to display something
echo "1";
}
else
{
echo "2";
}
} }
?>
Welcome to SO, it is hard to read your source code. You can use a simple query like this:
SELECT IF(u.experience > j.experience,1,0) FROM
(
(SELECT experience FROM user WHERE user_id = 1) u
JOIN
(SELECT experience FROM jobpost WHERE jobpost_id = 8) j
)
Try this query here: http://sqlfiddle.com/#!9/1742c0
Please check our datasource. Maybe the else case is correct.
All I need to do, is count all fields of the table, where postid=38.
But my code always prints "1" on screen, no matter what number I write in postid=38.
<?
$consulta2 = mysql_query("select count(*) from $tabla_db4 where postid='38';");
$result2 = mysql_num_rows($consulta2);
echo (string) $result2;
?>
You can do either this:
<?
$consulta2 = mysql_query("select count(*) as count from $tabla_db4 where postid='38';");
$result2 = mysql_fetch_array($consulta2);
echo $result2['count'];
?>
OR
$consulta2 = mysql_query("select* from $tabla_db4 where postid='38';");
$result2 = #mysql_num_rows($consulta2);
echo $result2;
Solved! Please close.
$sql = mysql_query(
"SELECT * FROM `comentarios` WHERE `postid` =
$registro[id] ORDER BY `postid` DESC",$conexion_db);
$cuenta = mysql_num_rows($sql);
echo $cuenta;
You can try this if you want to know how many rows there are:
<?php
$query = "select count(*) as amount from $tabla_db4 where postid='38';";
$result = mysql_query($query);
if(!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while($row = mysql_fetch_assoc($result)) {
echo $row['amount'];
}
mysql_free_result($result);
?>
Also, are you sure postid is a string, since you're putting it in between quotes. It looks like it might be an integer. Mind the datatypes.
I have this query. However, it does not work properly. The echo returns always a 1, but there are 3 rows in the db
<?php
include "db_connect.inc.php";
$sql = "SELECT COUNT(id) FROM profiles";
$res = mysqli_query($con, $sql);
$num = mysqli_num_rows($res);
if ($num == 0)
echo "0";
echo $num;
mysqli_close($con);
?>
You're doing an aggregate query, which means you'll ALWAYS get one row of results - one row containing the count() value you requested. Even if that count() is 0, you'll STILL get one row of results.
If you want to check the value of the count, you have to fetch that row and check the field's value, e.g.
$sql = "SELECT COUNT(id) AS cnt FROM profiles";
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
$row = mysqli_fetch_assoc($result);
if ($row['cnt'] == 0) { die("No profiles"); }
Your query returns 1 row with value 3
To see what you do expect you need something like:
<?php
include "db_connect.inc.php";
$sql = "SELECT COUNT(id) myCount FROM profiles";
$res = mysqli_query($con, $sql);
if ($row = mysqli_fetch_array($res, MYSQLI_ASSOC) ) {
echo $row['myCount'];
} else {
echo "0";
}
mysqli_close($con);
?>
i'm trying to run a very simple PHP function :
function evalLoggedUser($db_conx,$id,$e,$p){
$sql = "SELECT ip FROM users WHERE id={$id} AND email= '$e' AND password= '$p' AND activated=1 LIMIT 1 ";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows > 0){
return true;
} else {
echo $sql;
}
}
as part of a user authentication. The problem is that the query is not working and I dont know why! I know the mysql connection is working as I have checked the mysqli_errno and not getting anything there - Can anyone help?
Try this one..
function evalLoggedUser($db_conx,$id,$e,$p){
$sql = "SELECT ip FROM users WHERE id='$id' AND email= '$e' AND password= '$p' AND activated=1 LIMIT 1 ";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows > 0){
return true;
} else {
echo $sql;
}
}
Values gets substituted by default inside double quotes. But make sure you check the connection too.
$sql = "SELECT ip FROM users WHERE id=$id AND email= $e AND password= $p AND activated=1 LIMIT 1 ";
$query = "SELECT * FROM `table`";
$results = mysql_query($query, $connection);
If 'table' has no rows. whats the easiest way to check for this.?
Jeremy Ruten's answer above is good and executes quickly; on the other hand, it only gives you the number of rows and nothing else (if you want the result data, you have to query the database again). What I use:
// only ask for the columns that interest you (SELECT * can slow down the query)
$query = "SELECT some_column, some_other_column, yet_another_column FROM `table`";
$results = mysql_query($query, $connection);
$numResults = mysql_num_rows($results);
if ($numResults > 0) {
// there are some results, retrieve them normally (e.g. with mysql_fetch_assoc())
} else {
// no data from query, react accordingly
}
You could use mysql_num_rows($results) to check if 0 rows were returned, or use this faster alternative:
$query = "SELECT COUNT(*) AS total FROM table";
$results = mysql_query($query, $connection);
$values = mysql_fetch_assoc($results);
$num_rows = $values['total'];
Alternatively you can simply check if the result of mysql_fetch_assoc is false.
$query = "SELECT * FROM `table`";
$results = mysql_query($query, $connection);
$Row = mysql_fetch_assoc($results);
if ($Row == false)
{
$Msg = 'Table is empty';
}
One thing i noticed that was missed was the fact that the query might not succeed, so you do need to check if the $results variable is set. I'll use the answer given by yjerem as an example.
$query = "SELECT COUNT(*) AS total FROM table";
$results = mysql_query($query, $connection);
if ($results) { // or use isset($results)
$values = mysql_fetch_assoc($results);
$num_rows = $values['total'];
}
If you loop through the results, you can have a counter and check that.
$x = 1;
$query = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_assoc($query))
{
$x++;
}
if($x == 1)
{
//No rows
}