This question already has answers here:
PHP mysql_num_rows die error
(3 answers)
Closed 5 years ago.
I'm getting an error message when the data is not available in data base table. I would like to stop the qry search without getting an error when the data is not available.
It works good if the data is available in table but not works while the data is not available. For an example, when i search this city name in my table the requested city's data's are not available in that table. During this time i'm getting an "Undefined variable" error.
<?php
$city = 'chennai';
$cityQry ="SELECT * FROM area_data WHERE city ='$city' LIMIT 1";
$cityQryResult = mysql_query($cityQry);
while($row = mysql_fetch_array($cityQryResult)) {
$citypop = $row['population'];
}
?>
Check if mysql_num_rows gives you count more than 0, If count is 0 call die()
$city = 'chennai';
$cityQry ="SELECT * FROM area_data WHERE city ='$city' LIMIT 1";
$cityQryResult = mysql_query($cityQry);
if(mysql_num_rows($cityQryResult) == 0)
{
die("No Data Exists");
} else {
while($row = mysql_fetch_array($cityQryResult)) {
$citypop = $row['population'];
}
}
Please try Below Code (Not Tested Yet) :
<?php
$city = 'chennai';
$cityQry ="SELECT * FROM area_data WHERE city ='$city'";
$result = mysqli_query($cityQry);
// Mysql_num_row is counting table row
$count = mysqli_num_rows($result);
if($count > 0)
{
$cityQryResult = mysql_query($cityQry);
while($row = mysql_fetch_array($cityQryResult)) {
$citypop = $row['population'];
}
} else {
echo "No Data Exists";
}
?>
Related
This question already has answers here:
How to get count of rows in MySQL table using PHP?
(3 answers)
Closed 2 years ago.
How do I display this query result:
from MySQL onto a webpage, just like this:
?
I want to display the count only. I did run the same query through PHP but its returning '2'. The PHP code is below
<?php
//Connection for database
$conn = mysqli_connect("localhost", "root", "aaaaa", "db");
$query = "SELECT `gender`,COUNT(`gender`) FROM `reg` GROUP BY `gender`";
$result = mysqli_query($conn, $query);
if ($result)
{
// it return number of rows in the table.
$row = mysqli_num_rows($result);
if ($row)
{
printf("" . $row);
}
// close the result.
mysqli_free_result($result);
}
// Connection close
mysqli_close($conn);
?>
Please note that I have gone through other answers like this one but I can't really find my way through, I want the value count only not the total number of all rows in the table, .e.g. count for 'Male'.
You're using mysqli_num_rows(), which returns the number of rows in the result, not the actual data in the result. For that you need to use mysqli_fetch_assoc().
Your could would become:
$query = "SELECT `gender`,
COUNT(`gender`) AS `count`
FROM `reg`
GROUP BY `gender`";
$result = mysqli_query($conn, $query);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$gender = $row['gender'];
$count = $row['count'];
echo "$gender = $count<br>";
}
mysqli_free_result($result);
}
Note that I slightly changed your query to make the count accessible.
This question already has answers here:
Check if a row exists using old mysql_* API
(6 answers)
Closed 6 years ago.
Trying to display a message, when there are no results found.
<?php
mysql_connect ("localhost", "root","") or die (mysql_error());
mysql_select_db ("test");
$term = $_POST['term'];
$sql = mysql_query("select * from testable where FName like '%$term%' or LName like '%$term%' or ID like '%$term%' ");
while ($row = mysql_fetch_array($sql)) {
echo '<br/>First Name:'.$row['FName'];
echo '<br/>Last Name:'.$row['LName'];
echo '<br/>Phone:'.$row['Phone'];
echo '<br/><br/>';
}
?>
Try this :
if(mysql_num_rows($sql) == 0)
{
echo 'No results';
}
else
{
while($row = mysql_fetch_array($sql))
{
//..
}
}
You can use mysql_num_rows() function, Which return the number of returned rows.
if( mysql_num_rows($sql) == 0) exit('No records found!');
passing the above condition meaning that there are some result, So you can loop through:
while ($row = mysql_fetch_array($sql)) {
// print whatever you want.
}
I am trying to show some text depending upon what a status is set to in a db table.
See my code below:
$result=mysql_query("SELECT * FROM hr_recruitment_stages where vacancy_ref='$vacancyref' order by added_on DESC limit 0,1")or die('ERROR 315' );
$row = mysql_fetch_array($result);
$stage_name = $row ['stage_name'];
if($stage_name['stage_name'] == 'Shortlisting') { echo"Shortlisting"; } else { echo"Not Shortlisting"; } ?>
However this doesnt seem to be working properly as it is showing as Not Shortlisting even when stage_name equals Shortlisting.
Any ideas why?
Its variable type mistake. Check your assigned variable, you assigned the Array Element not the entire array. so try like below.
<?php
$result = mysql_query("SELECT * FROM hr_recruitment_stages where vacancy_ref='$vacancyref' order by added_on DESC limit 0,1") or die('ERROR 315' );
$row = mysql_fetch_array($result);
$stage_name = $row['stage_name'];
if($stage_name == 'Shortlisting') {
echo"Shortlisting";
} else {
echo"Not Shortlisting";
}
?>
Refer this Article for PHP Array understanding.
http://php.net/manual/en/language.types.array.php
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 6 years ago.
I am trying to check if record exist in SQL and get a return -> True or False.
It returns a notice:
Notice: Trying to get property of non-object in.... if($result->num_rows > 0)
$connection = new mysqli('localhost', 'user', 'pass','db_name');
$query = "SELECT * FROM order WHERE telephone = '".$telephone."' AND order_status_id='0' ";
$result = $connection->query($query);
if($result->num_rows > 0) {
echo 'found'; // The record(s) do exist
}
else{
echo 'not found'; // No record found
}
$connection->close();
ORDER is a mysql reserved word. Using it as field name, table name or whatever except ORDER BY fieldname requires using backticks:
SELECT * FROM `order` WHERE ....
Otherwise, you will get a query error, which will turn $result into boolean false.
Also, your code is vulnerable to sql-injection.
I advise you to use prepared statements.
You are checking the rows of the query object. The query object does not contain the rows.
What you want to do is something like
$data = $result->fetch_array();
if ($data->num_rows > 0)
{
//Rows exist
} else {
//No rows exist
}
It happens in case query do not injects object in your $result variable. You can make the following changes to proceed.
if(is_object($result) && $result->num_rows > 0) {
echo 'found'; // The record(s) do exist
} else{
echo 'not found'; // No record found
}
OR
if(!empty($result->num_rows)) {
echo 'found'; // The record(s) do exist
} else{
echo 'not found'; // No record found
}
Also keep the thing simple:
$query = "SELECT * FROM `order` WHERE telephone = '$telephone' AND order_status_id='0' ";
Use like this
$sqlReq = " SELECT * FROM order WHERE telephone = '".$telephone."' AND order_status_id='0' ";
$queryReq = mysql_query($sqlReq) or die(mysql_error());
if(mysql_num_rows($queryReq) > 0)
{
echo "found";
}else{
echo "not found";
}
$result1 = mysql_query("SELECT id FROM p_u_r WHERE p_name = '$d'");
$S=$row['id'];
if(mysql_num_rows($result1) == 0) {
row not found, do stuff...
}
this code send an error of undefined index error. I have created p_u_r table in my database but no data is inserted.
$result1 = mysql_query("SELECT id FROM p_u_r WHERE p_name = '$d'");
$S=$row['id'];
if(mysql_num_rows($result1) == 0) {
row not found, do stuff...
}
It is likely because $row... doesn't exist. You should write something like that:
// assuming you have created $d already...
$result1 = mysql_query("SELECT id FROM p_u_r WHERE p_name = '$d'");
if(mysql_num_rows($result1) == 0) {
// row not found, do stuff...
}
else {
//fetch your results using mysql_fetch_assoc() or mysql_fetch_row()
//...
}
BTW Do you know using MySQL extension is DEPRECATED and you should create code using MySQLi ?