PHP - Return Rows
I am trying make my code display different things depending on how many rows are returned
This is my code Code: [Select] <?PHP include 'db_con.php'; $month = date("n"); $day = date("j"); $sql = mysql_query("SELECT * FROM table WHERE month = $month AND day = $day ")or die(mysql_error()); while($row = mysql_fetch_array($sql) ) if (mysql_fetch_array($row = 1 ) ) ///THIS IS AN ERROR BUT IF ONE ROW RETURNED THEN THIS { echo "ONE THING"; } elseif (mysql_fetch_array($row >1 ) )////IF > 1 ROW RETURNED THAN THIS { echo"TWO THINGS"; } else { echo "SOMETHING ELSE "; } ?> I have tried several variations of the above code but cannot get it to work. Thanks for any help anyone can give me. Similar TutorialsGiven the below 3 database tables I am trying to construct a SQL query that will give me the following result: customer_favourites.cust_id customer_favourites.prod_id OR product.id product.code product.product_name product.hidden product_ section.section_id (MUST BE ONLY THE ROW WITH THE LOWEST SECTION ID, I.E. ROW ID #44108) product_ section.catpage (MUST BE ONLY THE ROW WITH THE LOWEST SECTION ID, I.E. ROW ID #44108) product_ section.relative_order (MUST BE ONLY THE ROW WITH THE LOWEST SECTION ID, I.E. ROW ID #44108) I currently have.... SELECT customer_favourites.cust_id, customer_favourites.prod_id, product.code, product.product_name, product.hidden, product_section.section_id, product_section.relative_order, product_section.catpage FROM customer _favourites INNER JOIN product ON customer_favourites.prod_id = product.id INNER JOIN product_section ON product_section.product_code = product.code WHERE `cust_id` = '17' AND `hidden` = '0' GROUP BY `code` ORDER BY `section_id` ASC, `relative_order` ASC, `catpage` ASC LIMIT 0,30 This gives me what I want but only sometimes, at other times it randomly selects any row from the product_section table. I was hoping that by having the row I want as the last row (most recent added) in the product_section table then it would select that row by default but it is not consistent. Somehow, I need to be able to specify which row to return in the product_section table, it needs to be the row with the lowest section_id value or it should by the last row (most recent). Pulling my hair out so any help is gratefully received. customer_favourites id cust_id prod_id 70 4 469 product id code product_name hidden 469 ABC123 My Product 0 product_section id section_id catpage product_code relative_order recommended 44105 19 232 ABC123 260 1 44106 3 125 ABC123 87 1 44107 2 98 ABC123 128 1 44108 1 156 ABC123 58 0 what Im basically trying to do is just like a phpmyadmin function... you select rows you want to update with a checkbox and then it takes you to a page where the rows that are clicked are shown in forms so that you can view and edit info in them... and then have 1 submit button to update them all at once. I have 2 queries that I want to join together to make one row
Hey all. My site has a config database table that has a column showlogin to set whether or not login functionality is enabled. I'm playing around with MySQLi, Classes, and Returns. My return is always 1. Where am I going wrong? test.php <?php // Start Session session_start(); // Production Settings ini_set('display_errors', 1); mysqli_report(MYSQLI_REPORT_ALL); include 'db.php'; $mysqli = new mysqli($host, $user, $pass, $db); class styles { function showlogin($show) { global $mysqli; $select = $mysqli->query("SELECT showlogin FROM config LIMIT 1"); $value = $select->fetch_object(); if ($value->showlogin = 1) { return 1; } return 0; } } $login = new styles(); echo $login->showlogin($show); ?> Hi Everyone,
My name is Raoul Grosser, live in The Netherlands.
I'm a front/backend developer working with AngularJS and Bootstrap and Symphone2.
I've been writing PHP code for over 4 years now my focus is on opensource projects.
If you have any question, please let me know!
Hello First of all i am very weak about OOP and classes in php. I am learning now but i stuck with very silly problem. My codes are below. <?php class myTestClass { function __construct() { $this->OldName("This is Old Name"); } function OldName($VeryOld) { if($VeryOld == "This is Old Name") $this->NewName(); else $VeryOld = "Something Wrong"; return $VeryOld; } function NewName() { echo "This is Brand New Name"; } } $i = new myTestClass(); ?> if i send value to OldName "This is Old Name" then codes works fine. But if i send "This is Old Nameeee" then does not appear anything. what i want to do is print "Something Wrong" text if i enter different value. I have no idea what return do and how it do? How to retrieve/print "Something Wrong"? i can do that with echo but i want to transfer result of return to another function. Really appreciate for any help/idea. return (int) $value VS return $value
What is the purpose of using int in a bracket? return (int) $value
Which is the best and safest method?
Thank you
If a user is on Page X, but not logged in, when they log in to my website, I want them to return back to Page X (in this example). To handle this, I am adding this code to the top of each webpage... Code: [Select] // Set current Script Name. $_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME']; And then as part of my Log In script I have... Code: [Select] // Redirect User. if (isset($_SESSION['returnToPage'])){ header("Location: " . BASE_URL . $_SESSION['returnToPage']); }else{ // Take user to Home Page. header("Location: " . BASE_URL . "index.php"); } What do you think about this approach? Thanks, Debbie I have a form page (referred to as form.php) which is then processed via a submit button, the script then processes the data from the form.php and if it discovers an error it then sets a variable containing the error message my process page contains Code: [Select] if (empty($b_addr)) { $b_addr_error = "Address is required"; $check_failed = TRUE;} header("Location: form.php"); then my form.php contains Code: [Select] $b_addr_error = safe($_GET['b_addr_error']); if (isset($_POST['$b_addr_error'])) { echo "<div class='errorfont' align='center'>'".$b_addr_error."';</div>"; } but the variables do not get passed back to form.php from the process page, any ideas? Thank you. How to do that when you turn 360 degrees no longer come back? Only in this way was? Hi, I have setup my paypal payment method for a shopping cart on my site, all is well but in order for my server to flag an invoice as paid, the user must return to my website after paying and the query string in the url give the script the go ahead to mark it as paid which in turn alerts the appropriate department that its ok to source the order. ~The problem is, if the user for whatever reason skip the redirect back to my site, the invoice is remain 'unpaid' and has to be changed manually when the payment has been checked in the account. As you might imagine this is a bit of a problem because not only can it cause quite a delay, it also requires manually changing values which can lead to other problems. So if there is a better way i would appreciate the info, thanks a lot. Hi everyone for my end of studies projects I decided to do a little e-commerce problem on the filter .. I use an ajax request at the click of the checkbox this goes to my controller which calls a method of my Model class but the concern is that .. if the array $ _POST ['brands'] has several elements then only the last one is returned to me? I thought I saw one day that if the same variable is in a url then the last value will be returned to me is for security but how can I do ... I have been there for too long .. and finally despair. . Help me please... :') request payload: brands%5B%5D=arai&brands%5B%5D=bell reply :
<- prepare var_dump ()
string (4) "arai" <- bind
var_dump () of my fetchAll () since the last value taken into account is bell then I get two items of the bell brand from my bdd but no arai ... Edited October 24, 2020 by jcb789error Where should I validate the return value?
In the function should I validate the value before returning it.
Or once the value has been returned, should I check it?
Is it really necessary to validate the return value?
Thank you.
Hi guys! I'm new to PHP and I'm using Drupal and there's a function that one block will be called if PHP scripts returns value TRUE. Now I want my "Search" to only show at ALL THE other pages but not index. With the code: if (Url::exists('http://www.example.com')) I get my Search function to return TRUE so it will be showed on index page only, but not on any other pages!! So I kinda need this on reverse - how do I get a TRUE value for something that excists? In this case it would be www.example.com, or if anyone else has any other ideas I would be very thankful! let say i have a function function checkme($x) { if(strlen($x)>10) { return $x; } else { return false; } } $variable=checkme("5"); so two questions i wish to ask... 1. If i wish to get the empty string for the $variable, then is it correct to have return false there? 2. If it is true that return shall not be used twice in a function as it will somehow reduce bytes space or something like that?? thanks in advance By calling function two will it break the loop?; or do i have to echo what is returned? Code: [Select] functione one() { $getids = mysql_query("SELECT * FROM table"); while($row=mysql_fetch_assoc($getids); { $this->two(); } } function two() { return 'break;'; } Say I have my login form which then when person tries to log in with the wrong user name and password and sent straight back to the log in page, how do I send back they got the password and/or username wrong. I read somewhere in some code header("location: index.php&$errorvariable"); or something like that. I haven't tested it, not even sure how to get it to work on the index, would I just use an isset on a get/post to check if it exists then echo the error? Hi all, can you give me suggestion how to return value from function like this Code: [Select] function returnArray(){ for($i=1; $i<=3; $i++){ return $i; //how to return this as array?? } } when I code "echo" inside the function then call the function it give me output => 123 but when I code "return" inside the function, it will give output => 1 Can you suggest me how to output it as array? Hi all, I am kinda stuck on the return() statement in the php.net manual about control structures. (http://au.php.net/manual/en/function.return.php) All I see is notes on when not to use it and some scary examples with loads of foo and bar but, it's hard to find out when it would be nice to use it. At the moment i am thinking its another way of echo, but I bet that's wrong. If someone could enlighten me a bit on when return() is useful I would be very happy since i see it more often everyday. CHeers! |