Php print single id issue - php

So here's what I want.. When user clicks on some link e.g. http://www.keevik.com/vicevi.php?id=24 that script prints out only that single id.
Here's my code
/* Get data. */
$sql = "SELECT * FROM $tbl_name ORDER BY id DESC LIMIT $start, $limit";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<a href ='vicevi.php?id=".$row['id']."'>".$row['id']."</a>";
echo "<br>";
echo nl2br($row["VicText"]);
echo "<hr>";
}
So, when I click on some link it doesn't do what I actually want :S

$sql = "SELECT * FROM $tbl_name ORDER BY id DESC LIMIT $start, $limit";
if(isset($_GET['id']))
{
$id=intval($_GET['id']);
$sql = "SELECT * FROM $tbl_name WHERE id=$id";
}
$result = mysql_query($sql);
if(!$result)
{
echo 'no data found!';
}
else
{
// etc..
}

Related

php code reuse. Is there a better way to do it?

I have upgrade my code. In the old code I had 2 functions: display_maker_success() and display_maker_fail() but I realised I can combine those two functions into one display_maker_stat() by putting more arguments into the function. I like it very much!
Is better way to do this? I want more code reuse.
function display_maker_success($link, $userid){
$status="closed";
$result="completed";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 6;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If ($isempty ==0) {
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>Completed</td></tr>";
};
echo "</table>";
};
};
function display_maker_fail ($link, $userid) {
$status="closed";
$result="fail";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If($isempty ==0){
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>fail</td></tr>";
};
echo "</table>";
};
};
function display_maker_stat ($link, $userid, $reuslt, $limit) {
$status="closed";
$result="fail";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If($isempty ==0){
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$result</td></tr>";
};
echo "</table>";
};
};
Try the below,
Also there were few errors in your code and i have corrected them.
function display_maker_stat($link, $userid, $reuslt = 'fail', $limit)
{
$status = "closed";
$html = '';
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$query = mysql_query($sql, $link);
if (mysql_num_rows($query) != 0) {
$html .= "<table border=1>";
$html .= "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($query, MYSQL_NUM)) {
$html.= "<tr><td>$row[0]</td><td>$row[1]</td><td>$result</td></tr>";
}
$html.= "</table>";
echo $html;
}
else {
echo "No Record";
}
}
Read about OOP

PHP - While loop not showing table records

In below code it shows table row value outside while loop but not shows inside while loop. While loop not working; Please let me know whats wrong in it?
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id;";
$res = q($sql) or die(mysql_error());
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res));
{
echo $row["title"];
echo "hi";
} // End While
} // End If
?>
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id";
^^^^
$res = mysql_query($sql) or die(mysql_error());
^^^^^^^^^^
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res));
{
echo $row["title"];
echo "hi";
} // End While
} // End If
?>
Try this:
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id";
$res = mysql_query($sql) or die(mysql_error());
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res))
{
echo $row["title"];
echo "hi";
} // End While
} // End If
?>

Php image error in displaying

