PHP - Problem With Mysql Retrieval
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>"; ?>
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 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"; } ?> 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 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 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. 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, 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. 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 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. 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? 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 this is probably going to be really simple to fix but i cant figure it out :/ on one page i have a form which uses php to populate a dropdown box, now this form will allow the user to add 1 to the person which was selected in the dropdown box. The code for this looks like this.. <form action="addpoint.php" method="post"> <select> <?php $sql="SELECT id,name FROM man"; $result =mysql_query($sql); while ($data=mysql_fetch_assoc($result)){ ?> <option value ="<?php echo $data['id'] ?>" ><?php echo $data['name'] ?></option> <?php } ?> </select> <input type="submit" value="Add Point"/> </form> the php to process this form looks like this.. <?php $sql="UPDATE man SET points = (points + 1) WHERE name = ('$_POST[name]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "$_POST[name] has been added"; mysql_close($con) ?> The problem i have is that i dont think its pulling the name from the form so therefore it wont effect the database, ive tried the sql like this.. UPDATE man SET points = (points + 1) WHERE name = 'joe' and it works fine, any help would be gratefully received Owen Well im using paypal IPN im trying to get it to update something out of my database. Heres the code I think this is all you need Quote <?php include('config.php') ?> <?php function updatePayments($data){ global $link; if(is_array($data)){ $sql = mysql_query("INSERT INTO `serverb` WHERE name = 'Official' (gold) VALUES ('yes')"); } } ?> How do i get it to work? I creating a members only web for my wife's 45th reunion. Everything went great untill I tackled something I never did before. PHP! From the home page I'm having the user enter a username and password. I created the db using phpmyadmin and populated the table with two users. I checked the include and it is attempting to open the db and proper table. however, the php loginck.php, goes to the programed error message. Can I get a little assistance, it would be greatly appreciated. The form calls loginck,pfp and it gets there OK. <div class="art-blockcontent"> <div class="art-blockcontent-body"> <!-- block-content --> <div style="border: medium ridge blue;padding:15px; font-size:.8em;"> To assist in maintaining contact with our alumni, please register. <br> Already registered, sign in. <br><br> <form action="loginck.php" method="POST"> <label>Username: </label><input type="varchar" style="height: 1em;" size="15" name="passWord" required> <br> <label>Password: </label><input type="password" style="height: 1em;" size="15" name="passWord" required> <br><br> <center><input type="submit" value="Sign In"> <input type="reset" value="Clear"></center> </form> <br><br> <a href="register.html">Register</a> <br> <hr> <br> This site best viewed using Google Chrome, FireFox, or Opera browsers. </form> </div> <!-- /block-content --> I attached loginck.php and the inserted a called file 'z_db.php' for your review. I know if I can get this figured out I can do the others I need to do. Sorry, if ther's too many typos, I'm relegated to typing with one finger (Stroke) and in a wheel chair. So i started on a project and not within long, i hit a wicked wall. The upper part works fine, i get the correct $pilotcorpid, however it doesnt seem to query the 2nd time, it just uses the variable from the first time. I have the information in the database, i just need to pick em out right. Code: [Select] <?php include 'config.php'; //database config include 'opendb.php'; //connect database $search = @$_GET['q'] ; //getting a name from a form $result = mysql_query("SELECT * FROM kb3_pilots WHERE plt_name='$search'")or die(mysql_error()); //here i choose to check in the kb3_pilots table, the plt_name column for the name written in the form earlier. $pilotcorpid=mysql_result($result,"plt_crp_id"); //since i found the guy in the query, i need info from that column, the plt_crp_id. $resultcorp = mysql_query("SELECT * FROM kb3_corps WHERE crp_id='$pilotcorpid'")or die(mysql_error()); //now we query the kb3_corps table to check if the corp exists. $pilotcorpname=mysql_result($resultcorp,"crp_name"); //Because it did, i will now pull out the name echo "TEST: Pilot Id: $pilotcorpid and corp name: $pilotcorpname"; include 'closedb.php'; ?> If somebody also have a big php/mysql ebook which explains commands great, that would be awesome. Best regards, Mumlebumle |