Suppose there is a table in which 25 fields, 16 of them have an empty value. How to process all fields with an empty value using php to give them the same value, for example, no information?
4 answers
$query = 'SELECT * FROM table'; $res = mysql_query($query); while($row = mysql_fetch_assoc($res)) { while(list($key,$value) = each($row)) { if(empty($value)) { $row[$key] = 'Пустое значение'; } } $data[] = $row; } something like this...
|
while ($result = mysql_fetch_array($data)){ foreach ($result as $v){ if ($v=='') { echo "Нет информации"; } else { echo $v; //Поле с данными } } } But the disadvantage is that the fields will be displayed in the order in which they are in the base table.
- Well, this is not the essence of it :) <br> It is important to understand that someone should go through the sampling results in a cycle ... - Zowie
|
SELECT ISNULL (field, 'no info') FROM table
- This is also an option, maybe sometimes the best, only annoyance - you need to list all the fields ^^ <br> True if it is not a good idea to write a procedure, but again - the table structure has changed - rewrite the structure, and how many fields are called in the loop on the drum Generally ... - Zowie
- you can issue this request as VIEW - renegator
- And what is it princely different from the procedure? <br> Absolutely nothing, I already wrote above that if the table structure changes, then there is a chance that you will have to rewrite the view request ... But in a cycle there are no such problems, although theoretically for true perverts You can set a loop in the SQL procedure, but faster than php it won't work all the same ... - Zowie
|
Or maybe it makes sense to do it with MySQL - which is faster in my opinion.
- I do not like this phrase, but this is really saving on matches - Zowie
- Yes, I agree - saving on matches) But I try everything regarding the sampling from the database to the maximum to display the already processed data - Ale_x
- That is not a question, I generally have been writing procedures for almost all operations with MySQL, but in fact - it's easier for me, I only win by being 100% secure from SQL injections ... I don’t win in speed ( true as for the speed of development, I win all the same: D) <br> But there is no point in this case IMHO ... <br> Regarding speed, this is not faster;) - Zowie
|