| View previous topic :: View next topic |
| Author |
Message |
rdub
Joined: 07 Nov 2006 Posts: 93
|
Posted: Thu Feb 14, 2008 12:55 am Post subject: Accessing database |
|
|
| Looking for a simple script just work out the database connections and query/write from the table. |
|
| Back to top |
|
 |
lostboy

Joined: 02 May 2004 Posts: 5128 Location: toronto, canada
|
Posted: Thu Feb 14, 2008 2:11 am Post subject: |
|
|
| 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 |
|
 |
rdub
Joined: 07 Nov 2006 Posts: 93
|
Posted: Thu Feb 14, 2008 3:12 am Post subject: |
|
|
| 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 |
|
 |
lostboy

Joined: 02 May 2004 Posts: 5128 Location: toronto, canada
|
Posted: Thu Feb 14, 2008 3:14 am Post subject: |
|
|
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 |
|
 |
rdub
Joined: 07 Nov 2006 Posts: 93
|
Posted: Thu Feb 14, 2008 3:16 am Post subject: |
|
|
<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 |
|
 |
lostboy

Joined: 02 May 2004 Posts: 5128 Location: toronto, canada
|
Posted: Thu Feb 14, 2008 3:41 am Post subject: |
|
|
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 |
|
 |
rdub
Joined: 07 Nov 2006 Posts: 93
|
Posted: Thu Feb 14, 2008 3:50 am Post subject: |
|
|
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 |
|
 |
rdub
Joined: 07 Nov 2006 Posts: 93
|
Posted: Thu Feb 14, 2008 4:09 am Post subject: |
|
|
| It is not even populating the select menu. |
|
| Back to top |
|
 |
lostboy

Joined: 02 May 2004 Posts: 5128 Location: toronto, canada
|
Posted: Thu Feb 14, 2008 5:01 am Post subject: |
|
|
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 |
|
 |
rdub
Joined: 07 Nov 2006 Posts: 93
|
Posted: Thu Feb 14, 2008 5:34 am Post subject: |
|
|
| Getting closer. Thank you. |
|
| Back to top |
|
 |
|