Query count and function not working properly - php

I am using the code below to select items and using the $result variable to do a count. if there are less than 1 it will say add more and if there are more than 5 it will say view all. It works for less than 1 but it won't for more than 5. Am i doing it right?
//Query
$sql = "SELECT id, name, why, date_time
FROM tabs
WHERE p_id = '$pid'
ORDER BY id
LIMIT 0, 5";
$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)) {
$name = $row['name'];
$w = nl2br($row['why']);
$y = $row['date_time'];
print("echoing contents here");
}
}
if(mysql_num_rows($result) > 5) {
echo "view all";
}
if(mysql_num_rows($result) < 1) {
echo "add one";
} ?>

if(mysql_num_rows($result) > 5) {
echo "view all";
}
if(mysql_num_rows($result) < 1) {
echo "add one";
}
Should be
if($rows > 5) {
echo "view all";
}
if($rows < 1) {
echo "add one";
}
Since you exhausted the result set with the previous mysql_fetch_array() loop

Related

Pass value from while loop in session

I have a small problem/question...
I have made a while loop that's echo a href link. How can I give the $_Session the correct value for the selected ncrnummer?
This is my code:
$query1 = "SELECT id FROM `ncr_input` WHERE projectnummer = '".$_POST['Projectnummer']."'";
$result1 = mysqli_query($conn, $query1);
if ($result->num_rows > 0) {
if ($result1->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result1)) {
$ncrnummer = $row['id'];
if ($row['id']) {
$_SESSION['ncrnummer'] = $row['id'];
echo "{$ncrnummer}";
echo "<br>";
}
}
}
}

PHP + MySQL taking a 3rd variable

I have a C# code and a PHP that gets info I send from C#.
So I need to have such code:
<?php
$link = mysqli_connect('localhost','root','');
$database = mysqli_select_db($link,'serial');
$serial = $_GET['serial'];
$hwid = $_GET['hwidin'];
$sql = "SELECT * FROM serial WHERE serial = '". mysqli_real_escape_string($link,$serial) ."'" ;
$result = $link->query($sql);
/*
0 = Wrong HWID
1 = HWID is correct
2 = HWID left empty
3 = No serial with that key
*/
if(strlen($hwid) < 1)
{
echo "2";
}
else
{
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if (strlen($row['hwid']) > 1)
{
if ($hwid != $row['hwid'])
{
echo "0";
}
else
{
echo "1";
}
}
else
{
$sql = "UPDATE serial SET hwid='$hwid' WHERE serial='$serial'";
echo "1";
if(mysqli_query($link, $sql))
{
echo $row['hwid'];
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
}
}
else
{
echo "3";
}
}
So what I want to do is add some checking, for example:
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if (strlen($row['hwid']) > 1)
{
if ($hwid != $row['hwid'])
{
echo "0";
}
else
{
echo "1";
}
If echo "1" {
$sql = "SELECT time FROM serial Where serial = '". mysqli_real_escape_string($link,$serial) ."'" ;
$result = $link->query($sql);
if $result > 1 {
echo "55";
}
I need somehow to combine it with hwid checker and serial checker.
I want to check "time" column variable and use > < 1 to decide what to echo.

Get data from category with php mysql

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.

Why does my query result repeat 3 times?

the code i have below basically behaves as a friend search engine where you can add someone if you don't have them on you account. So i am trying to do the following: If exists it should just display the friend and if not then it should say add friend.
I got it to work but the tail end of the query (please see this comment "//Problem code - when i use this echo below this where it repeats 3 times") is giving me trouble where it repeats Add 3 times.
<?php
$num_rows1 = mysql_num_rows($result);
if ($result == "") {
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if ($rows == 0) {
print("<div id=norequests>No results for <strong>$q
</strong></div>");
}
elseif ($rows > 0) {
while ($row = mysql_fetch_array($query))
{
$person = htmlspecialchars($row['full_name']);
$linksys = htmlspecialchars($row['name']);
$pid = htmlspecialchars($row['system_id']);
}
print("");
}
}
else{
echo '<div id="error">No results.</div>';
}
$sql = "SELECT `Friend_id` from `friends_container`
WHERE `System_id` = '$sid'";
$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))
{
$existing = htmlspecialchars($row['Friend_id']);
if ($existing == $pid) {
echo("<img src=$linksys />$person - Already Existing");
}
else
//Problem code - when i use this echo below this where it repeats 3 times
{
echo("Add $person");
}
}
?>
You must have 3 images stored for each account.
Your query will always result in a multiple result. what you should do is use php to convert it to something which will result in a better option or say convert the result in an array for convenience.

How to return all rows if using if (empty($variable))?

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)
}
}
}

Categories