IF-ELSE from MySQL based on single row - php

I try to get the value 'entryScans' from my SQL-table and send it to $output based on the IF-ELSE case. What am I doing wrong?
<?php
include ('config.php');
$TicketNo=$_POST["TicketNo"];
$hash=$_POST["hash"];
$sql = "SELECT TicketNo,hash,entryScans FROM `Tickets` WHERE `TicketNo` = '$TicketNo' AND `hash` = '$hash'" or die(mysql_error());
$result = mysql_query($sql);
$row=mysql_num_rows($result);
if($row['entryScans'] = 0){
$output="ok";
}
else if ($row['entryScans'] > 0) {
$output="maybe";
else{
$output="error";
}
print(json_encode($output));
mysql_close();
?>

You are doing ...
if($row['entryScans'] = 0){
Which has 2 problems, = is assignement, == is testing equal to. The second part is that your fetching the results in...
$result = mysql_query($sql);
$row=mysql_num_rows($result);
So $row is the number of rows, $result is the query results...
So to check the number of rows in the result set
if($row == 0){
etc.
Update:
If you want the value of the column entryScans to be used, then you need to change the call of mysql_num_rows() to mysql_fetch_assoc(), so
$row=mysql_fetch_assoc($result);
Then you can leave the rest of the code to use $row['entryScans']

You are not processing your results. You just get the total number of rows in $row variable.
You need to process your query result by looping method.
Here is the example
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
if($row['entryScans'] = 0)
{
$output="ok";
}
else if ($row['entryScans'] > 0)
{
$output="maybe";
}
else
{
$output="error";
}
}
Hope this works for you.

Related

mysqli_fetch_assoc() not returning anything

This is not the first time I have used mysqli_fetch_assoc() to get a specific column of a row. For some reason I can not figure out why it is not working this time around. Heres my code:
$sql = "SELECT * FROM database.table WHERE PushNumber > 0";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = mysqli_fetch_array($result)) {
// This will loop through each row, now use your loop here
$row = mysqli_fetch_assoc($result);
$pushNumber = $row['PushNumber'];
$followingIDs = $row['FollowingIDs'];
$usernameID = $row['Username'];
error_log("'$pushNumber", 0);
error_log("'$usernameID", 0);
}
}
There is one row in my table where PushNumber is greater than 0 and $pushNumber (type: int) and $usernameID (type: string) are printed in the error log and both are printed as: '' AND I DON'T KNOW WHY! There are no spelling errors and I know for a fact in that row PushNumber is equal to 1 and Username is equal to #yo. Any suggestions would be appreciated please help. Thank you!
You appear to assign two completely different sets of values into your $row variable.
$row = mysqli_fetch_array($result) transfers the entire array into $row, but then in your loop you use $row = mysqli_fetch_assoc($result) to transfer a single record into $row, which is most likely throwing your loop off.
Try this:
if($result->num_rows > 0)
{
while($row = mysqli_fetch_assoc($result))
{
// This will loop through each row, now capture each row's data here
$pushNumber = $row['PushNumber'];
$followingIDs = $row['FollowingIDs'];
$usernameID = $row['Username'];
error_log($pushNumber, 0);
error_log($usernameID, 0);
}
}

php get_result() returns an empty result set

So, I have a prepared statement that I have successfully prepared, bound, and executed:
SELECT * FROM users WHERE email = ? AND pass = SHA1(?)
It looks like one row is returned, as expected. However, why is the $result variable empty when I call #$stmt->get_result()? Thanks in advance.
$num_rows = mysqli_num_rows($stmt->get_result());
if ($num_rows == 1) {
// Fetch the result set
$result = $stmt->get_result();
//if result empty echo false
if(empty($result)) {
echo "result is empty";
}
}
Just to put the two comments together and elaborate a litte....
<?php
$result = $stmt->get_result();
$num_rows = mysqli_num_rows($result);
if ($num_rows == 1) {
// already fetched the mysqli_result
// now lets fetch the one record from that result set
$row = $result->fetch_assoc();
// ....do something with row
}
else {
// either 0 ...or more than 1 row
foo();
}
But you can even get rid of the call to mysqli_num_rows() (so it also works in case of unbuffered queries)
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if ( !$row ) {
// no such record
foo();
}
else {
// ....do something with row
// might want to check whether there are more matching records
// given the context there shouldn't, but ...
}

