0
<?php  

include('../Adress/includes/mysql_connection_recepti.php');

if (isset($_POST['username'])){

$conn = dbConnect($_POST['username']);


$city=$_POST['city'];

$sql= 'SELECT * FROM adress_table WHERE city="'.$city.'"';

$result=mysql_query($sql);

$numRows=mysql_num_rows($result);

?>

<p><?php echo $numRows; ?> records is found.</p>

<table>
<tr>
<th>Number</th>
<th>Name</th>
<th>Last Name</th>
<th>City</th>
<th>Country</th>
<th>Date</th>
</tr>

<?php 
$n=0;

while($row = mysql_fetch_assoc($result)){
//print_r($row);

$n++;

?>

<tr>
<td><?php echo $n . ".";?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['last_name'] ?></td>
<td><?php echo $row['city'];?></td>
<td><?php echo $row['country'] ?></td>
<td><?php echo $row['date'] ?></td>
</tr>

<?php }?>

</table>

<?php }?>
</body>
<body>

<form action="" method="post">
<input type="text" id="username" name="username"/>


<select name="city">
<option value="new_york">New York</option>
<option value="london">London</option>
<option value="belgrade">Belgrade</option>
<option value="zagreb">Zagreb</option>
<option value="moscow">Moscow</option>

<input type="submit" value="Find">
</select>
</body>

As you can see, my sql statement tells that every column has to be displayed when city="'.$city.'", and that is completely ok. But when I try to display some particular column(s), not all (*), for example

$sql= 'SELECT name,last_name FROM adress_table WHERE city="'.$city.'"';

then error emerges, bacause

 <td><?php echo $row['city'];?></td>
 <td><?php echo $row['country'] ?></td>
 <td><?php echo $row['date'] ?></td>

are not defined. In order to fix this I put @ sign

 <td><?php @ echo $row['city'];?></td>
 <td><?php @ echo $row['country'] ?></td>
 <td><?php @ echo $row['date'] ?></td>

to cover up error messages and I cat tell you that everything looks perfectly all right, but I don't know is it good practice. What should I do? Thanks.

3
  • 2
    The usual way to prevent this is checking each variable with php.net/isset but why are you trying to output columns that you're not selecting in the first place? I don't see how that would ever make sense. Commented Jun 30, 2013 at 8:48
  • Because I imagine that in sql statement name of columns that are to be displayed could be dynamically chosen by user, but I didn't show that in this code. Commented Jun 30, 2013 at 9:05
  • Can't you limit the displaying of columns using the same system you use to limit the querying? Commented Jun 30, 2013 at 9:12

2 Answers 2

2

You can just use an if to check if there is something in there or not:

<?php echo isset($row["city"]) ? $row["city"] : "" ?>
Sign up to request clarification or add additional context in comments.

3 Comments

Cant believe we added same answer :)
Yeah. Well I think he got the idea ^_^
Thanks, I think I did it in the first place, it seems obvious to me to try that, but something went wrong. I'll try it again, thanks!
1

You are trying to display a column that is not present in your SQL query. YOu can use something like -

<?php echo isset($row["city"]) ? $row["city"] : "" ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.