bchandel Posted August 18, 2009 Share Posted August 18, 2009 I have the following php code file which runs fine. However for E-mail, Web and Profile fields I want to print - in the table where no data exists. I would appreciate if some one can fix this code or can make it simple to work. Basically I have a database having say Name, E-mail, Web and Profile fields. Now I want to display this data in a table where for E-mail, Web and Profile I want to show E-mail, Web and Profile instead long hyperlinks. I want to print - for the records where there is no data. Thank you! <html> <body> <?php <h1><font color="#0000FF"><left>Search Results:</left></font></h1> <table border="1" width="100%" cellspacing="5" cellpadding="0"> <tr> <th><font face="Arial, Helvetica, sans-serif">Name</font></th> <th><font face="Arial, Helvetica, sans-serif">Last Name</font></th> <th><font face="Arial, Helvetica, sans-serif">First Name</font></th> <th><font face="Arial, Helvetica, sans-serif">Phone</font></th> <th><font face="Arial, Helvetica, sans-serif">Mobile</font></th> <th><font face="Arial, Helvetica, sans-serif">E-mail</font></th> <th><font face="Arial, Helvetica, sans-serif">Web</font></th> <th><font face="Arial, Helvetica, sans-serif">State</font></th> <th><font face="Arial, Helvetica, sans-serif">Country</font></th> <th><font face="Arial, Helvetica, sans-serif">Profile</font></th> </tr> <?php $i=0; while ($i < $num) { $Name=mysql_result($result,$i,"Name"); $LastName=mysql_result($result,$i,"LastName"); $FirstName=mysql_result($result,$i,"FirstName"); $Phone=mysql_result($result,$i,"Phone"); $Mobile=mysql_result($result,$i,"Mobile"); $Email=mysql_result($result,$i,"Email"); $Web=mysql_result($result,$i,"Web"); $State=mysql_result($result,$i,"State"); $Country=mysql_result($result,$i,"Country"); $Profile=mysql_result($result,$i,"Profile"); ?> <tr> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $Name; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $LastName; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $FirstName; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $Phone; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $Mobile; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"> <a href="mailto:<? echo "$Email"; ?>" ?>">E-mail</a></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"> <a href="<? echo "$Web"; ?>" ?>">Web Site</a></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $State; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $Country; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"> <a href="<? echo "$Profile"; ?>" ?>">Profile</a></font></td> </tr> <?php $i++; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/170769-need-help-with-php-code/ Share on other sites More sharing options...
MadTechie Posted August 18, 2009 Share Posted August 18, 2009 Is this all of the code ? as your missing the database connection, database selection, and the database query here's a quick example of using a database <?php $conn = mysql_connect("localhost", "mysql_user", "mysql_password"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("mydbname")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $sql = "SELECT id as userid, fullname, userstatus FROM sometable WHERE userstatus = 1"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } // While a row of data exists, put that row in $row as an associative array // Note: If you're expecting just one row, no need to use a loop // Note: If you put extract($row); inside the following loop, you'll // then create $userid, $fullname, and $userstatus while ($row = mysql_fetch_assoc($result)) { echo $row["userid"]; echo $row["fullname"]; echo $row["userstatus"]; } mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/170769-need-help-with-php-code/#findComment-900616 Share on other sites More sharing options...
bchandel Posted August 18, 2009 Author Share Posted August 18, 2009 Thank you for your help. I have the database connection. I am having problems with E-mail, Web, and Profile fields. Below is the complete code. Email, Web, and profile fields contain hyperlinks data. I just want to display Email, Website and Profile for this and not long hyperlink. I want to print - for the results where the data doesn't exists in the data base. I hope this will help you understanding the problem. Thank you! <html> <body> <?php $db_connect = mysql_connect("xxx.com", "xxxx_user", "password"); mysql_select_db("xxxx_database") or die( "Unable to select database"); $query="SELECT * FROM Directory"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <h1><center>Chandel's directory</center></h1> <table border="0" width="100%" cellspacing="2" cellpadding="2"> <tr> <th><font face="Arial, Helvetica, sans-serif">Name</font></th> <th><font face="Arial, Helvetica, sans-serif">Last Name</font></th> <th><font face="Arial, Helvetica, sans-serif">First Name</font></th> <th><font face="Arial, Helvetica, sans-serif">Phone</font></th> <th><font face="Arial, Helvetica, sans-serif">Mobile</font></th> <th><font face="Arial, Helvetica, sans-serif">E-mail</font></th> <th><font face="Arial, Helvetica, sans-serif">Web</font></th> <th><font face="Arial, Helvetica, sans-serif">State</font></th> <th><font face="Arial, Helvetica, sans-serif">Country</font></th> </tr> <?php $i=0; while ($i < $num) { $Name=mysql_result($result,$i,"Name"); $LastName=mysql_result($result,$i,"LastName"); $FirstName=mysql_result($result,$i,"FirstName"); $Phone=mysql_result($result,$i,"Phone"); $Mobile=mysql_result($result,$i,"Mobile"); $Email=mysql_result($result,$i,"Email"); $Web=mysql_result($result,$i,"Web"); $State=mysql_result($result,$i,"State"); $Country=mysql_result($result,$i,"Country"); ?> <tr> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $Name; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $LastName; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $FirstName; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $Phone; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $Mobile; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo $Email; ?>">E-mail</a></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><a href="<? echo $Web; ?>">Website</a></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $State; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $Country; ?></font></td> </tr> <?php $i++; } ?> </body> </html> Is this all of the code ? as your missing the database connection, database selection, and the database query here's a quick example of using a database <?php $conn = mysql_connect("localhost", "mysql_user", "mysql_password"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("mydbname")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $sql = "SELECT id as userid, fullname, userstatus FROM sometable WHERE userstatus = 1"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } // While a row of data exists, put that row in $row as an associative array // Note: If you're expecting just one row, no need to use a loop // Note: If you put extract($row); inside the following loop, you'll // then create $userid, $fullname, and $userstatus while ($row = mysql_fetch_assoc($result)) { echo $row["userid"]; echo $row["fullname"]; echo $row["userstatus"]; } mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/170769-need-help-with-php-code/#findComment-900622 Share on other sites More sharing options...
bubbasheeko Posted August 18, 2009 Share Posted August 18, 2009 Hi! Not sure why you are using a row counter to complete your loop. You should be doing this: <html> <body> <?php $db_connect = mysql_connect("xxx.com", "xxxx_user", "password"); mysql_select_db("xxxx_database") or die( "Unable to select database"); ?> <h1><center>Chandel's directory</center></h1> <table border="0" width="100%" cellspacing="2" cellpadding="2"> <tr> <th><font face="Arial, Helvetica, sans-serif">Name</font></th> <th><font face="Arial, Helvetica, sans-serif">Last Name</font></th> <th><font face="Arial, Helvetica, sans-serif">First Name</font></th> <th><font face="Arial, Helvetica, sans-serif">Phone</font></th> <th><font face="Arial, Helvetica, sans-serif">Mobile</font></th> <th><font face="Arial, Helvetica, sans-serif">E-mail</font></th> <th><font face="Arial, Helvetica, sans-serif">Web</font></th> <th><font face="Arial, Helvetica, sans-serif">State</font></th> <th><font face="Arial, Helvetica, sans-serif">Country</font></th> </tr> <?php $query="SELECT * FROM Directory"; $query_db=mysql_query($query); while ($results = $mysql_fetch_assoc($query_db)) { ?> <tr> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['Name']; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['LastName']; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['FirstName']; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['Phone']; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['Mobile']; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo $results['Email']; ?>">E-mail</a></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><a href="<? echo $results['Web']; ?>">Website</a></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['State']; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['Country']; ?></font></td> </tr> <?php } ?> </table> </body> </html> <?php mysql_close(); // or you could use mysql_free_result($query_db); ?> Quote Link to comment https://forums.phpfreaks.com/topic/170769-need-help-with-php-code/#findComment-900630 Share on other sites More sharing options...
bchandel Posted August 18, 2009 Author Share Posted August 18, 2009 Thank you for your help. Getting following error: Fatal error: Function name must be a string in /home/XXXXX/public_html/try/try2.php on line 26 See if it can be fixed. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/170769-need-help-with-php-code/#findComment-900637 Share on other sites More sharing options...
HoTDaWg Posted August 18, 2009 Share Posted August 18, 2009 change: <?php while ($results = $mysql_fetch_assoc($query_db)) { ?> to: <?php while ($results = mysql_fetch_assoc($query_db)) { not sure if itll fix the problem, not even sure THAT is line 26, but thats gotta be an error. did you even look at line 26:S? Quote Link to comment https://forums.phpfreaks.com/topic/170769-need-help-with-php-code/#findComment-900654 Share on other sites More sharing options...
bchandel Posted August 18, 2009 Author Share Posted August 18, 2009 Thank you for your message and help. This is giving me the following error message. I will appreciate if you can check the whole script and figure out the problem. I tried changing <?php while ($results = $mysql_fetch_assoc($query_db)) { ?> to: <?php while ($results = mysql_fetch_assoc($query_db)) { It does not work. Here is the error message: Fatal error: Function name must be a string in /home/XXXXX/public_html/try/try2.php on line 24 and here is line 24 while ($results = $mysql_fetch_assoc($query_db)) { and here is the comlete script: <html> <body> <?php $db_connect = mysql_connect("xxx.com", "xxxx_user", "password"); mysql_select_db("xxxx_database") or die( "Unable to select database"); ?> <h1><center>Chandel's directory</center></h1> <table border="0" width="100%" cellspacing="2" cellpadding="2"> <tr> <th><font face="Arial, Helvetica, sans-serif">Name</font></th> <th><font face="Arial, Helvetica, sans-serif">Last Name</font></th> <th><font face="Arial, Helvetica, sans-serif">First Name</font></th> <th><font face="Arial, Helvetica, sans-serif">Phone</font></th> <th><font face="Arial, Helvetica, sans-serif">Mobile</font></th> <th><font face="Arial, Helvetica, sans-serif">E-mail</font></th> <th><font face="Arial, Helvetica, sans-serif">Web</font></th> <th><font face="Arial, Helvetica, sans-serif">State</font></th> <th><font face="Arial, Helvetica, sans-serif">Country</font></th> </tr> <?php $query="SELECT * FROM Directory"; $query_db=mysql_query($query); while ($results = $mysql_fetch_assoc($query_db)) { ?> <tr> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['Name']; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['LastName']; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['FirstName']; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['Phone']; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['Mobile']; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo $results['Email']; ?>">E-mail</a></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><a href="<? echo $results['Web']; ?>">Website</a></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['State']; ?></font></td> <td align="center"><font face="Arial, Helvetica, sans-serif"><? echo $results['Country']; ?></font></td> </tr> <?php } ?> </table> </body> </html> <?php mysql_close(); // or you could use mysql_free_result($query_db); ?> Quote Link to comment https://forums.phpfreaks.com/topic/170769-need-help-with-php-code/#findComment-900701 Share on other sites More sharing options...
bchandel Posted August 18, 2009 Author Share Posted August 18, 2009 It seems working. I have made the following change. while ($results = $mysql_fetch_assoc($query_db)) { to while ($results = mysql_fetch_assoc($query_db)) { However, I need to fix one problem. I want to print - for records where the data does not exist. See if someone can help me with this. You can visit http://chandel.com/try/try2.php to see the problem. In the last record for website and profile there is np data. However the record is displaying value from my site url and display directory structure. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/170769-need-help-with-php-code/#findComment-900706 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.