MySQL select where $id - php

I have a website with this code snippet:
<?php
mysql_connect('localhost','root','password');
mysql_select_db('news');
$id_article = $_GET['newsid'];
$query = mysql_query('SELECT * FROM news WHERE id="$id_article"');
{
echo '<div class="item"><h1>'.$query['subject'].'</h1><br />';
echo $query['full_content'].'<br / >';
echo date('D-M-Y', $query['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
?>
The $id_article gets the id from an previous request. The $id_article works, but the $query doesn't. $query['***'] still blank space. I don't know why. Please help me! Thanks a lot!

You don't get your query result. Use like below:
<?php
mysql_connect('localhost','root','password');
mysql_select_db('news');
$id_article = intval($_GET['newsid']);
$query = mysql_query('SELECT * FROM news WHERE id=' . $id_article);
if (mysql_num_rows($query) > 0)
{
$row = mysql_fetch_assoc($query);
echo '<div class="item"><h1>'.$row['subject'].'</h1><br />';
echo $row['full_content'].'<br / >';
echo date('D-M-Y', $row['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
?>
Tip: use PDO or mysqli instead of mysql_ functions! Mysql_ functions are deprecated / not supported in new php versions!
Further, sanatize your input before passing to your query!

Use like this
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['id']. " - ". $row['date'];

You have not used mysql_fetch_assoc. Use as below :
$query = mysql_query('SELECT * FROM news WHERE id=' . $id_article);
$row = mysql_fetch_assoc($query);
if(count($row)>0)
{
echo '<div class="item"><h1>'.$row['subject'].'</h1><br />';
echo $row['full_content'].'<br / >';
echo date('D-M-Y', $row['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}

change your this code
$query = mysql_query('SELECT * FROM news WHERE id="$id_article"');
{
echo '<div class="item"><h1>'.$query['subject'].'</h1><br />';
echo $query['full_content'].'<br / >';
echo date('D-M-Y', $query['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}
to
$sql = "SELECT * FROM news WHERE id = $id_article";
$query = mysql_query($sql);
while($result = mysql_fetch_assoc($query));
{
echo '<div class="item"><h1>'.$result['subject'].'</h1><br />';
echo $result['full_content'].'<br / >';
echo date('D-M-Y', $result['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
}

There are a lot of errors in your code
mysql_query has been depreciated
Use the following code
and ckeck for the errors
<?php
mysql_connect('localhost', 'root', 'password');
$conn = mysql_select_db('news');
$id_article = $_GET['newsid'];
if($query = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM news WHERE id=$id_article"))){
echo '<div class="item"><h1>' . $query['subject'] . '</h1><br />';
echo $query['full_content'] . '<br / >';
echo date('D-M-Y', $query['date']) . '<br / >';
echo 'Posted by ' . $id_article;
echo '</div>';
}
?>

Try This..
$id_article = $_GET['newsid'];
$query = "SELECT * FROM news WHERE id='$id_article'";
$query1=mysql_query($query);

You should call a function to get the row returned by the query. You are trying to access the $query instead of the row. Your code should look like this:
<?php
mysql_connect('localhost','root','password');
mysql_select_db('news');
$id_article = $_GET['newsid'];
$query = mysql_query('SELECT * FROM news WHERE id="$id_article"');
if ($query) {
$row = mysql_fetch_assoc($query));
echo '<div class="item"><h1>'. $row['subject'].'</h1><br />';
echo $row['full_content'].'<br / >';
echo date('D-M-Y', $row['date']).'<br / >';
echo 'Posted by '.$id_article;
echo '</div>';
} else {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
?>
You can also take a look at the mysql_result(), mysql_fetch_array(), mysql_fetch_row() functions.

Related

php pagination with search not working together?

This is the small minor project from college. It ranks the students as per the no of votes. It does rank the student in the pagination but when the student profile is clicked the rank is not shown.
Ok so the below code works but the last part of the code does not display the rank in the output. how can I $num in both codes?
HOME
<br><br>
<form>
<input type="text" name="id" autocomplete="off" placeholder="enter student id">
</form>
<br><br>
<?php include 'conn.php'; ?>
<?php
$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);
$result_per_page = 5;
$no_of_result = mysqli_num_rows($result);
$no_of_page = ceil($no_of_result/$result_per_page);
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}
$page_first = ($page-1)*$result_per_page;
$sql = 'SELECT * FROM table ORDER BY votes DESC LIMIT ' . $page_first . ',' . $result_per_page;
$result = mysqli_query($conn, $sql);
$num = $page * $result_per_page -4;
while($row = mysqli_fetch_assoc($result)) {
echo '<div class="x">';
echo '#';
echo $num++;
echo ' ';
echo '<a href="?id='.$row['id'].'">';
echo $row['name'];
echo '</a>';
echo '</div>';
}
echo '<br>';
for($page=1;$page<=$no_of_page;$page++){
echo ''.$page.'';
echo ' ';
}
echo '<br>';
?>
<?php
$name = $_GET['id'];
$sql = "SELECT * FROM table WHERE id=$name";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<br>';
echo 'RANK = ';
echo '?';//display rank here?
echo '<br>';
echo 'NAME = ';
echo $row['name'];
echo '<br>';
echo 'VOTES = ';
echo $row['votes'];
echo '<br>';
}
} else {
//echo "0 results";
}
?>
The output of the above code.. I want to output the rank in the place of question mark.
current output
i want something like this
desired output

PHP: CMS Problems

Hey guys im writting a CMS but im having problems and need a fresh pair of eyes at this. I am getting this error: Parse error: syntax error, unexpected '}' on the line just above the last else statement but there is no reason for it.
The idea is to have display a substr of the articles and then click them to get the full article. Can someone have a look at my code and tell me where im going wrong or if this will even work.
class MyCMS
{
function get_content($id = "", $excerpt = FALSE)
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM content WHERE blog_id = '$id'";
$return = '<p> Go Back To Content Page</p>';
else:
$sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) !=0):
while($row = mysql_fetch_assoc($res))
{
echo '<div id="roundedbox"><h2>' . $row['title'] . ' </h2>';
echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
if ($excerpt):
echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more </div>";
else:
echo '<p>' . $row['body'] . '</p>';
}
else:
echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>';
echo $return;
endif;
}
}
Then in my main page i am calling the code as follows:
<?php
include 'cms.php';
$obj = new MyCMS();
?>
<?php
if(isset($_GET['id'])):
echo $obj->get_content($_GET['id'], TRUE);
else:
echo $obj->get_content($_GET['id']);
endif;
?
You forgot to add endif(for the if statement inside loop) and } (for closing class). Please check this code.
class MyCMS
{
function get_content($id = "", $excerpt = FALSE)
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM content WHERE blog_id = '$id'";
$return = '<p> Go Back To Content Page</p>';
else:
$sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) !=0):
while($row = mysql_fetch_assoc($res))
{
echo '<div id="roundedbox"><h2>' . $row['title'] . ' </h2>';
echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
if ($excerpt):
echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more </div>";
else:
echo '<p>' . $row['body'] . '</p>';
endif;
}
else:
echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>';
echo $return;
endif;
}
}
Also please notice that second code logic is absolutely wrong..
<?php
include 'cms.php';
$obj = new MyCMS();
?>
<?php
if(isset($_GET['id'])):
echo $obj->get_content($_GET['id'], TRUE);
else:
// echo $obj->get_content($_GET['id']); // This line is executing when $_GET['id'] is undefined.So dont use $_['id'] here.
endif;
?>

Basic PHP SQL Query not working

We have a basic PHP script to extract the title and description for each job from a MySQL database as simply display this information. This is what it looks like:
$sql = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'";
$query = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>');
$results = mysql_fetch_assoc($query);
<?php while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$results['title']}</h2>";
echo "<p>{$results['desc']}</p>";
echo '</div>';
} ?>
Now, this only extracts one row from the database, but it should extract two. So, I tried the following to replace the while statement:
<?php foreach($results as $result) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
} ?>
This statement doesn't work either. This just displays (weirdly) the first character of each column in the first row in the table.
Does anyone have any idea as to why this isn't working as it should?
In your while use same variable $result as you started:
while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
}
and remove the first $results = mysql_fetch_assoc($query);
Result variable you have used is result not results
Replace
$sql = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'";
$query = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>');
**$results = mysql_fetch_assoc($query);** // remove this line
<?php while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$results['title']}</h2>";
echo "<p>{$results['desc']}</p>";
echo '</div>';
} ?>
to
$sql = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'";
$query = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>');
<?php while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
} ?>
You already fetched the first row before your loop started, which is why it only prints the second row. Simply comment out that line:
#$results = mysql_fetch_assoc($query); # here is your first row,
# simply comment this line
<?php while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
} ?>
You are also looping over $result but using $results in your while loop body.
Check this:
$sql = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'";
$query = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>');
<?php
while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
}
?>
Change this line
<?php while($result = mysql_fetch_assoc($query)) {
to
<?php while($results = mysql_fetch_assoc($query)) {

How to just show few lines from a whole blog-post on a certain page?

I am making a blog in which I want to show just the summary of all posts on the home page rather than whole posts.
Below is the function that I use to get the posts on my home page.
function get_content($id = '')
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * from cms_content WHERE id = '$id'";
$return = '<p>Go back to Home page</p>';
echo $return;
else:
$sql = "select * from cms_content ORDER BY id DESC";
endif;
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) != 0):
while ($row = mysql_fetch_assoc($res)) {
echo '<h1>' . $row['title'] . '</h1>';
echo '<p>' . "by: " . $row['author'] . ", posted on: " . $row['date'] . '<p>';
echo '<p>' . $row['body'] . '</p><br />';
}
else:
echo '<p>We are really very sorry, this page does not exist!</p>';
endif;
}
And then simply I use this php code to get the posts:
<?php
if (isset($_GET['id'])) :
$obj->get_content($_GET['id']);
else :
$obj->get_content();
endif;
?>
So how to get the summary only using php function or mysql as I know that we can use mysql query to get limit the words?
function sumarize($your_string, $limit_lines){
$count = 0;
foreach(explode("\n", $your_string) as $line){
$count++;
echo $line."\n";
if ($count == $limit_lines) break;
}
}
sumarize($row['body'], 10);
will show first ten lines
1-lined:
echo '<p>'.implode("\n",array_slice(explode("\n",$row['body']),0,10)).'</p>';
instead of line with $row['body']

How to show records in a different order

I'm starting with this small PHP and mysql script. How would i make it show the tasks by the taskupdated column?
$query = "SELECT * FROM tasks2 where owner=72";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo $row['ownername'];
echo ' - ';
echo $row['tasktitle'];
echo '<br/>';
echo $row['taskdetails'];
echo '<hr/>';
}
<?php
$query = "SELECT * FROM tasks2 WHERE owner=72 ORDER BY taskupdated";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo $row['ownername'];
echo ' - ';
echo $row['tasktitle'];
echo '<br />';
echo $row['taskdetails'];
echo '<hr />';
}
?>
If you want to sort them the other way, use ORDER BY taskupdated DESC instead.
SELECT * FROM tasks2 where owner=72 ORDER BY taskupdated

Categories