When i run this query images are displaying from php correctly.
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("dawat");
$submit=$_GET['str'] ;
$sql = mysql_query("SELECT * FROM searchengine WHERE pagecontent LIKE '%$_GET%' ");
while($row=mysql_fetch_array($sql)) {
echo "<img src=image.php?pagecontent=".$row['pagecontent']." />";
}
?>
And code of image.php:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$conn = mysql_connect("localhost","root","");
if(!$conn)
{
echo mysql_error();
}
$db = mysql_select_db("dawat",$conn);
if(!$db)
{
echo mysql_error();
}
$pagecontent = $_GET['pagecontent'];
$q = "SELECT pageurl FROM searchengine where pagecontent='$pagecontent'";
$sql = mysql_query("$q",$conn);
if($sql)
{
$row = mysql_fetch_array($sql);
echo $row['pageurl'];
}
else
{
echo mysql_error();
}
?>
And when i run same code of above just few changes in search script:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("dawat");
$submit=$_GET['str'] ;
$sql = mysql_query("SELECT pageurl, BIT_COUNT(pagecontent^'$submit')
FROM searchengine WHERE pagecontent < 20 ORDER BY pagecontent ASC;
") or die(mysql_error());
while($row=mysql_fetch_array($sql)) {
echo "<img src=image.php?pagecontent=".$row['pagecontent']." />";
}
?>
It is showing errors that undefined index:pagecontent and also not showing images.Please help and thanks in advance.
In your query
$sql = mysql_query("SELECT pageurl, BIT_COUNT(pagecontent^'$submit')
FROM searchengine WHERE pagecontent < 20 ORDER BY pagecontent ASC;
") or die(mysql_error());
You are not fetching the column pagecontent and is trying to access it in
echo "<img src=image.php?pagecontent=".$row['pagecontent']." />";
^
So change your query to,
$sql = mysql_query("SELECT pageurl,pagecontent, BIT_COUNT(pagecontent^'$submit')
FROM searchengine WHERE pagecontent < 20 ORDER BY pagecontent ASC;
") or die(mysql_error());
Try it and get print value from $sql.. you will get idea from how to return it..
$sql = mysql_query("SELECT pageurl,pagecontent, BIT_COUNT(pagecontent^'$submit')
FROM searchengine WHERE pagecontent < 20 ORDER BY pagecontent ASC;
") or die(mysql_error());

How to display user avatar next to post? (php,mysql)

So I have two tables in my database one is called Users and the other is News
I made it that users can add News Posts to the site, but I couldn't display user's image next to his post
this is my code right now
<?php
$News = "";
$user_id = "";
$sqlCommand = "SELECT * FROM News ORDER BY id DESC LIMIT 10";
$sqlCommand3 = "SELECT * FROM Users";
$query = mysql_query($sqlCommand) or die(mysql_error());
$query3 = mysql_query($sqlCommand3) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$News .= "";
while(($row = mysql_fetch_array($query)) && ($row2 = mysql_fetch_array($query3)) ){
$News .= "<div class=\"news-post\"> <img src=\".$row2['author_avatar']."\"><p>".$row['author']."</p> <h2>".$row['title']."</h2></div> ";
} // close while
} else {
$News = "No News!";
}
?>
I want where is says $row2['author_avatar'] to echo the image from the users table
You missed out a speech mark (") just before $row2['author_avatar'].
<?php
$News = "";
$user_id = "";
$sqlCommand = "SELECT * FROM News ORDER BY id DESC LIMIT 10";
$sqlCommand3 = "SELECT * FROM Users";
$query = mysql_query($sqlCommand) or die(mysql_error());
$query3 = mysql_query($sqlCommand3) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$News .= "";
while(($row = mysql_fetch_array($query)) && ($row2 = mysql_fetch_array($query3)) ){
$News .= "<div class=\"news-post\"> <img src="\".$row2['author_avatar']."\"><p>".$row['author']."</p> <h2>".$row['title']."</h2></div> ";
} // close while
} else {
$News = "No News!";
}
?>
You should look up INNER JOIN rather than having two SQL queries as mentioned by andrewsi. Here's a good tutorial on how to use it https://www.youtube.com/watch?v=6BfofgkrI3Y.

Loop for Arrays in php

<?php
session_start();
$link = mysqli_connect("localhost", "xxx", "xxxxxx", "xxx");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$id = $_GET['id'];
if ($result = mysqli_query($link, "SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result))
{
$idS = $row['SWho'];
echo $idS;
if ($result = mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result))
{
echo "<img src='Member_ProfilePics/";
echo $row['PP'] . '.' . $row['Ext'];
echo "' width=42 height=40 style='float: left'>";
}
}
}
//<img src='Member_ProfilePics/john.jpg' width=42 height=40 style='float: left'>
}
?>
here in my code, i want to output the list of STo. and connect to other database and output the following rows in each STo.
Sample to be output:
(picture of id=1) 1
(picture of id=2) 2
(picture of id=3) 3
(picture of id=4) 4
(picture of id=5) 5
(picture of id=6) 6
How?
my code is not looping.
Sample output of my code:
(picture of id=1) 1
You are using the same variables for both the loops:
if ($result = mysqli_query($link, "SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result)) {
....
if ($result = mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result)) {
...
}
}
}
}
Try to change the seconda value to these
if ($result = mysqli_query($link, "SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result)) {
....
if ($result2 = mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6")) {
while($row2 = mysqli_fetch_array($result2)) {
...
}
}
}
}

Categories