Jump to content

[SOLVED] PHP MySQL script help


nonexistentera

Recommended Posts

Hello.

 

I have a current script that displays the contents of my MySQL table.

Because of the length of the article I am wondering how I can have the out put display only a certain number of letters before it quits, as to not overload the page.

Basically just a preview.

What I have is

 

$sql = " SELECT * FROM `table` WHERE `date` < NOW() ORDER BY `table`.`date` DESC LIMIT 0 , 4";

$query = mysql_query($sql);

while ($row = mysql_fetch_assoc($query)) {

echo "<li><a href=\"httpPage\"><span>".$row["name"]."</span><em>".$row["article"]."</em></a></li>";

}

 

I just can't think of what I could use.

All Help is very much appreciated.

Link to comment
Share on other sites

are you storing the whole article in the DB or just the path and the filename? It should be just the path and filename, then you can get the path & filename info from DB to retrieve from the server.

 

$path = $row['path'] . row['filename'];

$file = fopen($path, "r", TRUE);

$lines = //enter the number of lines you want from the file

$count = 0;

if($file){

while($count < $lines){

$buffer = fgets($file, 4096);

echo $buffer;

count++;

}

fclose($file);

}

Link to comment
Share on other sites

Get the articles out of your DB-that is a huge resource drainer and one of the worst errors as far as web developemt goes. Put in the path to the acrticle and the filename in the DB, but the actual file should be stored on the server. Make this change now and you will not regret it.

Link to comment
Share on other sites

Alright I have every thing out of the DB. They are all stored in files with the names rand(0,5000);.".php".

 

Now I am having some other issues. I am trying to put all of this in a function

 

$user = "root_user";
$pass = "pass";
$host = "localhost";
$db = "db";
$cxn = mysql_connect($host, $user, $pass) or die;
mysql_select_db($db, $cxn);
$sql = " SELECT * FROM `add` WHERE `date` < NOW() ORDER BY `add`.`date` DESC LIMIT 0 , 20	";
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
$path = $row['sentencePath'];

$file = fopen($path, "r", TRUE);
$lines = 3;   /*enter the number of lines you want from the file */
$count = 1;
if($file){
while($count < $lines){
$buffer = fgets($file, 50);
echo "<b>".$buffer."<b/>";
$count++;
}
fclose($file);
}

And when i do I get no output. But it works fine on its own.

I am doing this because of how my script is setup, the output seems to be the complete file path and all the contents.

Here is the code that gives me the output /system/file/path/rand.phpArticleContent

				<?PHP


			//Query from table////////////////////////////////////////

			$sql = " SELECT * FROM `add` WHERE `date` < NOW() ORDER BY `add`.`date` DESC LIMIT 0 , 20	";
			$query = mysql_query($sql);
			//While array gives new results in past 10 minutes////////

			echo "<ul class=\"block\">";	


			while ($row = mysql_fetch_assoc($query)) {
				echo "<li><span>".$row["name"]."";
				echo " in ".$row["city"]." wrote</span>";
				echo "<em>\"".
	$path = $row['sentencePath'];

	$file = fopen($path, "r", TRUE);
	$lines = 3;   /*enter the number of lines you want from the file */
	$count = 1;
	if($file){
		while($count < $lines){
		$buffer = fgets($file, 50);
		echo "<b>".$buffer."<b/>";
		$count++;
	}
	fclose($file);
}
"\"</em>";
				echo "</li>";

			}
			echo "</ul>";

?>

I'm not sure what to fix, or if I show go a different way with outputing the data.

Link to comment
Share on other sites

Alright Here is the main page code

<div class="main" id="two-columns">

<div class="col2">

	<div class="left">

		<div class="content">

			<h1>Recent Post</h1>
			<?PHP


			//Query from table////////////////////////////////////////

			$sql = " SELECT * FROM `add` WHERE `date` < NOW() ORDER BY `add`.`date` DESC LIMIT 0 , 20	";
			$query = mysql_query($sql);
			//While array gives new results in past 10 minutes////////

			echo "<ul class=\"block\">";	


			while ($row = mysql_fetch_assoc($query)) {
				echo "<li><span>".$row["name"]."";
				echo " in ".$row["city"]." wrote</span>";
				echo "<em>\"".
	$path = $row['sentencePath'];

	$file = fopen($path, "r", TRUE);
	$lines = 3;   /*enter the number of lines you want from the file */
	$count = 1;
	if($file){
		while($count < $lines){
		$buffer = fgets($file, 50);
		echo "<b>".$buffer."<b/>";
		$count++;
	}
	fclose($file);
}
"\"</em>";
				echo "</li>";

			}
			echo "</ul>";

?>

</table>


The form

<div class="contentForm" id="contentForm" style="position: relative; float: left;">
<form action="postContent.php?ampersand=2010" method="POST">
<table>
<tr>
	<td>
		<input type="hidden" name="random" value="<?= $captchas->random () ?>" />
	</td>
</tr>
<tr>
	<td>
		<input name="name" size="20" value="Name" onfocus="if( this.value==this.defaultValue ) this.value='';" onblur="if( this.value=='' ) this.value=this.defaultValue;" type="text"><BR>
	</td>
