Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am having some problems with my program.
I am creating a script which displays all the properties in the properties table in our database and one row in the table should show available or sold. it should check if the puserId is null or 1 or more
//Execute the Query
$records = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($records)){
echo "<tr>";
echo "<td>".$row['propertyId']."</td>";
echo "<td>".$row['propNum']."</td>";
echo "<td>".$row['AptNum']."</td>";
echo "<td>".$row['street']."</td>";
if (empty(['puserId'])) {
$status = 'Available';
}else {
$status = 'Sold';
}
echo "<td>".$status."</td>";
When i use this it shows that all properties are sold, also the ones wich have a puserId of null. It doesn't give me any errors though.
Anybody knows how I should do this?
Thanks in advance :)
Replace:
if (empty(['puserId'])) {
with:
if (empty($row['puserId'])) {
in order to fix the missing variable $row typo (your actual code checks whether the array ['puserId'] is null, which is obviously false) . If your field is either null or numeric, you can also write that as:
if (is_null($row['puserId'])) {
but my advice is to avoid using empty/null values in table fields related to identifiers. Just give them a default value of 0 to make everything easier. At that point you could write your check as:
if ($row['puserId'] == 0) {
Other than the $row['puserId'] typo, empty doesn't really look useful there. It includes an isset check that is pointless considering you know that column exists. You aren't checking that any of the other columns are set before you use them. There's really no need to check that one either. Just evaluate it directly. null or 0 values will evaluate as false in the if condition..
if ($row['puserId']) {
$status = 'Sold';
} else {
$status = 'Available';
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 13 days ago.
Improve this question
I am converting all my php scripts due to moving to a new server. I am stumped as to why $row[0] is not working.
I am correctly getting $row populated as an array, and if I run a foreach on it, I get all the values populated just fine. But if, instead, I try to directly access the first value of the array as $row[0], I get nothing. Anyone know what?
$sql = "DESCRIBE USER";
$result = $dbh->query($sql);
$count=0;
while($row = $result->fetch_assoc()) {
print $row[0]; // this prints nothing
foreach($row as $column) {
print "$column"; // this works as expected
}
} #<-- while
fetch_assoc returns your results as a key value of column names and values, so you need to look at the $row['myColumn'] to get the value.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am a beginner in php , and i want make a test with GET .
I want display , for example "ok" on my php page if my id parameter is 1 but i have always 1 when i change the id parameter to another value .
Details :
when i make this url :
http://localhost:81/test/testajax.php?id=2
expected result :
not ok
obtained result :
ok
testajax.php
<?php
if($_GET["id"] = 1)
{
die('ok');
}
else
{
die('not ok');
}
?>
One equal sign (=) sets the value of a variable. $foo = "bar"; would set $foo to store bar.
You want to use two equal signs (==), which is a comparison operator. ($foo == "bar") would check to see if $foo is equal to bar.
You can check the different types of operators at http://php.net/manual/en/language.operators.php
May be you should go through this basics before you start.
you should put two equal signs to compare.
if($_GET["id"]==1)
correct code:
<?php
if($_GET["id"] == 1)
{
die('ok');
}
else
{
die('not ok');
}
?>
As you are setting the variable, the if statement is always equating to true
Just thought it was worth Noting this as that is the logical reason for your issue
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hello All: I do not have much experience with Arrays but am trying to learn. Trying something new and don't know if this can be done this way or not. Essentially what I am trying to do is go through a wp database table; and go through the records; the $postnum value is equivalent to a WP post_id and is in the database multiple times. So I want to just whip through the table and create an array with each post_id ($postnum) in there just once. So I thought, all i need to do is create an array outside of loop; then while in loop just check if the $postnum is already in the array; if it is not just add it…so then all records with that same postnum in them they will not add to the array…
here is my code:
$posts_counted=array();
global $wpdb;
foreach( $wpdb->get_results("SELECT * FROM wp_c93hh3bk23_pvc_daily") as $key => $row) {
$id = $row->id;
$time = $row->time;
$postnum = $row->postnum;
$postcount = $row->postcount;
if (in_array($postnum,$posts_counted))
{
array_push($posts_counted,$postnum);
}
} // close out foreach
I have never really used the "in_array" thing or the "array_push" thing either; so these are all new to me…
I am getting: "Parse error: syntax error, unexpected ' ' (T_STRING) in…"
referring to the line "if (in_array($postnum,$posts_counted))"
So I am wondering if anyone can tell me if what I am trying to do can be done this way or what I am doing wrong on this…
Thanks!
This is how I get data from db:
$sql= "Select * FROM table";
$result=mysql_query($sql);
while($rows = mysql_fetchArray($result)){
$data= $rows;
}
//checking if something is in array
if(in_array($data['id')){
//do something
}
//now how you want to get data
// echo json_encode($data)
//OUTPUT: {id:"1",name:"xyz"}
Anyway if you want your "postNum" unique, fix your sql with adding DISTINCT after SELECT.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to use an IF AND statement in PHP to test for
The submit button being pressed and
That my $testemail varable is equal to one, ie valid email in a PHP script.
If the Email is good , I get:
Thank you, 1 // ( this is correct) and email is sent
If the email is bad I get:
Thank you, 0. // ( this is not correct ) and email is still trying to be sent with bad email address.
Because the script is outputting the 0 or 1 properly, I am certain it has tested the email and correctly deciding if it is good or bad ( hence the '0' and the '1') but the script never goes to the else statement. I can't seem to get the IF AND statement right. I have tried "1" and '1'.
Very confused. Thanks in advance.
if(!filter_var($emailtmp, FILTER_VALIDATE_EMAIL))
{
$testemail = "0";
}
else
{
$testemail = "1";
}
global $testemail;
var_dump($testemail);
if (isset($_POST['submit']) && ($testemail = 1) )
{
//Everything is good, proceed
sendEmail(preg_replace(array('/<td>.*href="\?remove.*<\/td>/i', '/<th.*
<\/th>/i'), '', $_SESSION['SHOPPING_CART_HTML']));
$result_message = "Thank You. $testemail ";
}
elseif (isset($_POST['submit']))
{
//Missing Required Information
$result_message = "You must use a Valid email addresse, $testemail ";
}
($testemail = 1)
should be
($testemail === 1)
Having just ($testemail = 1) simply sets the value of testemail to 1 (which for the purposes of the if, is true since its successful). == or better yet the more exact === is what you want for checking if the left & right values match.
The problem is you are assigning the value 1 to $testemail instead of comparing it.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have written a code where a value is got from mysql and if it is 1 then row should be in red colour and if it is 0 the row should be in green colour. When I execute the code it always goes to the else statement.
Code is as below:
while ($row = mysqli_fetch_array($result))
{
$bprofit=$row['profit_loss'];
if ($bprofit == "1") {
$colour='#FF0000';
} else {
$colour='#31B404';
}
echo "<tr bgcolor=$colour>";
echo "<td>" . $bprofit . "</td>";
}
and output is:
1
0
0
all in green colour only.
Any suggestions?
You have an extra p in $bpprofit:
if ($bprofit == "1")
change if ($bpprofit == "1") to if ($bprofit == "1")
In addition to the misspelling mentioned in other answers, I think you need to reference the first index of the array like so:
if ($bprofit[0] == "1") ...
EDIT
Based on your new code, are you sure the value that is being returned is a string and not numeric? If it is numeric, you would want your if statement to look like this:
if ($bprofit == 1) ...
In situations like this it never hurts to use var_dump() function and see what the variable actually contains and how you need to access it.
When encountering problems such as the one above, try activating error reporting in PHP. One way to do this is to update php.ini, but since this might affect other projects in which you don't want error reporting, it might be better to activate it when needed.
What's important is that you enable reporting of notices, since they'll appear when you use a variable that hasn't been set.
The code below is a simple example:
<?php error_reporting(E_ALL);
if ($undefined == 1) {
// do stuff
}
When this page is displayed, a notice will be shown:
Notice: Undefined variable: undefined in
/var/www/test/undefined_example.php on line 3
Of course, this might not be a suitable solution for all problems, but it might help you figure out where to look.
It might also make you a better programmer; by "reminding" you to declare variables with a standard value if they are not always assigned before later use.