Jump to content

[SOLVED] Something wrong =./


Cory94bailly

Recommended Posts

<?php
mysql_connect("***", "***", "***") or die(mysql_error());
mysql_select_db("***") or die(mysql_error());

$query1 = "SELECT * FROM stickynews"; 
$query2 = "SELECT * FROM news";

$result1 = mysql_query($query1) or die(mysql_error());
$result2 = mysql_query($query2) or die(mysql_error());

while($row1 = mysql_fetch_array($result1))
printNews($row1);

while($row2 = mysql_fetch_array($result2))
printNews($row2);

function printNews($news)
{
if ( mysql_num_rows($news) < 1 )
{
echo "<h1>No news!</h1>";
}
else
{
echo '
<table><tr>
<td><h2><u>' . $news['title'] . '</u></h2></td>
</tr><tr>
<td>' . $news['news'] . '</td>
</tr></table><br />';

}
}
?>

 

It's just not showing anything, anyone care to explain why?

Link to comment
Share on other sites

  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

Place the function bit before you use it

 

Same thing.. blank.

 

<?php
mysql_connect("***", "***", "***") or die(mysql_error());
mysql_select_db("***") or die(mysql_error());

$query1 = "SELECT * FROM stickynews"; 
$query2 = "SELECT * FROM news";

$result1 = mysql_query($query1) or die(mysql_error());
$result2 = mysql_query($query2) or die(mysql_error());

while($row1 = mysql_fetch_array($result1))
printNews($row1);

while($row2 = mysql_fetch_array($result2))
printNews($row2);

function printNews($news)
{
echo '
<table><tr>
<td><h2><u>' . $news['title'] . '</u></h2></td>
</tr><tr>
<td>' . $news['news'] . '</td>
</tr></table><br />';


if ( mysql_num_rows($news) < 1 )
{
echo "<h1>No news!</h1>";
}
}
?>

Link to comment
Share on other sites

<?php

function printNews($news)
{
if ( mysql_num_rows($news) < 1 )
{
echo "<h1>No news!</h1>";
}
else
{



echo '
<table><tr>



<td><h2><u>' . $news['title'] . '</u></h2></td>
</tr><tr>



<td>' . $news['news'] . '</td>
</tr></table><br />';




}
}

mysql_connect("***", "***", "***") or die(mysql_error());
mysql_select_db("***") or die(mysql_error());

$query1 = "SELECT * FROM stickynews"; 
$query2 = "SELECT * FROM news";




$result1 = mysql_query($query1) or die(mysql_error());
$result2 = mysql_query($query2) or die(mysql_error());



while($row1 = mysql_fetch_array($result1))



printNews($row1);

while($row2 = mysql_fetch_array($result2))



printNews($row2);

?>

 

Like this?

Link to comment
Share on other sites

<?php

function printNews($news)
{
if ( mysql_num_rows($news) < 1 )
{
echo "<h1>No news!</h1>";
}
else
{



echo '
<table><tr>



<td><h2><u>' . $news['title'] . '</u></h2></td>
</tr><tr>



<td>' . $news['news'] . '</td>
</tr></table><br />';




}
}

mysql_connect("***", "***", "***") or die(mysql_error());
mysql_select_db("***") or die(mysql_error());

$query1 = "SELECT * FROM stickynews"; 
$query2 = "SELECT * FROM news";




$result1 = mysql_query($query1) or die(mysql_error());
$result2 = mysql_query($query2) or die(mysql_error());



while($row1 = mysql_fetch_array($result1))



printNews($row1);

while($row2 = mysql_fetch_array($result2))



printNews($row2);

?>

 

Like this?

 

 

Same thing.. blank.

 

