Solved

Checking to see if DB cell contains data or not

Posted on 2004-09-19
18
261 Views
Last Modified: 2006-11-17
Hi,

<?php print mysql_result($result,2,"domain");?>

How do I convert the line above so it only prints the line if the database read attempt has data and does not return an error ?  i.e. if database only has 2 rows and I attempt to access the third row which does not exist ??

Thanx
0
Comment
Question by:MirageSF
  • 7
  • 6
  • 5
18 Comments
 
LVL 32

Expert Comment

by:ldbkutty
ID: 12097788
for($i=0; $i<mysql_num_rows($result); $i++) {
   print mysql_result($result,$i,"domain");
}

Howver, if you have large data-sets, its better to go with mysql_fetch_row() or mysql_fetch_array()
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12097793
Hi MirageSF,

You can check if 2 is greater than mysql_num_rows($resultofyourquery)

Also, you can do this:

while ($row=mysql_fetch_array($resultofyourquery)) {
   echo($row["domain"]);
}

Regards,
Zyloch
0
 

Author Comment

by:MirageSF
ID: 12097794
Also, how do I set the output text as a LINK so that people can click on the outputed db code and goto another page ?
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 12097801
while ($row=mysql_fetch_array($result)) {
   echo "<a href='otherpage.php'> $row['domain'] </a>";
}
0
 

Author Comment

by:MirageSF
ID: 12097802
Im putting the values directly into a table using individual PHP code segments, a FOR or WHILE loop would cause problems, I need an IF statement of some kind that actually checks if that row has data in the first column if so print if not then do nothing

Thanx
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12097815
Just make sure you keep the $results of the mysql query, like this:

$res1=mysql_query("your query");

Then, every time you need a new row, compare the new row number with mysql_num_rows($res1);
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 12097821
>> I need an IF statement of some kind that actually checks if that row has data in the first column if so print if not then do nothing

while ($row=mysql_fetch_array($result)) {
   if(!empty($row['domain']))
        echo "<a href='otherpage.php'> $row['domain'] </a>";
}

Is this something what you want ?
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12097829
If empty doesn't work, and it should, change !empty to isset
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 12097848
"domain" is column name, so it is always SET inside the while loop, yeah?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:MirageSF
ID: 12097858
got thid error from last segment of code...


Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 12097864
i dont see any errors with that piece of code. if the error is showed in line 10(for example), please post your code from line 7 till 12.
0
 

Author Comment

by:MirageSF
ID: 12097885
                     <td width="39"><span class="style4">Server</span></td>
                      <td width="44"><span class="style4">Created</span></td>
                      <td width="59"><span class="style4">Status</span></td>
                      <td width="30"><span class="style4"></span></td>
                    </tr>
                    <tr bgcolor="#fafafa">
                      <td bgcolor="#fafafa"><span class="style2 style10"><?php while ($row=mysql_fetch_array($result)) {
   if(!empty($row['domain']))
        echo "<a href='otherpage.php'> $row['domain'] </a>";
}

?></span></td>
                      <td><span class="style4">bloggs</span></td>
                      <td><span class="style4">Virtual I </span></td>
                      <td><span class="style4">Delta</span></td>
                      <td><span class="style4">10-10</span></td>
                      <td><span class="style4">ACTIVE</span></td>
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 12097907
Try this:
echo "<a href=\"otherpage.php\">" . $row["domain"] . "</a>";
0
 

Author Comment

by:MirageSF
ID: 12097955
ok works now, but produces a line of results, basically I need to tell it to look in row 0 and if data print result, on another row of the table I get it to look at row 1 and print results, then I go on and on.

Basically on my own table created in html I use...

echo "<a href='otherpage.php'>";print mysql_result($result,0,"domain");echo "</a>"

and

echo "<a href='otherpage.php'>";print mysql_result($result,1,"domain");echo "</a>"

for example and they work great if there are 2 tables, as soon as I use (which is empty) I get an error plastered within the table..

echo "<a href='otherpage.php'>";print mysql_result($result,2,"domain");echo "</a>"

If it was possible to do...


   if(!empty(mysql_result($result,0,"domain")))
echo "<a href='otherpage.php'>";print mysql_result($result,0,"domain");echo "</a>"

that would do the trick, but it produces errors like that
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12098071
Why can't you do it like Idbkutty says with the while ($row=mysql_fetch_array($result)) ?

That's basically the standard way to do it. If your links are different or whatever, you can make another array to correspond to it.
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 50 total points
ID: 12098104
probably, you miss " ; " (semi-colon) at the end. BTW, try this:

if(isset(mysql_result($result,0,"domain")) && !empty(mysql_result($result,0,"domain")))
     echo "<a href=\"otherpage.php\">" " . mysql_result($result,0,"domain") . "</a>";
0
 

Author Comment

by:MirageSF
ID: 12098832
How do I change the link so it matches the domain within the DB ?

<?php echo "<a href='http://www.'>"; print mysql_result($result3,0,"domain");echo "</a>";?>

Sorry for all the little questions, but im starting to make sense of alot of stuff and have pretty much got what I need working, about 5% left and im done me thinks, not bad for a days work ;)

Thanx
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12099334
You mean

<?php echo "<a href='" . mysql_result($result3,0,"domain") . "'>" . mysql_result($result3,0,"domain") . "</a>"; ?>
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Password forgotten. 3 40
How can define this $_GET["page"]  variable on my page? 5 32
Append letter to end of string 4 36
WordPress TK Title 8 23
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now