</tr>
<tr>
	<td>		
		<input name="city" size="20" value="City, State, Zip" onfocus="if( this.value==this.defaultValue ) this.value='';" onblur="if( this.value=='' ) this.value=this.defaultValue;" type="text"><BR>
	</td>
</tr>			
<tr>
	<td>
		<input name="mood" size="20" value="Current Mood" onfocus="if( this.value==this.defaultValue ) this.value='';" onblur="if( this.value=='' ) this.value=this.defaultValue;" type="text"><BR>
	</td>
</tr>
<tr>
	<td>
		<textarea rows="5" cols="20" name="sentence"  onfocus="if( this.value==this.defaultValue ) this.value='';" onblur="if( this.value=='' ) this.value=this.defaultValue;">Write something. Thats the point of this :]</textarea><br />
      			<?= $captchas->image () ?>
         		<br> <a href="<?= $captchas->audio_url () ?>">Phonetic spelling (mp3)</a>
	</td>
</tr>
<tr>
       	<td>
       	Enter What You See:  <input name="password" size="6" />
        	</td>
</tr>
<tr>
	<td>
		<input value="Submit" type="submit">
</table>
</form>
</div>

The script that inserts the data

<?php



// See query.php for documentation




  // Check the random string to be valid and return an error message
  // otherwise.
  if (!$captchas->validate ($random_string))
  {
    echo 'Every CAPTCHA can only be used once. The current CAPTCHA has already been used. Try again.';
  }
  // Check, that the right CAPTCHA password has been entered and
  // return an error message otherwise.
  elseif (!$captchas->verify ($password))
  {
    echo 'You entered the wrong password. Aren\'t you human? Please use back button and reload.';
  }
  // Return a success message
  else
  {
include('./fnc_filter.php');
include('db.php');
$amp = $_GET["ampersand"];
$name = $_POST["name"];
$city = $_POST["city"];
$mood = $_POST["mood"];
$words = $_POST["sentence"];
if(!isset($amp)){
exit("Form not submitted");
}
else
{
}

	$not_allowed = array("******"); // List of bad words
	$nameFilter = filter_words($_POST['name'], $not_allowed, $strict = $strict);
	$cityFilter = filter_words($_POST['city'], $not_allowed, $strict = $strict);
	$moodFilter = filter_words($_POST['mood'], $not_allowed, $strict = $strict);
	$wordsFilter = filter_words($_POST['sentence'], $not_allowed, $strict = $strict);
	$randNum = rand(0,50000);
	$file = $_SERVER['DOCUMENT_ROOT']."/articles/".$randNum.".php";
		$fp = fopen($file, 'w');
		fwrite($fp, $wordsFilter);
		fclose($fp);
	mysql_query("INSERT INTO `tylerper_cookbook2`.`add` (
			`id` ,
			`name` ,
			`city` ,
			`mood` ,
			`date` ,
			`sentenceFile` ,
			`sentencePath`
			)
			VALUES ( NULL , '$nameFilter', '$cityFilter', '$moodFilter', CURRENT_TIMESTAMP , '$randNum.php', '$file')");
	echo "Thank you :] <br />";
	echo "Please return to our <a href=\"http://add.tylerperroux.com/\">home page</a> to view your post";




}

?>

And the MySQL Table Layout

--
-- Table structure for table `add`
--

CREATE TABLE IF NOT EXISTS `add` (
  `id` bigint( NOT NULL auto_increment,
  `name` varchar(64) NOT NULL,
  `city` varchar(64) NOT NULL,
  `date` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `articleFile` varchar(20) NOT NULL,
  `articlePath` longtext NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;

--
-- Dumping data for table `add`
--

INSERT INTO `add` (`id`, `name`, `city`, `mood`, `date`, `articleFile`, `articlePath`) VALUES
(1, 'Name', 'City', '2008-06-11 09:38:14', '49488.php', '/my/path/to/articles/49488.php'),

My Function looks like

 

<?PHP

function myFunction(){
$user = "root";
$pass = "pass";
$host = "localhost";
$db = "tablename";
$cxn = mysql_connect($host, $user, $pass) or die;
mysql_select_db($db, $cxn);
$sql = " SELECT * FROM `add` WHERE `date` < NOW() ORDER BY `add`.`date` DESC LIMIT 0 , 20	";
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
$path = $row['sentencePath'];

$file = fopen($path, "r", TRUE);
$lines = 3;   /*enter the number of lines you want from the file */
$count = 1;
if($file){
while($count < $lines){
$buffer = fgets($file, 50);
echo "<b>".$buffer."<b/>";
$count++;
}
fclose($file);
}
}
echo $myFunction;
?>

And there is no output, just a blank Screen.

If I remove the code from the function, it works fine.

And there appears to be no errors either in the page or in error_log.

Im Thinking I messed up somewhere.

Link to comment
Share on other sites

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

Thank you so much. I kept going over what you said and my code wondering what you ment.

Thank you. I would have been stuck on this forever.

I guess this was going to happen someday. I never really spent much time learning how to make functions.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.