PHP forums, MySQL forums, Web Development resources

Home PageHome    PHP ResourcesTopic List    FAQFAQ    SearchSearch    MemberlistMemberlist    UsergroupsUsergroups 
 RegisterRegister
    ProfileProfile    Log in to check your private messagesLog in to check your private messages    Download the RSS Reader RSS Feed Download the RSS Reader RSS for this forum Log inLog in 

PHP Forum :: MySQL Forum :: Java Script Forum



Accessing database
Goto page 1, 2  Next
 
Post new topic   Reply to topic    WeberForums.com Forum Index -> MySQL General
View previous topic :: View next topic  
Author Message
rdub



Joined: 07 Nov 2006
Posts: 93

PostPosted: Thu Feb 14, 2008 12:55 am    Post subject: Accessing database Reply with quote

Looking for a simple script just work out the database connections and query/write from the table.
Back to top
View user's profile Send private message
lostboy



Joined: 02 May 2004
Posts: 5128
Location: toronto, canada

PostPosted: Thu Feb 14, 2008 2:11 am    Post subject: Reply with quote

Code:

<?

//db conn stuff
mysql_connect('host', 'user', 'passwrd') or die ("Error connecting to Database");

$sql = "select * from accomm_info ";

$result = mysql_query($sql) or die ("can't complete query". mysql error());

if (($result) && (mysql_num_rows($result)>0)){
   
   //set the path and file name
   $dir="/path/to/file/";
   $filename = $dir.'test.txt';

   // Let's make sure the file exists and is writable first.
   if (is_writable($filename)) {

   if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
   }

   while ($rows = mysql_fetch_array($result)){
      
       $content = "";
       //calc the length of the line (number of elements)
         for($x = 0; $x < count($rows); $x++){

            $content .= $rows[$x] . " | ";
         
         }//next

      $content = substr($content, 0, strlen($content)-2). "\n"; //remove the last pipe as its not needed and adds the new line character

         // Write $content to our opened file.
         if (fwrite($handle, $content) === FALSE) {
             echo "Cannot write to file ($filename)";
             exit;
         }
   }//end while
   
   fclose($handle);
   
   //call the function to force the download the users computer
   force_download($filename);
                   
} else {
   echo "The file $filename is not writable";
}//end if

function force_download($file)
{
   $dir="/path/to/file/";
   if (isset($_REQUEST["file"])) {
      $file=$dir.$_REQUEST["file"];
      header("Content-type: application/force-download");
      header("Content-Transfer-Encoding: Binary");
      header("Content-length: ".filesize($file));
      header("Content-disposition: attachment; filename="".basename($file).""");
      readfile("$file");
   } else {
      echo "No file selected";
   } //end if

}//end function
?>

_________________
Lostboy

Cat, the other other white meat

Please read Posting Etiquette before posting

You can always try Google
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
rdub



Joined: 07 Nov 2006
Posts: 93

PostPosted: Thu Feb 14, 2008 3:12 am    Post subject: Reply with quote

I'm sorry. Should I have posted the script I've been using? I've used it numerous times on several servers. Can't seem to get it to work on the current server with GoDaddy.
Back to top
View user's profile Send private message
lostboy



Joined: 02 May 2004
Posts: 5128
Location: toronto, canada

PostPosted: Thu Feb 14, 2008 3:14 am    Post subject: Reply with quote

yes, that would be helpful...one trick is to add debug code at each step of the database interaction to see where the failure occurs
_________________
Lostboy

Cat, the other other white meat

Please read Posting Etiquette before posting

You can always try Google
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
rdub



Joined: 07 Nov 2006
Posts: 93

PostPosted: Thu Feb 14, 2008 3:16 am    Post subject: Reply with quote

<script language="JavaScript">

function next(msg) {
document.location = "bus_businesses.php?select=" + msg + "&Submit=Select";
}

</script>



<?
// Connect database
mysql_connect("$host","$usr","$pwd");
mysql_select_db("ic_network");

$select = "--select--";

