Jump to content

Only Pull data when new data is in mysql


j5uh

Recommended Posts

I'm sorta new to the php game but I have this code below that I'm using to bring in data that's sitting in a mysql db. But I want it to only put the data that's "unique" and not duplicates. How would I go about doing this with what I have?

 

 

<?php
$host = "xx";
$user = "xx";
$pass = "xx";
$database = "xx";

mysql_connect($host,$user,$pass);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM xxx";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Latest Signal Subscription</center></b><br><br>";

$i=0;
while ($i < $num) {

$month=mysql_result($result,$i,"month");
$day=mysql_result($result,$i,"day");
$year=mysql_result($result,$i,"year");
$symbol=mysql_result($result,$i,"symbol");
$type=mysql_result($result,$i,"type");
$direction=mysql_result($result,$i,"direction");
$entryprice=mysql_result($result,$i,"entryprice");

echo "

<table border=1 width=100%>
  <tr>
    <td><strong>Month</strong></td>
    <td><strong>Day</strong></td>
    <td><strong>Year</strong></td>
    <td><strong>Symbol</strong></td>
    <td><strong>Type of Signal</strong></td>
    <td><strong>Direction</strong></td>
    <td><strong>Entry Price</strong></td>
  </tr>
  <tr>
    <td>$month</td>
    <td>$day</td>
    <td>$year</td>
    <td>$symbol</td>
    <td>$type</td>
    <td>$direction</td>
    <td>$entryprice</td>
  </tr>
</table>
<br>
";

$i++;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/98364-only-pull-data-when-new-data-is-in-mysql/
Share on other sites

first, this:

$num=mysql_numrows($result);

should be:

$num=mysql_num_rows($result);

 

Also, don't close the connection with mysql_close(); until you are done with all your mysql_* functions.

 

If you don't want the table header printed over and over, move it outside the loop. The only thing that should be inside the loop is:

  <tr>
    <td>$month</td>
    <td>$day</td>
    <td>$year</td>
    <td>$symbol</td>
    <td>$type</td>
    <td>$direction</td>
    <td>$entryprice</td>
  </tr>

 

Lastly, what do you mean by

only put the data that's "unique" and not duplicates

Hey, thanks for the response!

 

so what I have so far is:

 

<?php
$host = "xx";
$user = "xx";
$pass = "xx";
$database = "xx";

mysql_connect($host,$user,$pass);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM xxx";
$result=mysql_query($query);

$num=mysql_num_rows($result);

echo "<b><center>Latest Signal Subscription</center></b><br><br>";

$i=0;
while ($i < $num) {

$month=mysql_result($result,$i,"month");
$day=mysql_result($result,$i,"day");
$year=mysql_result($result,$i,"year");
$symbol=mysql_result($result,$i,"symbol");
$type=mysql_result($result,$i,"type");
$direction=mysql_result($result,$i,"direction");
$entryprice=mysql_result($result,$i,"entryprice");

mysql_close();

echo "

<table border=1 width=100%>
  <tr>
    <td><strong>Month</strong></td>
    <td><strong>Day</strong></td>
    <td><strong>Year</strong></td>
    <td><strong>Symbol</strong></td>
    <td><strong>Type of Signal</strong></td>
    <td><strong>Direction</strong></td>
    <td><strong>Entry Price</strong></td>
  </tr>
  <tr>
    <td>$month</td>
    <td>$day</td>
    <td>$year</td>
    <td>$symbol</td>
    <td>$type</td>
    <td>$direction</td>
    <td>$entryprice</td>
  </tr>
</table>
<br>
";
$i++;
}
?>

 

How do I move the table headers out side the loop? I'm still a noobie sorry for the dumb question.

 

What I mean by Unique data is, how do I make it so that it only pulls the latest data available or maybe even past week or something.

This is the style I use:

 

<?php
$host = "xx";
$user = "xx";
$pass = "xx";
$database = "xx";

mysql_connect($host,$user,$pass)
  or die("Unable to connect");
@mysql_select_db($database)
  or die("Unable to select database");
$query = "SELECT * FROM xxx";
$result = mysql_query($query)
  or die("Query Error: ".mysql_error());

echo "<b><center>Latest Signal Subscription</center></b><br><br>";
if(!mysql_num_rows($result)){
  echo "No rows found";
}else{
?>
<table border=1 width=100%>
  <tr>
    <td><strong>Month</strong></td>
    <td><strong>Day</strong></td>
    <td><strong>Year</strong></td>
    <td><strong>Symbol</strong></td>
    <td><strong>Type of Signal</strong></td>
    <td><strong>Direction</strong></td>
    <td><strong>Entry Price</strong></td>
  </tr>
<?php
  while($row = mysql_fetch_assoc($result)){
    echo "
  <tr>
    <td>$row['month']</td>
    <td>$row['day']</td>
    <td>$row['year']</td>
    <td>$row['symbol']</td>
    <td>$row['type']</td>
    <td>$row['direction']</td>
    <td>$row['entryprice']</td>
  </tr>
</table>
<br>
";
  }
}
mysql_close();
?>

 

Couple questions. Is there a reason you have month, day, and year as separate columns? You can have them all in one field. Make that column of type DATETIME. Then, you can do lot's with that. I'm not a MySQL guru though, so when you decide what you want to do with your query, I would trying a post over in that area of the forum.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.