I have this code:
<?php
include 'config.php';
date_default_timezone_set('America/Los_Angeles');
$d = date('Y-m-d');
$m = date("m");
$day = date("d");
$t = date("His");
$ip = $_SERVER['REMOTE_ADDR'];
$c = file_get_contents('http://api.wipmania.com/'.$ip);
echo "<h2>ALL RESULTS TODAY:</h2><table>";
$_GET['c'] = $c;
$sc = $_GET['sc'];
if($c === "key"){
if($sc === "t"){
$result = "SELECT * FROM main WHERE date = '$d' ORDER BY time";
while($row = mysqli_fetch_array($result))
{echo "<tr><td>".$row['key'] . "</td><td> " . $row['country']."</td><td>".$row['ip']."</td></tr>"; }
}
}
echo '</table>';
?>
i have tried without $con: mysqli_fetch_array($result), but it was the same...
But nothings appear...
no error no results...
Please help... Thanks!
You have not connected to the database or queried your results:
$conn = mysqli_connect($hostname,$username,$password,$dbname) or die(mysqli_error());
//...
$your_query = "SELECT * FROM main WHERE date = '$d' ORDER BY time";
$result = mysqli_query($conn, $your_query);
while ($row = mysqli_fetch_array($result)){
//...
}
you forgot mysqli_query.
replace this
$result = "SELECT * FROM main WHERE date = '$d' ORDER BY time";
by
$result =mysqli_query("SELECT * FROM main WHERE date = '$d' ORDER BY time");
You forgot to perform the query.
$result = mysqli_query($con, "SELECT * FROM main WHERE date = '$d' ORDER BY time");
Related
I am trying to generate a either 0 or 1 and when it is generated update a row with the value. 1 being heads and 0 being tails, like a coin flip. I can't use external libraries for this.
The problem is when it generates a 0 it won't update the row but with a 1 it does, and I've no idea as to why.
<?php
include_once('../library/user.php');
include_once('../config/connect.php');
$sql = "SELECT * FROM coinroulette ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
$rows = $result->fetch_array(MYSQLI_ASSOC);
$id = $rows['id'];
$user = $_SESSION["user"]->getUsername();
$beginGame = new DateTime($rows['time']);
$currentTime = new DateTime(date("Y-m-d H:i:s"));
$diff = $currentTime->diff($beginGame);
if ($currentTime < $beginGame) {
echo "Remaining:" . $diff->format('%S') . "<br>";
}
$beginGame->sub(new DateInterval('PT15S'));
//echo $beginGame->format('Y-m-d H:i:s') . "<br>" . $currentTime->format('Y-m-d H:i:s') . "<br>" . $rows['time'] . "<br>";
//echo rand(0,1);
//Check is game is in place. (result == empty) game in place.
//Otherwise its over and a new game is made.
if (empty($rows['result'])) {
echo "Accepting bets.";
if ($currentTime >= $beginGame) {
//Needs to be converted to an INT or else it cant be inserted into datbase. Took me 3 hours to figure this error out. :(
$gamble = mt_rand(0,1);
$sql = "UPDATE coinroulette SET result = '$gamble' WHERE id = '$id'";
$result = $conn->query($sql);
}
} else {
echo "No longer accepting bets.<br>Result: ";
if ($rows['result'] == "1") {
echo "Heads<br>";
} else {
echo "Tails<br>";
}
$sql = "SELECT * FROM bets WHERE gameID = '$id' AND user = '$user'";
$result = $conn->query($sql);
$betRows = $result->fetch_array(MYSQLI_ASSOC);
if ($result->num_rows > 0) {
echo "InGame<br>";
if ($rows['result'] == $betRows['guess']) {
if(empty($betRows['result'])){
$sql = "UPDATE bets SET result = 1 WHERE user = '$user'";
$result = $conn->query($sql);
$winAmount = $betRows['amount'] * 2;
$sql = "UPDATE users SET points = points + '$winAmount' WHERE username = '$user'";
$result = $conn->query($sql);
}
echo "Winner";
} else {
$sql = "UPDATE bets SET result = 0 WHERE user = '$user'";
$result = $conn->query($sql);
echo "Loser";
}
} else {
echo "Not<br>";
}
}
?>
Solved it myself, you cant update a null field. So I set it as -1.
I run my script ( see below ) but the place where industry & keywordrw vars are set it just doesn't do anything
The other queries where just the industry is set works fine
the other 2 that involve doing multiple LIKE queries on the keyword variable don't do anything
<?php
if (!isset($industry)) {
$industry = '';
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $keywordrw;
} elseif (!isset($keyword)) {
$industry = $_GET['industry'];
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE category LIKE '$industry' LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $industry;
} elseif (isset($keyword) && ($industry)) {
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') AND (category = '$industry') LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $keywordrw . '-' . $industry;
} else {
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
//echo $keywordrw.'-'.$industry;
}
?>
Changed a few things and updated working code is below
if(!isset($industry))
{
$industry = '';
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE title LIKE '%keywordrw%'",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} elseif(!isset($keyword)) {
$industry = $_GET['industry'];
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE category LIKE '$industry' LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} elseif(isset($keyword) &&($industry)){
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') AND (category = '$industry') LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} else {
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
//echo $keywordrw.'-'.$industry;
}
I am trying to create a tracker code, which is when a user enter my page, I will record and add the visitor view by 1
The problem with my code is on every monday, it not only reset the week stats, it also reset the month stats, I paste my increment code and reset code in php below.
Thanks for helping me, I try to figure it out for a few days but can't spot where I code wrongly.
This is my ads.php which I run it by do
<img src="ads.php" width="1" height="1">
at every page
This is my ads.php code
include 'conn.php';
$postID = $_GET['pid'];
$todayDate = date("m-d-y");
//Query with postDate
$sql = "SELECT * FROM wp_tracker WHERE postID LIKE '" . $postID . "'";
$result = mysql_query($sql);
$exist = 0;
while ($row = mysql_fetch_array($result)) {
$postDate = $row['postDate'];
$dayView = $row['dayView'];
$weekView = $row['weekView'];
$monthlyView = $row['monthlyView'];
$exist++;
}
if ($exist == 0) {
//create new record
$sql = "INSERT INTO wp_tracker (postID, postDate) VALUES ('$postID ','$todayDate')";
if (!mysql_query($sql, $con)) {
die('Error: ' . mysql_error());
}
}
if ($exist > 0) {
//if record exist
//check if it same day
if ($postDate == $todayDate) {
//if same day - just increment views
$dayView = $dayView + 1;
$weekView = $weekView + 1;
$monthlyView = $monthlyView + 1;
$sql2 = "UPDATE wp_tracker SET dayView = '$dayView',weekView = '$weekView',monthlyView = '$monthlyView' WHERE postID='$postID '";
$result2 = mysql_query($sql2);
}
}//end if exist
Below is my reset code (reset.php)
$todayDate = date("m-d-y");
//Query with postDate
$sql = "SELECT postDate FROM wp_tracker order by postDate ASC";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$postDate = $row[0];
$timestamp = time();
if ($postDate != $todayDate) {
//a new day -- update dayView to 0
$dayView = 1;
$sql3 = "UPDATE wp_tracker SET postDate= '" . $todayDate . "', dayView = '$dayView'";
$result3 = mysql_query($sql3);
}
if (date('D', $timestamp) === 'Mon') {
$weekView = 1;
$sql3 = "UPDATE wp_tracker SET weekView = '$weekView'";
$result3 = mysql_query($sql3);
}
if (date('d', $timestamp) === '01') {
$monthlyView = 1;
$sql3 = "UPDATE wp_tracker SET monthlyView= '$monthlyView'";
$result3 = mysql_query($sql3);
}
I've started something I don't know how to finish. I've built a table called 'blog'. In the blog table there is a column, date. I use the date() function to retrieve this column:
"1360225336".
I want Archives by year followed by month:
-2013
9.September
.
.
.
3.March
2.February
1.January
-2012
12.December
.
.
.
3.March
2.February
1.January
<?php
$query_o = mysql_query("SELECT * FROM `blog`")or die(mysql_error());
while($row_o = mysql_fetch_assoc($query_o)){
$new_date_y = date("Y", $row_o[date]);
$new_date_m = date("n", $row_o[date]);
$query_h = mysql_query("SELECT * FROM `blog` where date = $new_date_y ")or die(mysql_error());
while($row_h = mysql_fetch_assoc($query_h)){
$new_date_m1 = date("n", $row_o[date]);
echo'<ul>$new_date_y</ul>';
echo'<li>$new_date_m1</il>';
}
}
?>
Try this
$result = mysql_query("select distinct YEAR(date) as year from blog ") or die(mysql_error());
$years = array();
while($row = mysql_fetch_assoc($result))
$years[] = $row['year'];
$one_year_posts = array();
foreach($years as $year){
echo $year."<br/>";
//echo "select * from blog where YEAR(date) = '$year'";
$result1 = mysql_query("select * from blog where YEAR(date) = '$year'") or die(mysql_error());
while($row1 = mysql_fetch_assoc($result1))
$one_year_posts[] = $row1;
foreach($one_year_posts as $post){
//echo $post['date']."<br>";
$months.= date('m F',strtotime($post['date'])).", ";
}
echo $months = rtrim($months," ,");
}
Try this query:
SELECT * FROM `blog` GROUP BY YEAR(`Date`), MONTH(`Date`);
If you also need day, just add , DAY(`Date`) at the end.
$result = mysql_query("select distinct FROM_UNIXTIME(date,'%Y') as year from blog ") or die(mysql_error());
$years = array();
while($row = mysql_fetch_assoc($result))
$years[] = $row['year'];
$one_year_posts = array();
foreach($years as $year){
echo "<br/>-".$year."<br/>";
$result = mysql_query("select * from blog where FROM_UNIXTIME(date,'%Y') = '$year'") or die(mysql_error());
while($row = mysql_fetch_assoc($result))
$one_year_posts[] = $row;
foreach($one_year_posts as $post){
echo strftime('%d %B',$post['date']).", ";
}
}
Note: Use mysqli_ instead of mysql_ functions. The latter is deprecated and no longer maintained.
after trying a lot of new thing finally i succeed
<?php
$result = mysql_query("select distinct FROM_UNIXTIME(date,'%Y') as year from blog ") or die(mysql_error());
$years = array();
while($row = mysql_fetch_assoc($result))
$years[] = $row['year'];
$one_year_posts = array();
foreach($years as $year){
$result1 = mysql_query("select distinct FROM_UNIXTIME(date,'%M') as m from blog where FROM_UNIXTIME(date,'%Y') = '$year'") or die(mysql_error());
$ms = array();
while($row1 = mysql_fetch_assoc($result1))
$ms[] = $row1['m'];
$one_year_posts = array();
foreach($ms as $m){
$date= $year;
$date.= " ";
$date.= $m;
echo <<<PRINT
<li><a href='search.php?archie=$date'>$date</a></li>
PRINT;
}
}
?>
I'm encountering the following problem:
I'd like to compare today's date against some dates in a database, then if it isn't expired yet, show something... but if all the dates in the table are expired, show something like 'No lecture scheduled at this time, return again'.
As for the first thing it's no problem, but I can't show the text where there aren't any future dates...
Here's the code,
Table:
id, dateposted, date_course, title, body
$sql = "SELECT *
FROM L
ORDER BY L.dateposted DESC;";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$exp_date = $row['date_course'];
$todays_date = date("Y-m-d");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
if ($expiration_date >= $today)
{
echo "<a href='courses.php'>" . $row['title']. "</a>";
echo "</br>";
}
}
I'll assume you're using MySQL. A couple of small changes to your query and code should make this work. You should definitely do this kind of filtering in the query and not the code.
$sql = "SELECT *
FROM L
WHERE date_course < NOW() AND dateposted < NOW()
ORDER BY L.dateposted DESC;";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo "<a href='courses.php'>" . $row['title']. "</a>";
echo "</br>";
}
}
else
{
echo "No results available";
}
Several ways to do it. One would be to make the date comparison part of the query. If no rows are selected, show your special message.
Otherwise, you can set a flag like
$has_courses = false;
while ($row = fetch() {
$has_courses = true;
...
}
if (!$has_courses) { echo 'No courses'; }
Although you could do this far more efficiently by improving your query, here is the specific fix you request:
$sql = "SELECT *
FROM L
ORDER BY L.dateposted DESC;";
$result = mysql_query($sql) or die(mysql_error());
$out = false;
while($row = mysql_fetch_assoc($result))
{
$exp_date = $row['date_course'];
$todays_date = date("Y-m-d");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
if ($expiration_date >= $today)
{
$out = true;
echo "<a href='courses.php'>" . $row['title']. "</a>";
echo "</br>";
}
}
if (!$out)
echo 'Nothing found.';