// If submitted, check the value of "select". If its not blank value, get the value and put it into $select.
if(isset($select)&&$select!=""){
if(!empty($_GET['select'])){ $select = $_GET['select']; }
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--
.style1 {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 11px;
}

.style2 {color: #0000FF}

.style3 {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 11px;
font-weight: bold;
}
-->
</style>
</head>
<body>
<form ID="form1" Category Listing"form1" class="style1 style2" method="get" >
Business Categories :
<select name="select" class="style1 style2" onChange="next(form1.select.value);">
<option value="">--- Select ---</option>
<?
// Get records from database (table "members_list").
$list=mysql_query("select distinct Category_Listing from members_list order by id asc");

// Show records by while loop.
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<? echo $row_list['Category_Listing']; ?>"
<? if($row_list['ID']==$select){ echo "selected"; } ?>>
<? echo $row_list['Category_Listing']; ?></option>
<?
// End while loop.
}
?>
</select>
</form>
<hr>
<p>
<?
// If you have selected from list box.
if(isset($select)&&$select!="" && $select != "--select--" ){

// Get records from database (table "members_list").
$result=mysql_query("select * from members_list where Category_Listing='$select'");
echo "<h1>".$select."</h1><br>";
while($row=mysql_fetch_assoc($result)) {




?>
</p>
<table width="250" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><span class="style1"><strong>
<?


echo $row['Customer']; ?></strong></span></td>
</tr>

<tr>
<td><span class="style1"><? echo $row['Phone']; ?></span></td>
</tr>
<tr>
<td><span class="style1">
<?
if( $row['Fax'] != "") {
echo "Fax " . $row['Fax'];
}
?>
</span></td>
</tr>
<tr>
<td><span class="style1">
<?
if( $row['Description'] != "" && $row['Email'] != "") {
echo '<a href="mailto:'.$row['Email'].'">'.$row['Email']."</a>";
}
?>
</span></td>
</tr>
<tr>
<td><span class="style1">
<?
if( $row['Web_Link'] != "") {
echo '<a href="'.$row['Web_Link'].'" target="_blank">'.$row['Web_Link']."</a>";
}
?>
</span></td>
</tr>
</table>
<p><br>



<?
// End While statement
}
// End if statement.
}


// Close database connection.
mysql_close();
?>
</p>
<p><br>
<br>
</p>
</body>
</html>
Back to top
View user's profile Send private message
lostboy



Joined: 02 May 2004
Posts: 5128
Location: toronto, canada

PostPosted: Thu Feb 14, 2008 3:41 am    Post subject: Reply with quote

You should not post you actual passwords in the script

Code:

<?
// Connect database
if (!mysql_connect("$host","$usr","$pwd")){
 die("Can't connect to server...". mysql_error());
}

if (!mysql_select_db("ic_network")){
 die("Can't connect to db...". mysql_error()); ;
}

$select = "--select--";

// If submitted, check the value of "select". If its not blank value, get the value and put it into $select.
if(isset($select)&&$select!=""){
if(!empty($_GET['select'])){ $select = $_GET['select']; }
}
?>
...
<?
// Get records from database (table "members_list").
$list=mysql_query("select distinct Category_Listing from members_list order by id asc") or die(mysql_erorr());
..

_________________
Lostboy

Cat, the other other white meat

Please read Posting Etiquette before posting

You can always try Google
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
rdub



Joined: 07 Nov 2006
Posts: 93

PostPosted: Thu Feb 14, 2008 3:50 am    Post subject: Reply with quote

Yes, I know that but didn't pay attention, sorry.

I now get this error

Fatal error: Call to undefined function: mysql_erorr() in /home/content/r/e/i/reisch/html/icn/directory.php on line 31
Back to top
View user's profile Send private message
rdub



Joined: 07 Nov 2006
Posts: 93

PostPosted: Thu Feb 14, 2008 4:09 am    Post subject: Reply with quote

It is not even populating the select menu.
Back to top
View user's profile Send private message
lostboy



Joined: 02 May 2004
Posts: 5128
Location: toronto, canada

PostPosted: Thu Feb 14, 2008 5:01 am    Post subject: Reply with quote

crap, spelled error incorrectly, correct the spelling and you should get a more informative message
_________________
Lostboy

Cat, the other other white meat

Please read Posting Etiquette before posting

You can always try Google
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
rdub



Joined: 07 Nov 2006
Posts: 93

PostPosted: Thu Feb 14, 2008 5:34 am    Post subject: Reply with quote

Getting closer. Thank you.
Back to top
View user's profile Send private message
Display posts from previous:   
WeberTrivia Questions WeberTrivia Questions
 Think you are smart? Prove it!. Try your skills with these questions :
 WeberTrivia QuestionsRecursive arrays and multi-dimensional arrays are one and the same. (PHP and MySQL)
 WeberTrivia QuestionsThe \"cache_dir\" tag of the squid configuration has a default of /var/spool/squid. (Linux)

WeberTrivia Questions



PHP Code Examples
 Stream diffrent sizes of images from a single image to save disk space.
 JavaScript dropdown list menu to switch any page.
 Dump the contents of a PHP variable in html format with a recursive list of subfolders and files from a given root directory.
 PHP Dump in html format the contents of one array variable with a recursive list of the nested array variables inside.
 Link Extractor - This function is used to extract links from a given URL. This will convert relative path into absolute path and also remove PHPSESSID stuff.
 Building a Dynamic Form using Javascript and innerHTML. Add form elements in realtime without refreshing the page.
 A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Post new topic   Reply to topic    WeberForums.com Forum Index -> MySQL General All times are GMT + 2 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum






Powered by phpBB © 2001, 2005 phpBB Group
PHP Forum :: MySQL Forum :: Java Script Forum