It is currently Tue Feb 07, 2012 8:10 am

All times are UTC + 2 hours [ DST ]




Post new topic Reply to topic  [ 13 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Accessing database
PostPosted: Thu Feb 14, 2008 1:55 am 
Offline

Joined: Tue Nov 07, 2006 7:28 am
Posts: 106
Looking for a simple script just work out the database connections and query/write from the table.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 3:11 am 
Offline

Joined: Sun May 02, 2004 11:34 pm
Posts: 6498
Location: toronto, canada
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 4:12 am 
Offline

Joined: Tue Nov 07, 2006 7:28 am
Posts: 106
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 4:14 am 
Offline

Joined: Sun May 02, 2004 11:34 pm
Posts: 6498
Location: toronto, canada
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 4:16 am 
Offline

Joined: Tue Nov 07, 2006 7:28 am
Posts: 106
<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 rel="nofollow" href="mailto:'.$row['Email'].'">'.$row['Email']."</a>";
}
?>
</span></td>
</tr>
<tr>
<td><span class="style1">
<?
if( $row['Web_Link'] != "") {
echo '<a rel="nofollow" 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>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 4:41 am 
Offline

Joined: Sun May 02, 2004 11:34 pm
Posts: 6498
Location: toronto, canada
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 4:50 am 
Offline

Joined: Tue Nov 07, 2006 7:28 am
Posts: 106
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 5:09 am 
Offline

Joined: Tue Nov 07, 2006 7:28 am
Posts: 106
It is not even populating the select menu.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 6:01 am 
Offline

Joined: Sun May 02, 2004 11:34 pm
Posts: 6498
Location: toronto, canada
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 6:34 am 
Offline

Joined: Tue Nov 07, 2006 7:28 am
Posts: 106
Getting closer. Thank you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ]  Go to page 1, 2  Next

All times are UTC + 2 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


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

Search for:
Jump to:  
cron