Not enter while loop after mysql_fetch_assoc

please take a look at this code :
$sql = "SELECT * FROM shop";
$result = mysql_query($sql);
echo $result;
echo "before lop";
while ($xxx = mysql_fetch_assoc($result)) {
echo "inside lop";
echo $xxx['column_name'];
}
echo "after lop";
When I run such code i receive :
Resource id #244
before lop
after lop
It did not enter while lop, and I really don't know why :(
I used before such code and there were no problems.
Can someone help me?
$sql = "SELECT * FROM shop";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($result);
Check how many records are present in your shop table. I think shop table is empty.That is why not entering in the while loop.
You can do like this
$count = mysql_num_rows($result);
if($count > 0) {
while ($xxx = mysql_fetch_assoc($result)) {
echo $xxx['column_name'];
}
}
I would guess that the call to mysql_fetch_assoc() has returned false, possibly due to no results being returned from the database, this would cause the while loop to not execute even once. I would check the output of var_dump(mysql_fetch_assoc($result)) to ensure that data has been returned.

how to add data to array using for loop in php?

I am making a mysql query . I want to add result to an array. suppose I am selecting all user from user table. I want to get everyones name. if the row=5 i want to save every name according to the row index.
if (mysql_num_rows($query) > 0)
{
$row = mysql_fetch_array($query);
//echo ($row);
$num=mysql_num_rows($query);
echo ($num);
for ($i=1; $i<=$num;$i++){
//here I want to save all name to an array.
}
Please help.
You might be looking for something like this:
$rows = array();
// while there are more records, add them to `$rows`
while($row = mysql_fetch_assoc($result)) {
$rows []= $row;
}
Note that mysql_fetch_assoc() will just return false if there are no (more) records in the result set. So you don't need the call to mysql_num_rows()
$num = mysql_num_rows($query);
if($num > 0)
{
while($row = mysql_fetch_array($query)
{
$names[] = $row['name'];
}
}
That will create an array called $names which you can loop through later. Also, it's a good time to look into mysqli_* functions, or PDO.

php mysql query if $num_results > 0

I want to make a php search query.
First put a sentence and explode every word get $name.
Then I put $name make a query to match all the name which exist in my database.
How to use a if ($num_results > 0) if I am not sure how many $name are matched and echo out?
(may be $row['name'] is null, maybe $row['name'] is 1 or 2, I want to get them one by one)
$query = mysql_query("SELECT * FROM books
WHERE name LIKE '$name[0]' OR name LIKE '$name[1]' OR name LIKE '$name[2]'
OR name like '$name[3]' ");
while ($row = mysql_fetch_array($query)) {
if ($num_results > 0) {
echo $row['name'][0] ;
}
if ($num_results > 1) {
echo $row['name'][1] ;
}
if ($num_results > 2) {
echo $row['name'][2] ;
}
if ($num_results > 3) {
echo $row['name'][3] ;
}
}
use:
mysql_num_rows($query);
to get the amount of rows you get from your mysql query
<?php if (mysql_num_rows($query)>0){?>
Do stuff
<? }else{ ?>
We didn't find anything!
<?php }?>
How about:
$i = 0
while($row = mysql_fetch_array($query)) {
echo $row['name'][$i];
$i++;
}
I didn't actually understood what you really want but your code should make more sense like this:
<?php
$searchphrase="alex nick george";
// this:
$names = explode(" ",$searchphrase);
// produces the following:
$names = array("alex","nick","george");
// so you can make the query:
if(is_array($names)){
$query = "SELECT * FROM books WHERE name IN ('".implode(",",$names)."') ORDER BY FIELD (name,".implode(",",$names).")";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) == 0){
// no results
}else{
while ($row = mysql_fetch_array($result)) {
echo $row['name'];
}
}
}
?>
Perhaps if you describe it better we can help more efficiently.

Categories