:(

Link to comment
Share on other sites

At the top, put:

 

error_reporting(E_ALL);

ini_set('display_errors', 'E_ALL');

 

and see if it says anything.

 

If nothing shows up, then it's most likely a fatal error.

 

If that's the case, try setting display_errors in php.ini, look for a compile error, or put echo statements throughout the code to see where it stops going.

Link to comment
Share on other sites

If that's the case, try setting display_errors in php.ini, look for a compile error, or put echo statements throughout the code to see where it stops going.

 

I put a few echos and it went all the way past the "No news" part... Yet, it didn't show "No news!!!"........

Link to comment
Share on other sites

Hmm..

 

When I add news it displays the news, it says no news but it also gives me this:

 

Notice: Undefined variable: result1 in /home/content/m/a/r/markbailly/html/fcs/news.php on line 72

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/m/a/r/markbailly/html/fcs/news.php on line 72

 

Line 72:

 

if ( mysql_num_rows($result1) < 1 )

Link to comment
Share on other sites

At the top, put:

 

error_reporting(E_ALL);

ini_set('display_errors', 'E_ALL');

 

ini_set('display_errors', 'E_ALL'); should be ini_set('display_errors', 'On');

 

 

Ahhhh right.  I can't ever remember config directives ;p.  Should've just known that one though, since it's simply do you want to show errors or not....

 

 

 

Anyway.....

 

What you're passing to printNews() is already a row, so that num_rows check is kinda... unneeded, and might cause problems.

 

That error means that result1 is not defined.  Possibly it's out of the variable scope or been unset.  Can we see more code around it?

 

 

Link to comment
Share on other sites

Try:

<?php

mysql_connect("***", "***", "***") or die(mysql_error());
mysql_select_db("***") or die(mysql_error());

$query1 = "SELECT * FROM stickynews";
$query2 = "SELECT * FROM news";

$result1 = mysql_query($query1) or die(mysql_error());
$result2 = mysql_query($query2) or die(mysql_error());

function printNews($result)
{
    if ( mysql_num_rows($result) < 1 )
    {
        echo "<h1>No news!</h1>";
    }
    else
    {
        echo "<table>\n";

        while($news = mysql_fetch_assoc($result))
        {
            echo <<<HTML
  <tr>
    <td><h2><u>{$news['title']}</u></h2></td>
  </tr>
  <tr>
    <td>{$news['news']}</td>
  </tr>
HTML;
        }

        echo "\n</table><br />";
    }
}

printNews($result1);
printNews($result2);

?>

However a better suggestion would be to merge your news and stickynews tables into one seeing as though they are both basically the same.

Link to comment
Share on other sites

Try:

<?php

mysql_connect("***", "***", "***") or die(mysql_error());
mysql_select_db("***") or die(mysql_error());

$query1 = "SELECT * FROM stickynews";
$query2 = "SELECT * FROM news";

$result1 = mysql_query($query1) or die(mysql_error());
$result2 = mysql_query($query2) or die(mysql_error());

function printNews($result)
{
    if ( mysql_num_rows($result) < 1 )
    {
        echo "<h1>No news!</h1>";
    }
    else
    {
        echo "<table>\n";

        while($news = mysql_fetch_assoc($result))
        {
            echo <<<HTML
  <tr>
    <td><h2><u>{$news['title']}</u></h2></td>
  </tr>
  <tr>
    <td>{$news['news']}</td>
  </tr>
HTML;
        }

        echo "\n</table><br />";
    }
}

printNews($result1);
printNews($result2);

?>

However a better suggestion would be to merge your news and stickynews tables into one seeing as though they are both basically the same.

 

I'll try this whenever my sister get off my computer ;)

 

But that would ruin the whole point of "Stickied" news..

 

I want it so like if we are having a special for something, it will show it at the top always and if we ever post "less important" news, it will go under it..

 

 

Also, I wonder if there's a way to get it to show "Sticky:" in front of it without making it too complicated...

Link to comment
Share on other sites

But that would ruin the whole point of "Stickied" news..

 

I want it so like if we are having a special for something, it will show it at the top always and if we ever post "less important" news, it will go under it..

No it wont. All you need to do is just add a new field in your news table call this sticky. Set this new field to an ENUM type with possible values of Y and N

sticky ENUM('y','n') NULL DEFAULT 'n'

What ever news feeds you want to be stickied just set the sticky field to y. Then to retrieve stickied news from your news table just run the following query:

SELECT * FROM news WHERE sticky='y'

For non-stickied news run:

SELECT * FROM news WHERE sticky='n'

It seem pointless having two tables for news.

 

Also, I wonder if there's a way to get it to show "Sticky:" in front of it without making it too complicated...

Change:

function printNews($result)
{
    if ( mysql_num_rows($result) < 1 )
    {
        echo "<h1>No news!</h1>";
    }
    else
    {
        echo "<table>\n";

        while($news = mysql_fetch_assoc($result))
        {
            echo <<<HTML
  <tr>
    <td><h2><u>{$news['title']}</u></h2></td>
  </tr>
  <tr>
    <td>{$news['news']}</td>
  </tr>
HTML;
        }

        echo "\n</table><br />";
    }
}

printNews($result1);

to:

function printNews($result, $sn=false)
{
    if ( mysql_num_rows($result) < 1 )
    {
        echo "<h1>No news!</h1>";
    }
    else
    {
        $sticky = ($sn) ? 'STICKY: ' : null;
        echo "<table>\n";

        while($news = mysql_fetch_assoc($result))
        {
            echo <<<HTML
  <tr>
    <th>{$sticky}<u>{$news['title']}</u></th>
  </tr>
  <tr>
    <td>{$news['news']}</td>
  </tr>
HTML;
        }

        echo "\n</table><br />";
    }
}

printNews($result1, true); // sticky news

 

 

Link to comment
Share on other sites

No it wont. All you need to do is just add a new field in your news table call this sticky. Set this new field to an ENUM type with possible values of Y and N

sticky ENUM('y','n') NULL DEFAULT 'n'

 

How do I do that from phpmyadmin?

 

What SQL query do I run?

Link to comment
Share on other sites

Login to phpmyadmin, select your database then your news table. Make sure the Structure tab is selected.

 

Find where it says Add [1] field(s) (note [1] is a text box). At the end of that line select Go.

In the Field box type sticky

Choose ENUM from the Type menu.

Enter 'y','n' in the Length/Values field

Select null from the Null menu

Enter n in the Default field

Select Save button.

 

You should have a new column called sticky in your news table. When you browse to the Browse tab you should see all current news stored in your news table will have a sticky column with the letter n present in that column.

 

To skicky a news entry select a row and edit it, change the sticky field to the letter y

 

Now use the following code for displaying your news:

<?php

mysql_connect("***", "***", "***") or die(mysql_error());
mysql_select_db("***") or die(mysql_error());

$query1 = "SELECT * FROM news ORDER BY id, sticky ASC";

$result1 = mysql_query($query1) or die(mysql_error());

function printNews($result)
{
    if ( mysql_num_rows($result) < 1 )
    {
        echo "<h1>No news!</h1>";
    }
    else
    {
        echo "<table>\n";

        while($news = mysql_fetch_assoc($result))
        {
            $sticky = ($news['sticky'] == 'y') ? 'STICKY: ' : null;
            echo <<<HTML
  <tr>
    <th>{$sticky}<u>{$news['title']}</u></th>
  </tr>
  <tr>
    <td>{$news['news']}</td>
  </tr>
HTML;
        }

        echo "\n</table><br />";
    }
}

printNews($result1); // none sticky news

?>

EDIT: I posted the old code by mistake. New code above ^^

Link to comment
Share on other sites

Did that and I get this:

 

Unknown column 'id' in 'order clause'

 

Also, what do I do in my admin part to work with this?

 

 

 

Admin part:

 

<?php

require('config.php');
   // Make a MySQL Connection
   mysql_connect("$path", "$username", "$password") or die(mysql_error());
   mysql_select_db("$database") or die(mysql_error()); 

if(isset($_POST['postnews'])) { 
   
   $title = mysql_real_escape_string($_POST['title']);
   $news = mysql_real_escape_string($_POST['news']);
   
   // Insert the news
   $query = mysql_query("INSERT INTO news (title, news) VALUES ('$title', '$news')")or die(mysql_error());   
   
   echo "<meta http-equiv='refresh' content='1'> News posted!";

}
elseif(isset($_POST['poststicky']))
{
   $title = mysql_real_escape_string($_POST['title']);
   $news = mysql_real_escape_string($_POST['news']);
   
   // Insert the news
   $query = mysql_query("INSERT INTO stickynews (title, news) VALUES ('$title', '$news')")or die(mysql_error());   
   
   echo "<meta http-equiv='refresh' content='1'> <b>Stickied</b> news posted!</a>";
      
}
elseif(isset($_POST['delete']))
{
   // Delete it all!
   mysql_query("TRUNCATE TABLE news")or die(mysql_error());
   
   echo "<meta http-equiv='refresh' content='1'> All news deleted!";
      
}
elseif(isset($_POST['deletestickies']))
{
   // Delete it all!
   mysql_query("TRUNCATE TABLE stickynews")or die(mysql_error());
   
   echo "<meta http-equiv='refresh' content='1'> All <b>stickied</b> news deleted!";
}   
else
{
?>

<title>Add/Delete Recent News</title>
<a href="../">Click Here to go home.</a>

   <form action="<? $_SERVER['PHP_SELF'] ?>" method="post">
      Title:<br>
      <input type="text" name="title"></input><br><br>
      Text:<br>
      <textarea rows="10" cols="50" name="news"></textarea>
      <br><br>
      <input type="submit" name="postnews" value="Post regular News"> 
     <input type="submit" name="poststicky" value="Post sticky news">
   </form>  
   </form>

<br><br>

<h4>Options:</h4>

<form action="<? $_SERVER['PHP_SELF'] ?>" method="post">
<input type="submit" name="delete" value="Delete all regular news">
<input type="submit" name="deletestickies" value="Delete all stickied news">
</form> 

<u>Current features:</u>
<br>
<i>Quick Post News
<br>
Delete all regular news with one click
<br>
Delete all stickied news with one click
<br>
Option to title your news
<br>
"Sticky" News
<br>
Neatly displays all posted news in order from sticky to regular</i>
<br><br><br>
<u>Coming soon:</u>
<br>
<i>Ability to edit news
<br>
Ability to make stickied news regular and vise versa</i>

<?php
}
?>

Link to comment
Share on other sites

Did that and I get this:

 

Unknown column 'id' in 'order clause'

Do you have a field in your news table which holds an auto_increment key? Substitute id with that field in the following query:

$query1 = "SELECT * FROM news ORDER BY id, sticky ASC";

 

 

Also, what do I do in my admin part to work with this?

 

Admin part:

Recoded admin part:

NOTE it is untested.

<?php

require 'config.php';

// Make a MySQL Connection
mysql_connect("$path", "$username", "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());

if(isset($_POST['postnews']))
{
   $title  = mysql_real_escape_string($_POST['title']);
   $news   = mysql_real_escape_string($_POST['news']);

   // check thate status of the sticky radio button
   $sticky = (isset($_POST['sticky']) && $_POST['sticky'] == 'y') ? 'y' : 'n';

   // Insert the news
   $query = mysql_query("INSERT INTO news (title, news, sticky) VALUES ('$title', '$news', '$sticky')") or die(mysql_error());

   echo "<meta http-equiv='refresh' content='1'> News posted!";

}
elseif(isset($_POST['delete']))
{
    // delete all non-sticky news
    if(isset($_POST['delete']['r']))
    {
        $qry = 'DELETE FROM news WHERE sticky=\'n\'';
        $msg = 'All <b>non-stickied</b> news deleted!';
    }
    // delete all stickied news
    if(isset($_POST['delete']['s']))
    {
        $qry = 'DELETE FROM news WHERE sticky=\'y\'';
        $msg = 'All <b>stickied</b> news deleted!';
    }
    // delete all news
    else
    {
        $qry = 'TRUNCATE TABLE news';
        $msg = 'All news deleted!';
    }
   // Delete it all!
   mysql_query($qry)or die(mysql_error());

   echo "<meta http-equiv='refresh' content='1'> $msg";

}
else
{
?>

<title>Add/Delete Recent News</title>
<a href="../">Click Here to go home.</a>

   <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
      <p>Title:<br>
      <input type="text" name="title" /></p>
      <p>Text:<br>
      <textarea rows="10" cols="50" name="news"></textarea></p>
      <p>Sticky: <input type="radio" name="sticky" value="y" /> YES | <input type="radio" name="sticky" value="n" /> NO</p>
      <input type="submit" name="postnews" value="Post News">
   </form>
   </form>

<br><br>

<h4>Options:</h4>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="submit" name="delete" value="Delete All News">
<input type="submit" name="delete[r]" value="Delete Non-Sticky News">
<input type="submit" name="delete[s]" value="Delete Sticky News">
</form>

<u>Current features:</u>
<br>
<i>Quick Post News
<br>
Delete all regular news with one click
<br>
Delete all stickied news with one click
<br>
Option to title your news
<br>
"Sticky" News
<br>
Neatly displays all posted news in order from sticky to regular</i>
<br><br><br>
<u>Coming soon:</u>
<br>
<i>Ability to edit news
<br>
Ability to make stickied news regular and vise versa</i>

<?php
}
?>

Link to comment
Share on other sites

Ok so 3 things I found wrong so far.

 

1.) If non-stickied, theres spaces in front of the title.

 

2.) For the "Delete all news" button, I get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM news WHERE sticky='y'' at line 1

 

3.) For the "Delete sticky news" button, I get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM news WHERE sticky='y'' at line 1

Link to comment
Share on other sites

1.) If non-stickied, theres spaces in front of the title.

Change the following line in the printNews function:

$sticky = ($news['sticky'] == 'y') ? 'STICKY: ' : null;

to:

$sticky = ($news['sticky'] == 'y') ? 'STICKY: ' : '';

I don't know why you're getting a space for non-sticky news. As both lines above are the same.

 

2.) For the "Delete all news" button, I get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM news WHERE sticky='y'' at line 1

 

3.) For the "Delete sticky news" button, I get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM news WHERE sticky='y'' at line 1

Argh. Recopy the code from my last post. Had a few SQL errors which I corrected. You must of copied the code before I updated it.

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.