I am doing the following...
$approved = oci_parse($conn_prs, "select * from patch_files where patch_reviewer = '". $_SESSION['id'] ."' and patch_status = 'Approved'"); // now rows available for current id
oci_execute($approved);
while ($row = oci_fetch_array($approved, OCI_BOTH + OCI_RETURN_NULLS )) {
var_dump($row); // shows nothing
if ($row == null) { echo "<p>None Found...</p>"; }
else { ....
Not sure why the null condition is not working...
Your null condition is not working because $row is never null. oci_fetch_array() returns an array of the row fields, or false. If there are no rows your while loop is not executed.
You seem to have misunderstood the purpose of OCI_RETURN_NULLS. When used, it creates array elements for empty fields in $row, not an empty array if there are no rows.
A quick and dirty way to do what you want is:
$i = 0;
while ($row = oci_fetch_array($approved, OCI_BOTH + OCI_RETURN_NULLS)) {
$i++;
...
}
if ($i == 0) {
echo "<p>None Found...</p>";
}
try with -
while ($row = oci_fetch_array($approved, OCI_BOTH + OCI_RETURN_NULLS )) {
var_dump($row); // shows nothing
if (empty($row)) { echo "<p>None Found...</p>"; }
else { ....
oci_fetch_array() - returns the next row from a query as an array.
Related
Hi i've got the following code, I'm trying calculate num rows to show a message if there is no rows in the database. I've got an array and I've used foreach to loop the arrays values and query the database. The problem is that if one of the ids from the array returns 0 rows but the other returns one row I get the value from the database query but also the message saying no rows found. Does anybody know why? Many thanks
$user_id = 30;
// Array of ids
$id_gruppo= user_group_ids($conn, $user_id);
$query_last_tickets = mysqli_prepare($conn, "SELECT ticket_id, ticket_subject, ticket_body, ticket_time_stamp, ticket_status, ticket_user_id, ticket_group FROM user_tickets WHERE ticket_group = ? ORDER BY ticket_time_stamp DESC LIMIT 5");
mysqli_stmt_bind_param($query_last_tickets, 'i', $data);
foreach ($id_gruppo as $data) {
mysqli_stmt_execute($query_last_tickets);
mysqli_stmt_bind_result($query_last_tickets, $ticket_id, $ticketsubject, $ticketbody, $ticketdate, $ticketstatus, $ticketuserid, $ticket_group);
mysqli_stmt_store_result($query_last_tickets);
$numTicket = mysqli_stmt_num_rows($query_last_tickets);
if ($numTicket > 0) {
while (mysqli_stmt_fetch($query_last_tickets)) {
echo $ticket_id;
}
}
}
if ($numTicket < 1) {
echo "no rows found";
}
You're checking the variable $numTicket after the loop that is updated inside the loop.
If the last $numTicket is 0, you'll fall in your last condition.
You can try something like this :
$have_tickets = false ;
foreach ($id_gruppo as $data) {
mysqli_stmt_execute($query_last_tickets);
mysqli_stmt_bind_result($query_last_tickets, $ticket_id, $ticketsubject, $ticketbody, $ticketdate, $ticketstatus, $ticketuserid, $ticket_group);
mysqli_stmt_store_result($query_last_tickets);
$numTicket = mysqli_stmt_num_rows($query_last_tickets);
if ($numTicket > 0) {
$have_tickets = true;
while (mysqli_stmt_fetch($query_last_tickets)) {
echo $ticket_id;
}
}
}
if (!$have_tickets) {
echo "no rows found";
}
You are assigning a value to $numTicket each time through the loop. This means that when you get to the if statement after the loop, it will contain the last value assigned to it within the loop. What you want to do is use a flag to determine if any of the iterations through the loop produce a count/result.
$user_id = 30;
// Array of ids
$id_gruppo= user_group_ids($conn, $user_id);
$query_last_tickets = mysqli_prepare($conn, "SELECT ticket_id, ticket_subject, ticket_body, ticket_time_stamp, ticket_status, ticket_user_id, ticket_group FROM user_tickets WHERE ticket_group = ? ORDER BY ticket_time_stamp DESC LIMIT 5");
mysqli_stmt_bind_param($query_last_tickets, 'i', $data);
// initialize the flag to false
$ticketsFound = false;
foreach ($id_gruppo as $data) {
mysqli_stmt_execute($query_last_tickets);
mysqli_stmt_bind_result($query_last_tickets, $ticket_id, $ticketsubject, $ticketbody, $ticketdate, $ticketstatus, $ticketuserid, $ticket_group);
mysqli_stmt_store_result($query_last_tickets);
$numTicket = mysqli_stmt_num_rows($query_last_tickets);
if ($numTicket > 0) {
// flip the flag to true since tickets were found
$ticketsFound = true;
while (mysqli_stmt_fetch($query_last_tickets)) {
echo $ticket_id;
}
}
}
if (!$ticketsFound) {
echo "no rows found";
}
I'm making a script to insert/update data from a mysql database to a postgres database.
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$postgres = "SELECT * FROM res_partner";
echo $postgres . "\n";
$pgresult = pg_query($db,$postgres);
$a = 0;
if(pg_num_rows($pgresult) > 0)
{
while($pgrow = pg_fetch_assoc($pgresult))
{
if($pgrow["id"] == $row["customer_id"])
{
$a + 1;
}
else
{
$a + 0;
}
}
var_dump($pgrow);
exit();
I want to check if the id in mysql database is also in the postgres database.
But the $pgrow returns "bool(false)" with my var_dump.
I have no idea why.
Because the var_dump() is outside the while loop.
The loop will continue to call pg_fetch_assoc until it returns false
Place the call inside the loop to see the contents of the current row:
while($pgrow = pg_fetch_assoc($pgresult))
{
if($pgrow["id"] == $row["customer_id"])
{
$a + 1;
}
else
{
$a + 0;
}
var_dump($pgrow);
}
Or use a proper debugger like XDebug to avoid having to write code like this
Can't you just do something like SELECT true FROM res_partner WHERE id = $customer_id so as not to do the while loop?
Then you would get either one row with a single value true if it exists, or false because no rows were returned.
I have a query on my site and if there is no results, I'd like to say something else rather than have a blank page...
$sth = $conn->prepare("SELECT * FROM directory WHERE user_active != '' AND County = :county");
$sth->execute(array(':county' => $county));
$c = 1;
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
RETURNED DATA GOES HERE
$c++;
}
$sth->execute(array(':county' => $county));
if ($sth->rowCount() == 0) {
echo 'no rows';
} else {
while(yada yada yada) { ... }
}
Relevant docs: http://php.net/manual/en/pdostatement.rowcount.php
You're counting the results in $c, so you can check which value that has by appending your code with this:
if($c == 1) { // Counter is stuck at 1
echo "No results were found.";
}
2 possible solution,
execute a SELECT COUNT
execute a fetchall check it and later display the result
So I have a query that I am returning all of the items into a mysql_fetch_array. Now, I know I could write another query and just select the items I need into a seperate query but, is there a way to just filter from the larger query what I want dependent on $_GET?
So, in english the user comes from a hyperlink that has ?id=1 and I peform a while that gets the all the values but, only display the $_GET['id'] items in a list
<?php //give ma all values but only echo out list of the $_GET['id'] in the url
while ($row = mysql_fetch_array($result) {
$id = $rowvideo["id"];
$title = $rowvideo["title"];
$length = $rowvideo["length"];
}
echo("<li><a href='#'>". $title." " .$length. "</a></li>");
?>
Hope this makes sense. Thank you all.
If you do not want a second query to get just what you need, a simple-if-statement in your loop should work:
<?php
$getId = isset($_GET['id']) ? $_GET['id'] : false;
//give ma all values but only echo out list of the $_GET['id'] in the url
while ($row = mysql_fetch_array($result)) {
$id = $row["id"];
$title = $row["title"];
$length = $row["length"];
if ($id == $getId) {
echo("<li><a href='#'>". $title." " .$length. "</a></li>");
}
}
?>
Note that I declared $getId outside of the loop to prevent having to use isset() during every iteration. If you don't verify if it's set and attempt to use it it will throw an undefined index warning - assuming you have error_reporting turned on (with that level enabled).
Alternatively, you could use PHP's array_filter() on the data after you've parsed it all:
$results = array();
while ($row = mysql_fetch_array($result)) $results[] = $row;
if (isset($_GET['id'])) {
$filtered = array_filter($results, function($element) use ($_GET['id']) { return ($element['id'] == $_GET['id']); });
$results = $filtered;
}
foreach ($results as $result) {
echo("<li><a href='#'>". $result['title']." " .$result['length']. "</a></li>");
}
My personal opinion would be to be more efficient and write the second query though, assuming of course you don't actually need all of the results when an id is specified. It would be as simple as:
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$query = 'SELECT id, title, length FROM table WHERE id=' . (int)$_GET['id'];
} else {
$query = 'SELECT id, title, length FROM table';
}
// your existing code as-is
A little more clarity here:
This will allow the filter by id in the url by specifying id=xxx, IF xxx is an integer that is positive. So id of 'bob' or -1 will not filter the results still giving all results
$filter=false;
if(isset($_GET['id']))
{
$filter_id=intval($_GET['id']);
if($id>0) $filter=true;
}
while($row = mysql_fetch_array($result))
{
if( (!$filter) || ( ($filter) && ($filter_id==$row['id']) ) )
{
$id = $row["id"];
$title = $row["title"];
$length = $row["length"];
// do other stuff here
}
}
I also changed $rowvideo to $row as this is the array you used to fetch the results.
<?php //give ma all values but only echo out list of the $_GET['id'] in the url
while ($row = mysql_fetch_array($result)) {
$id = $rowvideo["id"];
$title = $rowvideo["title"];
$length = $rowvideo["length"];
if ($id == $_GET['id']) { // or even ===
echo("<li><a href='#'>". $title." " .$length. "</a></li>");
}
}
?>
Im trying to figure out how to handle this is no results are returned, how would I code that?
while($row = mysql_fetch_array($Result))
So like if there a results: print them out
else: show a link
http://ca3.php.net/manual/en/function.mysql-num-rows.php
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result)) { ... }
} else {
// show link
}
You can use mysql_num_rows() to tell you how many results are found. Using that with a simple if-statement, and you can determine what action to take.
if (mysql_num_rows($result) > 0) {
// do while loop
} else {
// show link
}
Others suggest using mysql_num_rows() but you should be aware that that function works only if you use a buffered query. If you query using mysql_unbuffered_query(), the number of rows in the result is not available.
I would use a simple flag variable:
$found_row = false;
while ($row = mysql_fetch_array($result)) {
$found_row = true;
. . .
}
if ($found_row == false) {
// show link
}
It may seem redundant to set $found_row to true repeatedly, but assigning a literal value to a variable ought to be an insignificant expense in any language. Certainly it is small compared to fetching and processing an SQL query result.
Use even shorter syntax without insignificant mysql_num_rows to save processor time:
if($result) {
// return db results
} else {
// no result
}
I might have figured it out:
if (!($row = mysql_fetch_array($descResult)))
{
echo "<tr><td>Add Link</td></tr>";
}
This can be done without mysql_num_rows() or an additional (flag) variable
if ( false===($row=mysql_fetch_array($result, MYSQL_ASSOC)) ) {
echo 'no rows in result set';
}
else {
do {
echo $row['X'];
} while ( false===($row=mysql_fetch_array($result, MYSQL_ASSOC)) );
}
but it duplicates the actual fetch command (one in the if-statement and one in the while-clause).