<?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)) {
...
}
}
}
}
Related
I try to get data from category using mysql and php.
Sql Structure:
Category
-cat_id
-name
Date
-id
-url
-category
Php code:
<?php
$sql = "select * from category";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo '.$row["url"].';
}
}
?>
when i select the category the data is not listed.
Any idea?
Try this Code . Just removed single quotations
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo $row["url"];
}
}
May this will work:
PHP Code
<?php
$sql = "select * from category";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo '<select id="category" name="category">';
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
echo '</select>';
}
}
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo 'url is: '.$row["url"];
}
}
?>
Try this
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo $row["url"];
}
}
if you want result with in single quotes
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo "'".$row["url"]."'";
}
}
Please avoid mysql_* because the mysql_* functions have been removed in PHP7. Use MySQLi instead.
PHP + Mysql :
<?php
$sql = "select * from category";
$result = mysql_query($sql);
echo "<select name='category'>";
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
echo "</select>";
if(!empty($_POST['category'])) {
$category_id = $_POST['category'];
$sql = "select * from date WHERE category = '".$category_id."'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result))
{
echo '.$row["url"].';
}
}
}
?>
PHP + Mysqli
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select * from category";
$result = $conn->query($sql);
echo "<select name='category'>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
echo "</select>";
if(!empty($_POST['category'])) {
$category_id = $_POST['category'];
$sql = "select * from date WHERE category = '".$category_id."'";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while ($row = $result->fetch_assoc())
{
echo '.$row["url"].';
}
}
}
$conn->close();
?>
Nothing changed.
I'm using this code:
<?php
$sql = "select * from category";
$result = mysql_query($sql);
echo "<select name='category'>";
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
echo "</select>";
if(!empty($_POST['category'])) {
$sql = "select * from date WHERE category = '1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result))
{
echo $row["url"];
}
}
}
?>
If i delete if condition the data is listed but all the time.
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
?>
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.
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..
}
I am using this query below and it only returns the first query in the entry if i use only the if (empty($field1)) to display. If i fill in the print(""); it works but i want to use the if (empty($field1)) snippet to display. How can i do it?
$sql="SELECT field1, field2 FROM table WHERE p_id='$pid'
and k_id='$kid' ORDER BY id DESC";
$result=mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
print("");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$field1 = $row['field1'];
$field2 = $row['field2'];
print("");
}
}
if (empty($field1)) {
echo ""; //Thats right, i don't want anything to show for this portion
} else {
echo "<div id=comments>Comments</div><br>
<div id=entries>$field1 and $field2</div>";
}
What are you trying to do? somthing like this:
$sql="SELECT field1, field2 FROM table WHERE p_id='$pid' and k_id='$kid' ORDER BY id DESC";
$result=mysql_query($sql) or die ("Error: ".mysql_error());
$rows = mysql_num_rows($result);
if ($rows > 0)
echo "here are your entries\n";
while($row = mysql_fetch_array($result))
{
echo $row['field1']." ";
echo $row['field2']."\n";
}
another way
$sql="SELECT field1, field2 FROM table WHERE p_id='$pid' and k_id='$kid' ORDER BY id DESC";
$result=mysql_query($sql) or die ("Error: ".mysql_error());
$rows = mysql_num_rows($result);
if ($rows > 0)
echo "here are your entries\n";
while($row = mysql_fetch_array($result))
{
if (empty($row['field1'])) {
echo " ";
} else {
echo $row['field1']." ";
echo $row['field2']."\n";
}
}
i believe mysql_fetch_array only returns one row
http://www.w3schools.com/PHP/func_mysql_fetch_array.asp
also ur sure that neither p_id and k_id are not unique?
i would also try $sql="SELECT * FROM table WHERE p_id='$pid'
and k_id='$kid' ORDER BY id DESC";
just to see if that yields any different results, you can always parse out just the two fields from the return data
TRY THIS TO START WITH (the $results variable is just confusing things):
$sql="SELECT field1, field2 FROM table WHERE p_id='$pid'
and k_id='$kid' ORDER BY id DESC";
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$rows = mysql_num_rows($query);
if($rows == 0)
{
print("");
}else{
while($row = mysql_fetch_array($query))
{
if ($row['field1'] == "")
{
print("");
}else{
$field1 = $row['field1'];
print($field1)
}
if ($row['field2'] == "")
{
print("");
}else{
$field1 = $row['field2'];
print($field2)
}
}
}