PHP - Simple Array Retrieval
I am just starting php and attempting a simple program that will retrieve some data from an array
I have 2 files one with the function containing the array, another with the code that should select a value from the array. I have done what I thought would work however it does nothing. Any help would be greatly appreciated function.php <?PHP function getMonthName(){ $a=array("one","two","three","four","five"); print_r($a); } ?> retrieve.php <?PHP require_once('functions.php'); $number=3; if ($number == $a) { echo "$a"; } ?> Similar TutorialsI have a problem where I am unable to display a DB entry that for exampe says "John Doe". The code I am using is: Code: [Select] <tr> <td>Agent Full Name:</td> <td><input type="text" name="fullname" value='.$agentdata['fullname'].'></td> </tr> The output shows "John" instead of "John Doe", have I missed something here I can't get it to use the item that is selected in the drop down box as the variable $va in the bit where it then queries the data base to retrieve only the line in the able that match the program_name with variable $va. variable $va would be the result of the drop down box. Below is the code i have so far, any help would be appreciated. Cheers. Code: [Select] <?php include ('dbConn.php'); mysql_select_db($dbselect, $con); $QuerySelects = "SELECT program_name FROM program_names"; $Query = mysql_query($QuerySelects) or die (mysql_errno() . ": " . mysql_error(). "\n"); echo '<label>Select Sto </label>'; echo '<select id="program_name" name="program_name">'; echo '<option value="va">Select Store</option>'; while ($row = mysql_fetch_assoc($Query)) { $va = $row['program_name']; echo "<option value=''>$va</option>"; } echo '</select>'; $QuerySelects1 = "SELECT * FROM offers1 WHERE end_date>CURDATE() AND program_name = '$va'"; $Query1 = mysql_query($QuerySelects1) or die (mysql_errno() . ": " . mysql_error(). "\n"); while($result=mysql_fetch_assoc($Query1)) { include ('variables.php'); echo" <div class='spacerbox'> <div class='outerbox eviecodes'> <div><div class='topbox'> <div class='leftbox'> <div class='offerimage'> <div class='progdiv'><a class='progname' href=".$url." target='_blank'>".$program_name."</a></div> </div> </div> <div class='rightbox'><div class='descbox boxwidth'><h1> <a href=".$url." target='_blank'>".$description." at ".$program_name."</a></h1></div> <div class='voubox boxwidth'><h2 class='vvv'>Voucher Code:<span class='vcode'>".$code."</span ></h2></div> </div> </div> <div class='linkbox boxwidthl'> <a href=".$url." target='_blank'>To Take Advantage of this offer at <span class='prodname'>".$program_name."</span>, click here!</a> </div> <div class='expires'> <span class='end'>Expires:</span> <span>".$dateformat."</span > </div> <div class='socialbox'> {module Tell A Friend Module} </div></div> <div class='spacer'> </div> </div> </div> "; } ?> I'm going bananas with one of those issues where it looks like everything is right but the code won't work. I've included a script below that fetches data from a database and creates a drop down menu. I'm trying to use a field called "gone" and enter 'gone' in that field when I want to delete the row but be able to retrieve it by making the field empty. There is something in the WHERE clause of my sql that is screwing things up. I get nothing retrieved with any of the WHERE clauses below. Interestingly, when I take the where clause out completely, I get the rows that have no 'gone' in them. The code is below. None of the following in the included code get me a result. All of them return nothing. I also don't get any errors. Just nothing. WHERE gone != 'gone' WHERE gone <> 'gone' WHERE gone = '' WHERE gone = 'gone' Without the WHERE clause it runs fine. As a work around I tried using if($row['gone'] == 'gone') { continue;} In the while.. function at the end of the file to bypass rows with 'gone' in the field. It also stopped the script. Here's my code: <?php // person_selectall.php /** Create new pdo object */ require 'Db.php'; //$sql = "SELECT person_id, fname, lname FROM Persons"; $sql = "SELECT * FROM Persons WHERE gone = ''"; $stmt = $pdo->prepare($sql); $stmt->execute(); echo "<p><select class=\"select-field\" name=\"person_id\" >\n"; while ($row = $stmt->fetch()) { $name = $row['fname'] . ' ' . $row['lname']; echo "<option value=\"" . $row['person_id'] . "\">" . $name . "</option>\n"; } echo "</select>"; ?>
Hi,
I have made a database linked with php that I use a html form for. The HTML form requests a particular date to retrieve, and the php form it is linked to then retrieves data from the MySQL database from that date.
My question is: I want there to be a restriction so that the retrieval can only be for dates before the present date (and certainly not any date in the future).
e.g. if it is 6th July 2020, then the database can only retrieve from the 5th July 2020 and before. (7th July 2020 onwards also is not allowed.) Is there a way I can do it? Anyone who can guide me to a particular code function or something of that sort - I would be deeply grateful as my searches have not come up fruitful. Edited July 10, 2020 by samanjclarity Hi all! I am trying to get an admin status ("y" or "n" to be retieved from an SQL select. I do so in the following function: Code: [Select] function get_admin_status($username) { // query database for the name for a category id $conn = db_connect(); $result = $conn->query("select admin from user where username='".$username."'"); $result = @$conn->query($query); if (!$result) { return false; } $num_cats = @$result->num_rows; if ($num_cats == 0) { return false; } $row = $result->fetch_object(); return $row->admin; } I then utilize this function in another file in the following code. However, the adminhome.php page never loads. It always goes to "survey1.php" . I'm not sure why this is happening. Any help would be appreciated. Thanks for you time! Code: [Select] if ($username && $password) { // they have just tried logging in login($username, $password); $admin = get_admin_status($username); if($admin == "y"){ header("Location: adminhome.php"); } else{ //login($username, $password); // if they are in the database register the user id $_SESSION['valid_user'] = $username; $_SESSION['admin'] = $admin; header("Location: survey1.php"); } Hello everyone, Let's say I am connecting to a database using PDO (I am excluding prepared statements for the sake of simplicity): try { $conn = new PDO('mysql:host=localhost;dbname=cms', 'root', 'password'); $result = $conn->query('SELECT * FROM news'); } catch (PDOException $e) { echo 'Error: '.$e->getMessage(); exit; } So far I been using the following to manipulate the data in object form: while ($row = $result->fetchObject()) { echo $row->title; echo $row->text; } My question is, is there a better way? What is the foreach code equivalent to that? I have had no luck keeping the data in object form using foreach statements. Which method is faster, while or foreach? Thank you, any suggestions are highly appreciated. Hello, I am writing a data retrieval script, in order to print data stored in a DB. For some reason the script does not work.. More details below.. Code: [Select] the form that calls the initiator script.. <form method='get' action="init.Get.php"> <div><input type="submit" name="get" value="See Data"></div> </form> Code: [Select] the initiator script <?php include 'dataGet.class.php'; $data= new getData(); $data= getData($name, $email, $text); ?> Code: [Select] my class.. <?php class getData { private $name; private $email; private $text; function __construct(){ $this->host = 'localhost'; $this->uname = 'root'; $this->pword = '1111'; $this->dbname = 'teststorage'; $this->dbtable = 'userData'; } function getData($name, $email, $text){ $this->dbconnect = mysql_connect($this->host, $this->uname, $this->pword); if (!$this->dbconnect) die("Connection Unable " . mysql_error()); mysql_select_db($this->dbname); $sql_query = "SELECT * FROM $this->dbtable "; $result = mysql_query($sql_query); if ($result){ echo $result; } else{ echo "Retrieval Unsuccesful"; } mysql_close($sql_query); } } ?> Can someone please tell me what am I doing wrong? Thank you in advance. Hi I am currently making a site where users can upload second hand books for sale, I have everything working as i wish apart from i am having trouble creating a account page where users can review the books they have posted. so far i use the following code to bring view a the data associated with an uploaded book based on its id Code: [Select] public static function getById( $id ) { $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sql = "SELECT * FROM books WHERE id = :id"; $st = $conn->prepare( $sql ); $st->bindValue( ":id", $id, PDO::PARAM_INT ); $st->execute(); $row = $st->fetch(); $conn = null; if ( $row ) return new Book( $row ); } I think i can use a similar function for my user account however, each book entered by a user has their member id attached to it, which is gained from there id in the members table which in turn is stored in $_SESSION['id']. My question is, is there a way to rework the code above to have something like select * where member_id = $_SESSION['id']. I have tried a few things and get stuck manly due to the fact i dont know what to replace the current :id with. Thanks in advance, i hope i explained it well enough for you lot to understand. Ian Hello, When I use PHP to gain data from another webpage I usually use file_get_contents Is there a faster way to retrieve data from webpages faster since it only seems to do around 200 website line retrievals per minute? Is cURL faster? Thank you Here's the site: http://www.secretauctions.com/thank_atv_cb754.php# (If anyone wants to see how the problem is occurring. The Username and Password shown on the page are functional.) Here's the problem: Whenever anyone tries to click to the second page of results, the Session fails. Here's a short version of the code: Code: [Select] <?php $username = "user493d"; $password = "w7atv"; session_start(); $my_array=array($username, $password); $_SESSION['login']=$my_array; if ($_SESSION['login'][0]=="user493d" && $_SESSION['login'][1]=="w7atv") include("atv_list.php"); else include("sorry1.php"); ?> This works GREAT on the initial log-in. But the moment anyone tries to go to the second page of results, the Error state trips, and they get the "sorry" page. This isn't supposed to happen. This is my first foray into Sessions, so I'm certain there's something I'm not understanding, but shouldn't the above code store the username/password, then use it to verify page reloads? Can anyone tell me why it's allowing the Fail state to trip, when the initial test succeeds? Or am I doing the whole thing wrong? I have the following query: $getVideos = mysql_query("SELECT catergory, COUNT(catergory) as 'catCount' FROM videos GROUP BY catergory"); Using this in PHPMyAdmin returns the correct results of: catergory | catCount 0 | 7 1 | 1 10 | 2 How would get those results into a PHP array or what not...I have done this before but along time ago and cannot for the life of me remember how to do it. Hopefully someone can point me in the right direction? Regards, PaulRyan. Hi guys, I have a file location stored in mysql. when i populate the table i need this file location to be a hyperlink to the file itself, so the visitors can click like a normal link and open the file in word and pdf (both formats stored). example of file location as in db "_private/Incident_Reports/Incident%20-%20Applecross%20-%2017%20December%202010%20-%20Website.doc" example of php code Code: [Select] echo $row['word_document']; any ideas would be really appreciated. This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=358972.0 Hey, Can someone please fix my code below? I simply want to add all values for $row[score1] in first if statement and add all values for $row[score2] in second if statement and then add them together to return final value . Code: [Select] while( $row = mysql_fetch_assoc($query)) { if($clanID==$row[clan1] && $row[score1] > $row[score2]) { $points[] = $row[score1]; } if($clanID==$row[clan2] && $row[score1] < $row[score2]) { $points[] = $row[score2]; } } return $points;. So lets say I echo the variables that I want added... Code: [Select] if($clanID==$row[clan1] && $row[score1] > $row[score2]) { $points[] = $row[score1]; echo $row['score1']."<br>"; } if($clanID==$row[clan2] && $row[score1] < $row[score2]) { $points[] = $row[score2]; echo $row['score2']."<br>"; } On page it returns as: 11 16 14 Add it together is 41 and this is what I want the output to be. Hope you understand I appreciate any help! P.S. I must be able to return final value OUTSIDE loop. Thanks. Hey guys I have an array and want to make it into a sting and separate each array segment by a comma...is there a function for this or should I just do it in a loop myself?...thanks Hi all, I've been trying to find a simple script that can output an array of months within a given year which each contain the days for that month. Something along the lines of this: Code: [Select] [1] => Array ( [0] => Array ( [num] => 1 [ts] => 2011/08/01 ) .... 1 being the month (jan) 0 is the day (which contains the day & full date) Does anybody know of a script that does something along these lines as all the ones i have found seem over compliated and are outputting into tables whereas i only need this as an array? Thanks! Hello everyone, I have a question with the use of the functon in_array... I have a list of categories in my table and I want to know if a specific category exists so I am doing this: $categoriasexistentes= mysql_query("SELECT * FROM LugaresTuristicos WHERE DestinoPeg='$LinkDestino' GROUP BY Clasificacion ") or die(mysql_error()); $array = mysql_fetch_array($categoriasexistentes); print_r($array); WHAT IS DISPLAYED BY print_r (there should be much more categories) IS THIS: Array ( => Bar [Clasificacion] => Bar ) If I ad this all categories are displayed: while($categoria = mysql_fetch_array($categoriasexistentes)) { $todas=$categoria['Clasificacion']; ?> <?php echo"{$todas} - "; ?> <?php }; ?> |