Accidental infinite while loop in PHP [duplicate] - php

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
Hi I have created a while loop but cannot understand why it loops forever. Could somebody please explain why?
while ($i<3){
if ($i=1){
$x='psu';
}else if ($i=2){
$x='cases';
}
$sqlcpu = "SELECT * FROM $x WHERE name LIKE '%{$term}%'";
$query = mysqli_query($db, $sqlcpu);
while ($row = mysqli_fetch_array($query)){
?><br /> Name: <?php echo $row['name'];?>
<?php
echo ' Price: £'.$row['price'];
}
$i++;
}
There are other problems with this code but they are not my main concern right now as this loop seems like it should be simple. The variables $x and $i never change after $i=1

if ($i=1) You're assinging 1 to $i here, instead of comparing 1 and $i, use
if ($i==1)
instead.
Instead of the for/if-elseif construct you could also use
foreach( array('psu', 'cases') as $table ) {
$sqlcpu = "SELECT * FROM $table WHERE name LIKE '%{$term}%'";

Related

Issues using between, min and max

I have created a catalog with a block of years search function for characters and the years I've assigned to them. So anything from 1940-1949 would be in the 1940's block of time, and so on. I'm using a href to group these timeframes.
<?php
$sql = "SELECT * FROM catalog";
$displayby = $_GET['displayby'];
$displayvalue = $_GET['displayvalue'];
if($displayby && $displayvalue){
$sql = "SELECT * FROM catalog WHERE $displayby LIKE '$displayvalue'";
}
if($displayby == 'year'){
$min = $_GET['min'];
$max = $_GET['max'];
$sql = "SELECT * FROM catalog WHERE year BETWEEN '$min' AND '$max'";
}
//$result = mysqli_query($con,$sql);
$result = mysqli_query($con,"SELECT * FROM catalog WHERE year BETWEEN '$min' AND '$max'");
while($row = mysqli_fetch_array($result)){
$name = $row['name'];
$filename = $row['filename'];
$cid = $row['cid'];
echo "\n<div class=\"holder\">";
echo "<img src=\"thumbs/$filename\">";
echo "$name<br />\n";
echo "</div>";
}
?>
With this href to only bring up certain characters within those years:
40's Villans<br/>
However they are showing up in the years prior - it might be 1945 as a set date for the character but they only appear in 1930's link.
What am i doing wrong?
Edit : here is the table
BETWEEN ... AND .... can be used for integers and strings. so:
BETWEEN 1 AND 3
Will be true for 1, 2 and 3. But:
BETWEEN 'a' AND 'c'
Will be true for 'a', 'b' and 'c'.
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between
You've put quotes around your numbers, turning them into strings. So the comparison is done on the characters, not the numbers.
Removing the quotes should make it work.
$result = mysqli_query($con,"SELECT * FROM catalog WHERE year BETWEEN $min AND $max");
But as said by others, there are a lot of other problems with the code. This mistake is a symptom of someone who doesn't really know what they're doing. Why not start at the beginning, and grab a good book. Read it. Do the examples. Experiment. It can take years to become a fluent programmer. (This is meant as an ecouragement, not critizm.)
instead of using between you can also use the following statement for getting the desired data as following
$sql = "SELECT * FROM catalog WHERE year >= '$min' AND year <='$max'";
and in phpmyadmin you can run the query to make sure that you are getting the correct data ,and after that you can check your code where you are displaying it
i hope it help's you..

PHP MySQL Get Specific Value When Result Show [duplicate]

This question already has answers here:
Extracting Twitter hashtag from string in PHP
(7 answers)
Closed 8 years ago.
How to get specific value of query in MySQL.
My code :
$q = mysqli_query("SELECT * FROM tb_post WHERE post LIKE '%#%'");
while($d = mysqli_fetch_array($q))
{
$post = $d['post'];
}
Example data result like this :
I'm groot #me
What are you doing #what
Sometimes I felt like superman #me
I don't know what to do #confuse
Now, my question is : is it possible to get just hashtag? Example : #me, #what, #confuse.
So I do not need another value.
Please help!
This should work for you:
$post = $d['post'];
$whatIWant = substr($post, strpos($post, "#") + 1);
echo "#" . $whatIWant . "<br />";
Output:
#me
#what
#me
#confuse

zero not disappearing [duplicate]

This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 8 years ago.
$i=0;
$sql = $dbh->prepare('SELECT kappaleen_nimi, levy_id, kesto FROM kappaleet');
$ok = $sql->execute();
if(!$ok) { print_r( $sql->errorInfo() ) ; }
while($row = $sql->fetch(PDO::FETCH_ASSOC) ) {
foreach($row as $value) {
if($value!=null || $value!=0){
$haku2[$i] = $value;
$i=$i+1;
}
}
}
I'm working with this sql database, where i'm fetching kappaleen_nimi (name of the track), levy_id (record_id) and kesto (length) from the sql table kappaleet. I'm trying to remove fields containing null or 0. However, the final zero gets through anyway through this filter: "if($value!=null || $value!=0)".
When printing my table the following way:
foreach($haku2 as $value){
echo $value.', ';
}
I get this kind of result (last few fields): "Sateen tango, 7, 3.11, 0, "
The final zero is still there, and i can't get my head around it, why the he** it passes the if condition... I know that the final zero is levy_id (record_id).
I think you are looking for an AND conditional instead of an OR. To fix this, replace the following line
if($value!=null || $value!=0){
with this one
if($value!=null && $value!=0){

Adding pagination to my mysql query [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP & MySQL Pagination
I'm a bit confused as to how to add pagination to my mysql query, since I am looping through the results and adding them to a variable each time:
$DBQuery3 = mysqli_query($dblink, "SELECT * FROM images WHERE project_id = '$FormProjectID'");
$ProjectContent ='';
while($row = mysqli_fetch_array($DBQuery3)) {
$DBImageID = $row['image_id'];
$DBProjectID = $row['project_id'];
$DBImageName = $row['image_name'];
$DBImageDescription = $row['image_description'];
$DBDateCreated = $row['date_created'];
$DBLinkToFile = $row['link_to_file'];
$DBLinkToThumb = $row['link_to_thumbnail'];
$DBGivenName = $row['given_name'];
//if the image was given a name by the user, display it
//otherwise display the generated name
if (strlen($DBGivenName) > 1) {
$FileName = $DBGivenName;
} else {
$FileName = $DBImageName;
}
$ProjectContent .= '
<img src="uploads/'.$DBLinkToThumb.'" width="150px" height="150px" alt="'.$FileName.'" title="'.$FileName.'"/>
';
Any pointing in the right direction would be greatly appreciated.
Hy,
Try use a php pagination script, there are a lot on the net, just search for those words.
For example this: http://www.coursesweb.net/php-mysql/pagination-class-script_s2 can paginate data stored into a database, or into an array.

How to do something different for the last item in an array (from mysql_fetch_array)? [duplicate]

This question already has answers here:
Echo a comma on all but the last value? (result from mysql_fetch_array)
(11 answers)
Why doesn't it prevent further looping?
(2 answers)
Closed 8 years ago.
I am trying to make sure that commas go after all of the items EXCEPT for the last one - I am trying to figure out how to calculate out what the last item is for variable lists. I decided to try to use count($variablename) but it doesn't seem to work.
Is there something wrong with my syntax?
$servicearray = mysql_query("select serv_id from org_serv_xref where org_id='".$org_id."'");
$servi = 0;
while ($servicearrayrow = mysql_fetch_array($servicearray)) {
$servdescarray = mysql_query("select serv_desc from service where serv_id='".$servicearrayrow['serv_id']."'");
while ($servdescarrayrow = mysql_fetch_array($servdescarray)) {
if ($servi < 5 OR $servi < count($servdescarrayrow)) {
ECHO $servdescarrayrow['serv_desc'].",";
$servi++;
}
else if ($servi == 5 OR $servi == count($servdescarrayrow)) {
echo $servdescarrayrow['serv_desc'];
$servi++;
}
else {
$servi = 0;
break 2;
}
}
}
Because you're starting $servi at 0. It will always be less than the count of the array. If you start it at 1 it should work the way you expect it to.
change your query to
$servicearray = mysql_query("select GROUP_CONCAT(serv_id) from org_serv_xref where org_id='".$org_id."'");
now sql builds the nice comma delimited list for you :)
As near as I can figure, your trying to do something like this (although I could be totally wrong):
// Initialise vars
$lastid = '';
$items = array();
// Do the query
$query = "SELECT serv_desc, serv_id
FROM service
WHERE serv_id IN (
SELECT serv_id
FROM org_serv_xref
WHERE org_id='".$org_id."'
)
ORDER BY serv_id";
$servdescarray = mysql_query($query);
while ($servdescarrayrow = mysql_fetch_assoc($servdescarray)) {
// We've reached the next ID, echo the result of the last one.
if ($lastid != $servdescarrayrow['serv_id']) {
$lastid = $servdescarrayrow['serv_id'];
if (count($items)) {
echo implode(',',$items);
$items = array();
}
}
if (count($items) == 5) { // We don't want more than 5 items
continue;
} else { // Add an item to the results
$items[$servdescarrayrow['serv_desc']];
}
}
// Make sure we get the last one
if (count($items)) {
echo implode(',',$items);
}

Categories