Jump to content

display problem


divinedesigns1
 Share

Recommended Posts

heya, me again, im having a problem with displaying my news after i post it,

<?phpinclude('MyConnect.php');$query = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y) as sd FROM news";$result = mysqli_query($con, $query);if($result){while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){  $url = 'comments.php?id=' . $row['id'];  echo '<p><br>' . $row['title'] . '</b></br>' .  $row['sd'] . '<br /> Posted by: <br>' . $row['author'] . '</b><br />' . $row['post'] . '<br /> <a href="javascript:openComments(\''.$url.'\')">Add New Comment or View posted comments</a></p>';}}else{echo 'There are no news posts to display';}?>

i kept looking over it but i dont see anything wrong with iti been studying this code and reviewing it for a few mins well, i notice i had spelled mysqli_fetch_array() wrong, so i corrected that but that wasnt it, then i notice i had $row['$post'] but so i corrected that also but that wasnt it either. am i missing something? i had also change the fetch function from mysqli_fetch_array() to mysql_fetch_array() but that still wasnt it, now im thinking its the $query that is giving the problem but how is it giving a problem im not too sure, but on the other hand, it's displaying the echo 'There are no news posts to display'; so something is working but something isnt working, ahhhh ill go take a walk

Link to comment
Share on other sites

if it is executing the else part you can use mysqli_error() to see why the query is failing and also if query get succeded but there is no result to fetch then you will see nothing. you can use mysql_num_rows() to determine the number of resulsts.

Edited by birbal
Link to comment
Share on other sites

if it is executing the else part you can use mysqli_error() to see why the query is failing and also if query get succeded but there is no result to fetch then you will see nothing. you can use mysql_num_rows() to determine the number of resulsts.
ok thanks birbal
Link to comment
Share on other sites

can we see the update code? and what is showing when you run it?

Link to comment
Share on other sites

I redid the mysqli_error() function and i got this error now

Warning: mysqli_query() [function.mysqli-query]: Empty query in ..../news/news.php on line 21There are no news posts to display
but i think im doing this all wrong
function openComments(url){comments = window.open(url, "Comment", "menubar=0, resizable=0, width=380, height=480");comments.focus();}</script></head><body><?phpinclude('MyConnect.php');if(!$con -> query("SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news")){printf("Errormessage: %s\n", $mysqli->error);}$result = mysqli_query($con, $query);if($result){while($row = mysql_fetch_array($result, MYSQLI_ASSOC)){  $url = 'comments.php?id=' . $row['id'];  echo '<p><br>' . $row['title'] . '</b></br>' .  $row['sd'] . '<br /> Posted by: <br>' . $row['author'] . '</b><br />' . $row['post'] . '<br /> <a href="javascript:openComments(\''.$url.'\')">Add New Comment or View posted comments</a></p>';}}else{echo 'There are no news posts to display';}?>

Link to comment
Share on other sites

so i went and mess up with the codes for a little, and i did the following with the code, remove the "i" from MYSQLI_ASSOCchange $result = mysql_query($query) and i got this error Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/14/8709614/html/news/news.php on line 18Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/14/8709614/html/news/news.php on line 18There are no news posts to displaynotice the "there are no news posts to display" still tormenting me -_- ill get to the bottom of this...........stupid echo lol

Link to comment
Share on other sites

If you're changing to mysql, be sure to also change mysqli_connect() to mysql_connect()
thank foxy, ill try that now, and sorry for the late reply was sleeping
Link to comment
Share on other sites

the error is removed but there's no news displaying still sigh

Link to comment
Share on other sites

if you are looking for !$con as in false/no connection, would it not preventquery("SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news") from being triggered for a true connection?would you not check for no connection and just show error for that none connection, else run the query.that why its failing, its not getting reference to $query SQL statement for $result = mysqli_query($con, $query);

Link to comment
Share on other sites

if you are looking for !$con as in false/no connection, would it not preventquery("SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news") from being triggered for a true connection? would you not check for no connection and just show error for that none connection, else run the query. that why its failing, its not getting reference to $query SQL statement for $result = mysqli_query($con, $query);
yeah, i did that but for some reason it still isnt showingthis is what i did
include('MyConnect.php');$query = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news";$result = mysqli_query($con, $query);

im thinking about doing this

$dbhost = "";$dbuser = "";$dbpass = "";$dbname = "";$con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die('can not connect to database');$query = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news";$result = mysqli_query($con, $query);

i think this would work better

Link to comment
Share on other sites

correction dsone, that worked, what i did was, remove the MyConnect.php and placed it into the news.php script itself then i remove the if statement for the connection to the database, which enable me to displayed whatever is in the database

Link to comment
Share on other sites

