PHP If statement doesn't append variable properly [closed] - php

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
I am querying my database and dumping the data into an HTML table. One of my database keys is called "time_expire", with a value of -1 meaning never. Therefore, to show something to the effect of "never" in the HTML table instead of the raw data I am attempting to change the variable before it is echo'd.
This is my code
<?php
$sql = "SELECT * FROM penalties ORDER BY id DESC";
$result = mysql_query($sql);
while ($rows = mysql_fetch_array($result)) {
if ($rows['time_expire'] = '-1') {
$rows['time_expire'] = '<span class="label label-important">Permanent</span>';
}
echo "<tr>";
echo '<td>' . $rows['id'] . '</td>';
echo "<td>" . $rows['client_id'] . "</td>";
echo "<td>" . $rows['type'] . "</td>";
echo "<td>" . date('m/d/Y', $rows['time_add']) . "</td>";
echo "<td>" . $rows['duration'] . "</td>";
echo "<td>" . $rows['time_expire'] . "</td>";
echo "<td>" . $rows['reason'] . "</td>";
echo "</tr>";
}
?>
This code does not error, however every row in my HTML table has an expiration value of "never", even if the the raw data is different.

This seems to be a common typo in the if statemenet
if ($rows['time_expire'] = '-1') [
^
|
If you want to the interpreter spot these for you, try using yoda conditions like this:
if ('-1' = $rows['time_expire'])
This form will create error however the correct forms both work ok:
'-1' == $rows['time_expire']
or
$rows['time_expire'] == '-1'

I think it is supposed to be:
if ($rows['time_expire'] == '-1')
not
if ($rows['time_expire'] = '-1')

First what type is time_expire?
If time_expire is numeric then you should change the condition to:
if ($rows['time_expire'] == -1) which will evaluate properly.
Secondly your condition checking is actually assigning the value to $rows['time_expire'] with single =, it should be double = like ==.

Try changing If condition to if ($rows['time_expire'] == '-1'). Right now what your condition says that variable $rows['time_expire'] is equal to '-1' which is always returning true.It might get you in the unlimited looping.

Related

How do I show only the column name of the SQL table [closed]

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 3 years ago.
Improve this question
Currently my code consists of
$sql = "SHOW columns FROM tblexercise";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach ($row as $field => $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
This allows me to show the column names but it also includes all the attributes and types etc.
is there any way to show just the column name?
You don't need a loop inside a loop. You only need one foreach loop and then you can access the key Field which holds the name of the column. The query SHOW COLUMNS is explained in the MySQL doc. You can check in that link what are the results of this query and their sample values. Then you can decide which values you want to access.
$result = $conn->query("SHOW columns FROM tblexercise");
foreach ($result as $row) {
echo "<tr>";
echo "<td>" . $row['Field'] . "</td>";
echo "</tr>";
}

Syntax error when trying to print something (PHP/SQL) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I am a beginner/terrible when it comes to PHP/SQL, keep that in mind. Ok, so what I am trying to do is print some "values" or whatever you could call them with PHP/SQL, like this:
<?PHP
require_once
?>
Now this works just fine.
Just add them like you added the rest of your select columns.
require_once("dbb.php");
$SQL="select
date_format(timestamp,'%Y %M %d') as datum,
arena.namn as arena,
concat(hemma.lagnamn,'-',borta.lagnamn)as lag,
(select count(*) from mål where matchID=matchID and hemma.lagID=hemma) as h,
(select count(*) from mål where matchID=matchID and borta.lagID=borta)as b,
publiksiffror,
hemmagoal,
bortagoal
from
matcher
inner join
lag as hemma
on hemma.lagID = hemma
inner join
lag as borta
on borta.lagID = borta
inner join
arena
on matcher.arena=arena.arenaID;";
$result = mysql_query($SQL) or die(mysql_error());
while($rad = mysql_fetch_row($result))
{
echo "<tr>";
echo "<td>" . $rad[0] . "</td>";
echo "<td>" . $rad[2] . "</td>";
echo "<td>" . $rad[3] . "</td>";
echo "<td>" . $rad[4] . "</td>";
echo "<td>" . $rad[1] . "</td>";
echo "<td>" . $rad[5] . "</td>";
echo "</tr>";
}
?>

assign value to checkbox using loop php [closed]

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
The $file-name should be for example: a.pdf,b.pdf,c.pdf,d.pdf respectively.
But why I cannot assign the value into the checkbox?
please help/__\
while($row = Mysqli_fetch_array($result))
{
$file-name = $row['Name'] . ".pdf";
echo "<tr>";
echo "<td><input type='checkbox' name='q1[]'value=<?php echo $filename ?>' > </td>";
}
Per the comments above, try the following:
while($row = Mysqli_fetch_assoc($result))
{
$filename = $row['Name'] . ".pdf";
echo "<tr>";
echo "<td><input type='checkbox' name='q1[]' value='".$filename."' /> </td>";
}
As the comments to your question mentioned you were assigning $file-name (an invalid variable name in PHP) and trying to use a (presumably) unassigned variable: $filename you also were missing an opening single-quote for the value attribute which could prevent it from properly rendering, and you can use string concatenation instead of trying to echo-inside-an-echo.

Echo this string properly so i have something to study from [closed]

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 9 years ago.
Improve this question
I have table and i know how to do most of the echo strings, BUT when i get a long one that include words, and headings i get lost. So how do i convert this line?
<a class="signUpButton" href="#">Sign Up</a>
Take this line and get it to work in the below code which is only partial code.
echo "<tr>";
echo "<td>" . $row->name . "</td>";
echo "<td>" . $row->monthly . "</td>";
echo "<td>" . $row->annually . "</td>";
echo "</tr>";
echo "</tr>";
}
echo "</tbody>";
echo "<tfoot>";
echo "<tr>";
echo "<td>";
echo "<a class='signUpButton','href='#'>";
echo "</a>";
echo "</td>";
echo "<td>";
echo "<a class='signUpButton','href='#'>";
echo "</a>";
echo "</td>";
echo "<td>";
echo "<a class='signUpButton','href='#'>";
echo "</a>";
echo "</td>";
echo "</tr>";
echo "<tfoot>";
This code here is what i have now.
echo "<a class='signUpButton','href='#'>";
it does not work either so something is up. only part that works is everything from the echo to the ',' it closes fine, im just not able to see my mouse pointer show that indeed it is a link..
P.S. this code above is only a section, the table is huge, and is working except for the minor issues i am having with links and displaying the button words "signup"
I'm not quite sure what you're asking, but if you need to echo out:
<a class="signUpButton" href="#">Sign Up</a>
then use:
echo '<a class="signUpButton" href="#">Sign Up</a>';
or:
echo "<a class=\"signUpButton\" href=\"#\">Sign Up</a>";
The first method uses single quotes in the echo statement allowing the use of double quotes inside
The second method uses double quotes for the echo statement so uses a \ character to escape the " inside the echo so it doesn't close it.

How to fetch MySQL data in PHP table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to fetch some records from MySQL view table in my web page by PHP. I am using this following code:
$result="SELECT method,test_name,test_code FROM vtest_ord ORDER BY
test_ord_id DESC";
echo"<table border='1'><thead><tr><th>Method</th><th>Test Name</th><th>Test
Code</th></tr></thead>";
while ($row=$mysql_fetch_assoc($result)) {
echo "<tr><td>";
echo "<td>" . $row['method'] . "</td>";
echo "<td>" . $row['test_name'] . "</td>";
echo "<td>" .
$row['test_code'] . "</td>";
echo "</tr></td>"; }
echo '</table>';
but I am getting this error:
Fatal error: Function name must be a string in
C:\xampp\htdocs\test2\ord_view.php on line 211 Method Test Name Test
Code (table header shows)
You have some problem with the PHP syntax.
mysql_fetch_assoc is a function and you called it like a variable (which means nothing here).
You try to call this function with a string, but you need a ressource, obtain via mysql_query.
You don't need to write multiple echo statement, thanks to the concatenation syntax.
A potential correct code is:
$sql="SELECT method, test_name, test_code FROM vtest_ord ORDER BY test_ord_id DESC";
echo "[Debug] MethodTest NameTest Code";
$result = mysql_query($sql) or exit(mysql_error()); // Display SQL error if raised
while ($row=mysql_fetch_assoc($result)) {
echo $row['method'] . " " . $row['test_name'] . " " . $row['test_code']; echo "";
}
I also advise you to drop mysql_* and use PHP PDO, since the first one is deprecated for years now.
You need basic php logic. you have some serious errors in your code. please try to read basic of programming.
some errors:
1) $mysql_fetch_assoc is not a variable it is a function mysql_fetch_assoc(); you can not use $ in this name.
2) you need to execute query first then you can fetch.
$link = mysql_connect('host_name','db_user_name','db_password'); //fill correct credentials
mysql_select_db('your_db_name'); //set your database name
$sql="SELECT method,test_name,test_code FROM vtest_ord ORDER BY
test_ord_id DESC";
echo"<table border='1'><thead><tr><th>Method</th><th>Test Name</th><th>Test
Code</th></tr></thead>";
$result = mysql_query($sql); // execute query
while ($row=$mysql_fetch_assoc($result)) {
echo "<tr><td>";
echo "<td>" . $row['method'] . "</td>";
echo "<td>" . $row['test_name'] . "</td>";
echo "<td>" .
$row['test_code'] . "</td>";
echo "</tr></td>"; }
echo '</table>';
You forgot to call mysql_query method.
Then you could use the returned resource with the mysql_fetch_assoc function in which you have a typo. You're treating it as a variable and not a function.
Try this, You need execute the query using mysql_query, Also, mysql_fetch_assoc not a $mysql_fetch_assoc function
$query ="SELECT method,test_name,test_code FROM vtest_ord ORDER BY test_ord_id DESC";
$result = mysql_query($query) or die(mysql_error());
Should be
while ($row=mysql_fetch_assoc($result)) {
instead of
while ($row=$mysql_fetch_assoc($result)) {
Note: Use mysqli_* functions or PDO instead of using mysql_* functions(deprecated)
Here you can find good tutorial for connecting mysql database: http://us2.php.net/manual/en/mysqli.query.php

Categories