I need a way to do this, If a mysql query dosn't retrieve any data, something happens. Here's an example.
$color="red";
$query = mysql_query("SELECT * FROM people WHERE shirt='$color'");
if($query = null){
echo 'No people wearing '.$color' shirts available.';
}
Use mysql_num_rows() for this.
if( mysql_num_rows($query) == 0 ) {
// No results returned
Use mysql_num_rows to check, how many rows have been returned by your query.
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
if( $num_rows == 0 ) {
// No results returned
echo "No results!";
} else {
echo "$num_rows Rows\n";
}
?>
Besides the mysql_num_rows() there is also COUNT() which could be used like this:
"SELECT COUNT(*) FROM people WHERE shirt='$color'"
You might actually need more data from the database but if you only need a number then this would remove the need for an if.
You could use empty either:
$num_rows = mysql_num_rows($result);
if(empty($num_rows)){
echo 'No results';
}
Related
I want to show user if he liked a image or not..
for that I am creating php code
$userid=$_COOKIE['userid'];
$sql = "SELECT * FROM likes WHERE `user_id`='{$userid}'";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($query);
if($row){
echo "unlike";
}
else{
echo "like";
}
I can not do this for everything like 'tags', 'shares', 'comments', 'favourites' ...many
Isn't there anything simpler than this...?
Like say $row_check=mysqli_check_exist($table,$column_name,$userid);
use mysql fetch row method
$num_row = mysqli_num_rows($query);
if($num_row>0)
{
//add your code
}
else
{
//add your code
}
There are a lot of ways of doing this really but if you arnt going to use any more information then weither or not the user has liked it doing select * is a bad idea. The reason why is that you are asking the database to return the value of every column in that table.
Assuming its a small database its probably not a problem no but as your database gets bigger you are puting more load on it then you need you should try and only select the columns you need and intend to use. Ok in this case the userid is probably indexed and its only one row, but if you get in the habit of doing it here you may do it else where as well.
try this instead.
$userid=$_COOKIE['userid'];
$sql = "SELECT count(user_id) as total FROM likes WHERE `user_id`='{$userid}'";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($query);
if( $row ['total'] > 0){
echo "unlike";
}
else{
echo "like";
}
This way we are just getting the total. simple and elegant
Use mysqli_num_rows($query) if > 0 exist
You simply need to count the available records using
mysqli_num_rows($query);
This will return a number (count) of available records
So simple put a check like this :
$userid=$_COOKIE['userid'];
$sql = "SELECT * FROM likes WHERE `user_id`='{$userid}'";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
if($count>0){
echo "unlike";
}
else{
echo "like";
}
I am trying to view how many rows there are based on a SQL query using PHP.
I seem to be able query the database and return fields from a row but can't seem to find out how many rows there are based on the same query.
<?php
$host = 'localhost';
$user = 'MyUsername';
$pass = 'MyPassword';
$database = 'MyDatabase';
$con=mysqli_connect($host,$user,$pass ,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM MyTable WHERE test='123' AND test2='456'");
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
mysqli_close($con);
?>
All it returns is the text Rows on the screen, without the number of rows at the start.
Like I said, this same query works and returns a value if I try and select a row using:
while($row = mysqli_fetch_array($result))
{
echo $row["test"];
}
Anyone know why it won't return the number of rows?
You are using MySQLi. Because of you don't have a mysql query, mysql_num_rows doesn't return desired value.
You have to replace your mysql function with mysqli equal:
$num_rows = mysqli_num_rows($result);
You are using Mysqli to you should use mysqli_num_rows
$result = mysqli_query($con,"SELECT * FROM MyTable WHERE test='123' AND test2='456'");
$num_rows = mysqli_num_rows($result);
If you want only count then you can directly use count(*) like this:-
$result = mysqli_query($con,"SELECT COUNT(*) FROM MyTable WHERE test='123' AND test2='456'");
echo $result;
I'll do some thing like this:
$result = mysqli_query($con,"SELECT COUNT(*) FROM MyTable WHERE test='123' AND test2='456'");
echo $result
I have this piece of code:
require_once('connectvars2.php'); //This is the connection to my database
$data = mysql_query("SELECT * FROM calzone ORDER BY id DESC");
if (isset($_POST['submit'])) {
$post = trim($_POST['post']);
$data = mysql_query("SELECT * FROM calzone WHERE overskrift LIKE '%$post%' OR postnummer = '$post'");
if (!(empty($data))) { // This is my poor attempt :-)
echo 'No result!';
}
}
This is my search function on my website.
I want it to return something if there is no queries which match the searchwords. Say if I write "6737" and the database finds no results, it should print "Sorry, but there's no results for your search!".
Is there a way for MySQL to check if there was returned any queries? and if not, echo some text?
Use mysql_num_rows
$data = mysql_query("SELECT * FROM calzone WHERE overskrift LIKE '%$post%' OR postnummer = '$post'");
if(mysql_num_rows($data) > 0){
//show your data
}
else{
echo "Sorry, but there are no results for your search!";
}
Also, as mentioned, mysql_query is deprecated. Switch to PDO.
Use mysql_num_rows function to count no. of rows..
$data = mysql_query("SELECT * FROM calzone WHERE overskrift LIKE '%$post%' OR postnummer = '$post'");
$num=mysql_num_rows($data);
if($num==0)
{
echo 'Sorry, but theres no results for your search';
}
require_once('connectvars2.php'); //This is the connection to my database
$data = mysql_query("SELECT * FROM calzone ORDER BY id DESC");
if (isset($_POST['submit'])) {
$post = trim($_POST['post']);
$data = mysql_query("SELECT * FROM calzone WHERE overskrift LIKE '%$post%' OR postnummer = '$post'");
if (mysql_num_rows($data) == 0){
echo "Sorry, but there's no results for your search!";
}else{
echo "matched";
}
}
use
if(mysql_num_rows($data)==0){
echo "Sorry, but there's no results for your search";
}
you are probably looking for mysql_num_rows
Use like this
$data = mysql_query("SELECT * FROM calzone WHERE overskrift LIKE '%$post%' OR postnummer = '$post'");
if (mysql_num_rows($data) == 0) {
echo 'No result!';
}
On a side note; you are using the mysql_XX functions to access the database. These have been replace with Mysqli and MySQL(PDO). Use one of these upto date libraries.
use mysql_num_rows($data)==0
Although you should use mysqli and not mysql as mysql is deprecated nowadays
First of all, you should not use mysql_-functions cause this PHP extension is deprecated. Try to use PDO or Mysqli extension instead.
By the way in this extension there is mysql_num_rows function which does exactly what you want.
$data = mysql_query("SELECT * FROM calzone WHERE overskrift LIKE '%$post%' OR postnummer = '$post'");
if (!mysql_num_rows($data)) {
echo 'No result!';
}
Use http://www.php.net/manual/en/function.mysql-num-rows.php
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
$result=mysql_query("SELECT * FROM $name WHERE date='$date'");
How to check whether anything selected or not.
i have tried these but of no use :
if($result == NULL ){} , if(!$result){}
Help !
You can use
mysql_num_rows ($result);
to get the number of rows returned
if(!$result)
Will only tell wether the query executed or not
You can count number of records by
$num_rows = mysql_num_rows($result);
if($num_rows > 0)
{
//do something..
}
How could i get $result too equal nothing if the column doesn't exist in PHP?
I was thinking something like this:
$result=mysql_query("SELECT * FROM users WHERE username=$key DESC LIMIT 1")or die ($result = '');
But i was told thats wrong.
It's wrong because you're killing the script with die when a DB error occurs, rather than doing stuff when you find no row.
What you presumably need is more like:
$result = mysql_query($query);
if ($result) {
if ($row = mysql_fetch_assoc($result)) {
// do stuff with row
} else {
// do stuff without row
}
} else { // not needed but left here for illustration purposes
// this is the part that would occur, had you called mysql_query(...) or die;
die(mysql_error());
}
$result=mysql_query("SELECT * FROM users WHERE username= '$key' LIMIT 1")or die (mysql_error());
then check the result of mysql_num_rows()
If you mean that the result returns 0 rows, you can check with mysql_num_rows, like this:
$result=mysql_query("SELECT * FROM users WHERE username=$key DESC LIMIT 1");
if (mysql_num_rows($result) == 0)
$result = '';
Your code will set $result to '' if there's an error, in which case mysql_query returns false. It will also halt the code, since you're calling die(). An empty result set is not an error, however. In that case mysql_query returns a valid resource identifier with no rows. If I understand your question, this is what you want to do:
$result=mysql_query("SELECT * FROM users WHERE username=$key DESC LIMIT 1");
if (mysql_num_rows($result) == 0){
$result = '';
}
<?php
// Here I assume you're using PHP PDO
$pdo = new PDO("mysql:server=localhost;dbname=mydatabase", "root", "");
$result = $pdo->query("SELECT * FROM users WHERE username=$key DESC LIMIT 1");
$errorcode = $pdo->errorCode();
$errorinfo = $pdo->errorInfo();
// Columns doesn't exist
if($errorcode == "43072") $result = "";
// Other error...
else if($errorcode != "00000") die("MySQL Error: " . $errorinfo[2]);
?>