now im having problems with the edit page 0.o i selected a news to be edited but it doesnt display it and the same method i use for the news.php page isnt working either blah

<?php$dbhost = ''; // These have a value i just remove it so i can display it on the forum$dbuser = '';$dbpass = '';$dbname = '';$con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);if((isset($_GET['id'])) && (is_numeric($_POST['id']))){$id = $_GET['id'];}elseif ((isset($_POST['id'])) && (is_numeric($_POST['id']))){$id = $_POST['id'];}else{echo 'Please choose a news post to edit';exit();}if(isset($_POST['submitted'])){$error = array();if(empty($_POST['title'])){  $error[] = 'You forgot to enter a title. ';}else{  $title = $_POST['title'];}if(empty($_POST['name'])){  $error[] = 'You forgot to enter an author.';}else{  $name = $_POST['name'];}if(empty($_POST['message'])){  $error[] = 'You forgot to enter a message';}else{  $message = $_POST['message'];}if(empty($errors)){  $query = "UPDATE news SET title='$title', author='$name', post='$message' WHERE id=$id";  $result = mysqli_query($con, $query);  if($result){   echo "News post has been updated!";  }else{   echo 'News post could not be updated.';  }}else{  echo 'news post could not be updated for the following resions -<br />';  foreach($errors as $msg){   echo "- $msg<br />\n";  }}}else{$query = "SELECT title, author, post, id FROM news WHERE id=$id";$result = mysqli_query($con, $query);$num = mysqli_num_rows($result);$row = mysqli_fetch_array($result, MYSQLI_NUM);$title = $row['0'];$name = $row['1'];$message = $row['2'];if($num == 1){  echo '<h3>Edit news post</h3>  <form action"?id=edit_news$num='.$id.'" method="post">  <p>News Title: <input type="text" name="title" size="25" maxlength="255" value="'.$title.'" /></p>  <p>Name: <input type="text" name="name" size="15" maxlength="255" value="'.$name.'" /></p>  <p>Message:<br /><textarea row="5" cols="40" name="message">'.$message.'</textarea></p>  <p><input type="submit" name="submit" vaule="submit" /></p>  <input type="hidden" name="id" value="'.$id.'" />';}else{  echo 'news post could not be edited, please try again.';}}?>

Link to comment
Share on other sites

im suppose to have a query here right

$dbhost = ''; // These have a value i just remove it so i can display it on the forum$dbuser = '';$dbpass = '';$dbname = '';$con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); <-- after this im suppose to have a query right?if((isset($_GET['id'])) && (is_numeric($_POST['id']))){$id = $_GET['id'];}elseif ((isset($_POST['id'])) && (is_numeric($_POST['id']))){$id = $_POST['id'];}else{echo 'Please choose a news post to edit';exit();}

Link to comment
Share on other sites

Check your if statements, you're confusing $_GET and $_POST. It's also better to use mysqli than mysql, mysqli supports more features. You should also keep your database information in an include file, there is no difference in having that in an include file versus the script. Including a file is just like copying and pasting the code from the included file into the parent file. When you have your database connection information in every file you're just setting yourself up for more headaches later.

Link to comment
Share on other sites

Think you will get beter results with
$con = mysqli_connect($dbhost, $dbuser, $dbpass); mysqli_select_db($con,$dbname); $query = "SQL CODE"; $result = mysqli_query($con, $query);

ok ill try that dsone
Check your if statements, you're confusing $_GET and $_POST. It's also better to use mysqli than mysql, mysqli supports more features. You should also keep your database information in an include file, there is no difference in having that in an include file versus the script. Including a file is just like copying and pasting the code from the included file into the parent file. When you have your database connection information in every file you're just setting yourself up for more headaches later.
actually its in an include file, i took it out because it wasnt reading the include file for some reason, thats how i got the news.php file to work, not sure why but yeah thats how i got ti fix, but thanks for the advice tho
Link to comment
Share on other sites

i took it out because it wasnt reading the include file for some reason, thats how i got the news.php file to work, not sure why but yeah thats how i got ti fix, but thanks for the advice tho
Are you interested in fixing that, or do you just want to do it the way that makes it harder to update? If it can't find the file then you're not using the correct path, if it finds the file then it will import the code and use it.
Link to comment
Share on other sites

Are you interested in fixing that, or do you just want to do it the way that makes it harder to update? If it can't find the file then you're not using the correct path, if it finds the file then it will import the code and use it.
.........the path is correct, since i have it in an include file on the admin, etc which work fine
Link to comment
Share on other sites

Then what's the problem? If the path is correct then it would work like any other file. What code is in the include file?
the database information, the variable $con and function mysqli_connect()
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...