PHP - Oop Php Data Retrieval Script Issue..
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. Similar TutorialsHello, 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 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? 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 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=358972.0 I 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'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>"; ?>
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"; } ?> 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> "; } ?> 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. 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 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. <?php error_reporting(0); $servername = "localhost"; $username = "user"; $password = "pw"; $dbname = "news"; $datatable = "ttrss_user_labels2"; // MySQL table name $datatable2 = "ttrss_entries"; $results_per_page = 10; // number of results per page // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?> <?php if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = ($page-1) * $results_per_page; $sql = "SELECT * FROM ".$datatable2.", ".$datatable." WHERE article_id = id ORDER BY date_entered DESC LIMIT $start_from, ".$results_per_page; $rs_result = $conn->query($sql); ?> <?php while($row = $rs_result->fetch_assoc()) { ?> <?php $title = preg_replace('/[^(\x20-\x7F)]*/','', $row['title']); $content = preg_replace('/[^(\x20-\x7F)]*/','', $row['content']); ?> <table id="news" border="1" cellpadding="1" class="mx-auto col-xs-12 col-sm-12 col-md-12 col-lg-12"> <tr class="wrap"><td class="rounded-lg shadow curved"> <?php echo '<div charset="UTF-16" class ="text-center" style ="font:14px/18px Arial,tahoma,sans-serif top:5px">', $title, '<hr class=mx-auto></div>'; ?> <?php echo '<div charset="UTF-16" class ="text-center" style ="font:11px/21px Arial,tahoma,sans-serif">', $content, ' ', '<a href= "' . $row['link'] . '" target=_blank>Read More</a>', '</div>'; ?> <div style ="font:9px/15px Arial,tahoma,sans-serif" class="text-right"><? echo date('F j, Y g:i a', strtotime($row["date_entered"])) ?><br></div> <? echo '</td></tr></table><br><br>'; ?> <?php }; ?> <!--Grid row--> <div class="row d-flex justify-content-center"> <!--Grid column--> <div class="top_menu col-xs-12 col-sm-12 col-md-12 col-lg-12"> <div class="container"> <div class="row"><div class="col-md-9"> <ul class="pagination pagination-sm pagination-centered"> <?php $sql = "SELECT COUNT(ID) AS total FROM ".$datatable2; $result = $conn->query($sql); $row = $result->fetch_assoc(); $total_pages = ceil($row["total"] / $results_per_page); // calculate total pages with results $firstPage = 0; // or 1, depending on your implementation $currentPage = (int)$_GET['page']; if ($currentpage != $total_pages) { $nextpage = $currentPage + 1; $prevpage = $currentPage - 1; echo "<li><a href='index.php?page=$prevpage'><<</a></li> "; // for ($i=1; $i<=$total_pages; $i++) { // print links for all pages for($i = $page + 1; $i <= min($page + 4, $total_pages); $i++) { echo "<li><a href='index.php?page=".$i."'"; if ($i==$page) echo " class='curPage'"; echo ">".$i."</a></li> "; }; echo "<li><a href='index.php?page=$nextpage'>>></a></li> "; // echo "<li><a href='index.php?page=$total_pages'>last</a></li></u><br><br>"; } ?> </div></div></div><div class="clearfix">
Hi fellow PHP coders, I have gotten stuck with this code a bit as I was trying to build my first pagination script. I am getting only the 10 latest results from the database on all result pages. I am not sure what exactly I did wrong in this code to make it show all the results database has to offer spanning across pages at a rate of 10 per page from most current first to the oldest. Anyone have any ideas what I am doing wrong here? Any suggestions are much appreciated.
Hi... I encountered problem in my query and while loop my data was duplicates when I join 2 tables, because the one field that I need to display is from another table. here is my code: Code: [Select] <div> <table> <thead> <th>Items</th> <th>Sub Items</th> <th>Item Code</th> <th>Demanded Qty</th> <th>UoM</th> <th>Class</th> <th>Description</th> <th>BIN Location</th> </thead> <?php $sql = "SELECT Items FROM bom_items ORDER BY Items"; $res_bom = mysql_query($sql, $con); while($row = mysql_fetch_assoc($res_bom)){ echo "<tr> <td style='border: none;font-weight: bold;'> $row[Items]</td> </tr>"; //$sql = "SELECT SubItems, ItemCode, UoM, Class, Description FROM bom_subitems WHERE Items = '$row[Items]' ORDER BY Items"or die(mysql_error()); //$sql = "SELECT DISTINCT bs.SubItems, bs.ItemCode, bs.UoM, bs.Class, bs.Description,w.BINLocation FROM bom_subitems bs LEFT JOIN wms w ON bs.Items = w.Items WHERE bs.Items = '$row[Items]' AND w.Items = '$row[Items]' ORDER BY bs.Items, w.Items"or die(mysql_error()); $sql = "SELECT DISTINCT bs.SubItems, bs.ItemCode, bs.UoM, bs.Class, bs.Description,w.BINLocation FROM bom_subitems bs, wms w WHERE bs.Items = '$row[Items]' AND w.Items = '$row[Items]' ORDER BY bs.Items, w.Items"or die(mysql_error()); $res_sub = mysql_query($sql, $con); while($row_sub = mysql_fetch_assoc($res_sub)){ echo "<tr> <td style='border: none;'> </td> <td style='border: none;'> $row_sub[SubItems]</td> <td style='border: none;'> $row_sub[ItemCode]</td> <td> </td> <td style='border: none;' size='3'> $row_sub[UoM]</td> <td style='border: none;'> $row_sub[Class]</td> <td style='border: none;'> $row_sub[Description]</td> <td style='border: none;'> $row_sub[BINLocation]</td> </tr>"; } } ?> I will attach my sample page. Thank you I have a major problem; I have two tables - Category and Product The catID is the primary key for Category and acts as the foreign key for Product. I have some example data below; CATEGORY catID: 3 cat: Pink PRODUCT prodID: 1 product: Fuchsia catID: 3 prodID: 2 product: Dark Pink catID: 3 what I want is the page to display the data like this? Header: Pink Content: Fuchsia Dark Pink Below is the code thus far; Code: [Select] <?php error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", 1); require ('includes/config.inc.php'); include ('./includes/header.html'); require (MYSQL); include ('./includes/main.html'); if($id = isset($_GET['catID'])) { $q = ('SELECT `category`.`catID` , `category`.`cat` , `product`.`product` FROM `product` LEFT JOIN `hairext`.`category` ON `product`.`catID` = `category`.`catID` WHERE `product`.`catID`="'.$_GET['catID'].'"'); $r = mysqli_query($dbc, $q); echo '<div id="right">'; $num = mysqli_num_rows($r); for ($j = 0 ; $j < $num ; ++$j) { $row = mysqli_fetch_row($r); echo '<h1>' . $row[1] . '</h1>'; } while($row = mysqli_fetch_array($r)) { echo '<p>'; echo $row[2]; echo "<br />"; echo '</p>'; } echo '</div>'; } include ('./includes/footer.html'); ?> At the moment, this is not doing what I want, I apologise if this is very long winded, but how do I solve this problem please? My db table has columns "id" (int-11), "date(varchar-50)", "author name(varchar-100)", "text (varchar-100000)"... I am storing images + text in text field of database. Everything is going fine while i am using ckeditor to insert data into the database for that particular text field. Now when i am retrieving data from database... i am able to extract id, date, autorname successfully (i have check it while the issue came)..but when i am trying to retrieve text field which have image, that jumps to page not found error... what could be the issue... am i having problem with database or editor that i am using???? |