PHP - Selecting A Name From Table 2 But Get No Returns
I have a table called users with a fieldname called service_id. In a table called services I have id and name.
I want to query the users table and, based on the service_id, display the name of the service (which is stored in the services table). However, when I try this code I get No records returned. I later echo out under a while statement $row['name']; or $row['id'] $query = "SELECT users.username, users.lname, users.fname, users.service_id, services.name, services.id FROM users, services WHERE users.inst_id = '".$userarray['inst_id']."' and users.id !='".$userarray['id']."' and users.service_id = services.id "; Similar TutorialsHi All ,
I have a small table with 4 fields namely Day_ID, Dues, Last_Visit, Points. where Day_ID is an auto-increment field. The table would be as follows:
Day_ID -- Dues --- Last_Visit --- Points.
1 --------- 900 -------- 1/12 -------- 6
2 --------- 700 -------- 4/12 -------- 7
3 --------- 600 -------- 7/12 -------- 5
4 --------- 600 -------- 9/12 -------- 6
5 --------- 600 -------- 10/12 ------- 6
6 --------- 600 -------- 14/12 ------- 6
So this is the record of a person's visit to say a club. The last row indicates the last date of his visit to the club. His points on this date are 6. Based on this point value of 6 in the last row I want to retrieve all the previous BUT adjoining all records that have the same Points i.e. 6.
So my query should retrieve for me, based on the column value of Points of the last row (i.e. Day_ID - 6 ), as follows:
4 --------- 600 -------- 9/12 -------- 6
5 --------- 600 -------- 10/12 ------- 6
6 --------- 600 -------- 14/12 ------- 6
This problem stated above had been completely resolved, thanks to a lot of help from Guru Barand by this following query :-
$query = "SELECT cv.day_id, cv.dues, cv.last_visit, cv.points FROM clubvisit cv WHERE last_visit >= ( SELECT MAX(last_visit) FROM clubvisit WHERE points <> ( SELECT points as lastpoints FROM clubvisit JOIN ( SELECT MAX(last_visit) as last_visit FROM clubvisit ) as latest USING (last_visit) ) )";I am using this and it works perfectly except that now there is a slight change in the table because the criteria for points is now dependent on more than one column cv.points and is more like cv.points1, cv.points2, cv.points3 etc. So now I need to make a selection based on each of these cv.points columns. As of now I can still get the results by running the query multiple times for each of the cv.points columns ( seperately for cv.points1, cv.points2, cv.points3) and it works correctly. However I am wondering if there is a better way to do this in just one go. This not only makes the code repetitive but also since the queries are interconnected, involves the use of transactions which I wish to avoid if possible. The values that I require for each of the cv.point columns is 1. day_id of the previous / old day on which the cv.points value changed from the current day value, and 2. cv.points on that old/ previous day. So for example if the table is as below: Day_ID -- Dues --- Last_Visit --- Points1 --- Points2. 1 --------- 900 -------- 1/12 ----------- 9 ------------ 5 2 --------- 600 -------- 4/12 ----------- 6 ------------ 6 3 --------- 400 -------- 7/12 ----------- 4 ------------ 7 4 --------- 500 -------- 9/12 ----------- 5 ------------ 8 5 --------- 600 -------- 10/12 ---------- 6 ------------ 8 6 --------- 600 -------- 11/12 ---------- 6 ------------ 8 7 --------- 600 -------- 13/12 ---------- 6 ------------ 7 8 --------- 500 -------- 15/12 ---------- 5 ------------ 7 9 --------- 500 -------- 19/12 ---------- 5 ------------ 7 Then I need the following set of values : 1. day_id1 -- Day 7, points1 ---- 6, days_diff1 -- (9-7 = 2) . // Difference between the latest day and day_id1 2. day_id2 -- Day 6, points2 ---- 8, days_diff2 -- (9-6 = 3) 3. day_id3 -- .... and so on for other points. Thanks all ! Hi All, I am trying to pull a record from my table using the date of birth column (which is a date column) like so.. Code: [Select] $birthday = ('1936-08-21'); $query = "Select * from my_table where dob = $birthday"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)){ echo "Name is - " .$row['name']; } I know there is a record in the table with a date of birth(dob) value of 1936-08-21 but it does not return any records ? thanks for looking, Tony Hi, I've got a basic sign up form but I want a drop down list which will list different catergories that relate to different tables which when selected will input the sign up information into that table which was selected from the catergory drop down. This is the signup form <html><head><title>Birthdays Insert Form</title> <style type="text/css"> td {font-family: tahoma, arial, verdana; font-size: 10pt } </style> </head> <body> <table width="300" cellpadding="5" cellspacing="0" border="2"> <tr align="center" valign="top"> <td align="left" colspan="1" rowspan="1" bgcolor="64b1ff"> <h3>Insert Record</h3> <form method="POST" action="test.php"> <? print "Enter Company Name: <input type=text name=company_name size=30><br>"; print "Enter Contact Name: <input type=text name=contact_name size=30><br>"; print "Enter Telephone: <input type=text name=telephone size=20><br>"; print "Enter Fax: <input type=text name=fax size=30><br>"; print "Enter Email: <input type=text name=email size=30><br>"; print "Enter Address: <input type=text name=address1 size=20><br>"; print "Enter Address: <input type=text name=address2 size=30><br>"; print "Enter Postcode: <input type=text name=postcode size=30><br>"; print "Enter Town / City: <input type=text name=town_city size=20><br>"; print "Enter Website: <input type=text name=website size=30><br>"; print "Enter Company Type: <select name='table'> <option>stationary</option><option>reception</option></select><br>"; print "<br>"; print "<input type=submit value=Submit><input type=reset>"; ?> </form> </td></tr></table> </body> </html> This is the part which I can't figure out and is probably totally wrong! Im trying to use this script to sort the drop down list to then run the correct script to insert the form data. <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Hello!</title> </head> <body> <?php if($_POST['table']=='stationary' 'birthdays_insert_record.php') else if($_POST['table']=='reception' 'insert_reception.php') ?> </body> </html> This is the script which works! that inserts the form data into a specific table <html><head><title>Birthdays Insert Record</title></head> <body> <? /* Change db and connect values if using online */ $company_name=$_POST['company_name']; $contact_name=$_POST['contact_name']; $telephone=$_POST['telephone']; $fax=$_POST['fax']; $email=$_POST['email']; $address1=$_POST['address1']; $address2=$_POST['address2']; $postcode=$_POST['postcode']; $town_city=$_POST['town_city']; $website=$_POST['website']; $db="myflawlesswedding"; $link = mysql_connect('localhost', 'root' , ''); if (! $link) die(mysql_error()); mysql_select_db($db , $link) or die("Select Error: ".mysql_error()); $result=mysql_query("INSERT INTO reception (company_name, contact_name, telephone, fax, email, address1, address2, postcode, town_city, website) VALUES ( '$company_name', '$contact_name', '$telephone', '$fax', '$email', '$address1', '$address2', '$postcode', '$town_city', '$website')") or die("Insert Error: ".mysql_error()); mysql_close($link); print "Record added"; ?> <form method="POST" action="birthdays_insert_form.php"> <input type="submit" value="Insert Another Record"> </form> <br> <form method="POST" action="birthdays_dbase_interface.php"> <input type="submit" value="Dbase Interface"> </form> </body> </html> I hope somebody can help me out here! or can point me in a better way to sort this problem! Thanks for any advice! Hello, i have a query that returns the rows of certain parts of a databse. i made a button on the end of each echo, to make sure that, the button being printed with each echo, correlates to that echo. However, the SESSIONS only return the same varaibles each time. The last in the list. $sql = "SELECT p.Title, p.PerfDate, p.PerfTime FROM performance AS p INNER JOIN production AS r ON p.Title = r.Title"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo '<tr> <td>'.$row['Title'].'</td> <td>'.$row['PerfDate'].'</td> <td>'.$row['PerfTime'].'</td> <td><button name="availability" value="'.$row['Title'].'">Availability</button></td> </tr>'; $_SESSION["Title"] = $row['Title']; $_SESSION["PerfDate"] = $row['PerfDate']; $_SESSION["PerfTime"] = $row['PerfTime']; } } else { echo "0 results"; } $_SESSION["name"] = $_GET["name"]; ?> How am i able to make it so each button correlates to each row?
There's a screenshot of how it looks for reference Edited December 8, 2019 by AdamSteele this is supposed to display the distinct first two characters of a string(billnmbr) in a combobox ive tried this on phpmyadmin and it returns 08.. which is correct when i run this on my server, it returns nothing.. there must be something wrong with the way i called the billnmbr does anybody have an idea on how i could call and display this correctly? thanks and have a nice day. <?php $mquery = "select distinct(substring(`billnmbr`, 1, 2)) from `tbltest`"; $mres =mysql_query($mquery) or die(mysql_error()); while($mrow=mysql_fetch_array($mres)){ $mm = $mrow['billnmbr']; echo "<option value=" . $mrow['billnmbr'] . ">"; echo $mrow['billnmbr'] . "</option>"; } echo " </select>"; ?> I'm hoping someone can help me cos my hair's going white with this one. I'm trying to put together a script that acts as web-based interface to an SQL server. There are actually two parts, admin.htm and admin.php. The first part is just a form that passes login credentials to the PHP file. That part seems to work fine, but I'll post the source anyway: - Code: [Select] <!DOCTYPE HTML> <html lang="en"> <head> <title>SQL admin login</title> <meta charset="iso-8859-1" /> </head> <body> <form action="admin.php" method="post"> <label for="username">Username: -</label> <br /> <input type="text" name="username" id="username" /> <br /> <br /> <label for="password">Password: -</label> <br /> <input type="password" name="password" id="password" /> <br /> <br /> <label for="server">Server: -</label> <br /> <input type="text" name="server" id="server" /> <br /> <br /> <label for="database">Database: -</label> <br /> <input type="text" name="database" id="database" /> <br /> <br /> <input type="submit" value="Login" /> <input type="reset" value="Reset" /> </form> </body> </html> Following is the content of admin.php. By this point I can see the connection in MySQL Workbench, and when I submit the query 'SELECT * FROM subscribers' it's being stored in '$_POST['query']', but 'mysql_query($_POST['query'],$_SESSION['con']);' is returning nothing. There is definitely a record in that table, and the user I'm logging on with has permission to run the 'SELECT' command against this database, so I can't figure out why mysql_query(); is returning nothing: - Code: [Select] <!DOCTYPE HTML> <?php session_start(); if(!$_SESSION['con']) { if(!($_POST['username'] || $_POST['password'])) { if(!($_SESSION['username'] || $_SESSION['password'])) { $error="Username and password variables empty."; } } else { $_SESSION['username']=mysql_real_escape_string($_POST['username']); $_SESSION['password']=mysql_real_escape_string($_POST['password']); $_SESSION['server']=mysql_real_escape_string($_POST['server']); $_SESSION['database']=mysql_real_escape_string($_POST['database']); $_SESSION['con']=mysql_pconnect($_SESSION['server'],$_SESSION['username'],$_SESSION['password']); if(!$_SESSION['con']) { $error="Failed to connect to server."; } else { $database=mysql_select_db($_SESSION['database'],$_SESSION['con']); if(!$database) { $error="Failed to connect to database."; } } } } if(!$_POST['query']) { $error="No query submitted."; } else { $result=mysql_query($_POST['query'],$_SESSION['con']); if(!$result) { $error="Query returned nothing."; } } ?> <html lang="en"> <head> <title>SQL admin interface</title> <meta charset="iso-8859-1" /> </head> <body> <form action="admin.php" method="post"> <textarea name="query" rows="10" cols="50">SELECT * FROM subscribers</textarea> <br /> <br /> <input type="submit" value="Submit query" /> </form> <?php if($error) { echo $_POST['query']."<br /><br />".$result."<br /><br />".$error; die(); } else { while($row=mysql_fetch_assoc($result)) { echo $row['name']." ".$row['email']; echo "<br />"; } } ?> </body> </html> Can anyone help? MOD EDIT: [code] . . . [/code] tags added. I'm getting frusted over an if else statememt that won't work. It is calling one variable from the table. If the variable is in the table it will display one thing, if the variable is absent, it should display something else. I've tried !empty, isset, even $num_rows =1, but the content in the else doesn't display. Here's the code: Code: [Select] <? $result = mysql_query( "SELECT * FROM codeWords WHERE Mycode = '$MyCode' " ) or die("SELECT Error: ".mysql_error()); $num_rows = mysql_num_rows($result); while ($row = mysql_fetch_array($result)) { extract($row); if (!empty( $MyCode )) { echo "here is the code word"; } else { echo "you need to go back and get the code"; } $row_count++; } mysql_close; ?> When the code variable is correct the "here is the code word" displays. when the code variable is incorrect, what should appear as "you need to go back and get the code" is just blank. Any thoughts on how to structure the query in such a way that eh incorrect text appears? Thanks, im not new enough for this to take so long to figure out, but for some reason $rs = mysql_query("SELECT * FROM cl"); returns the last record instead of all the records in the table. anyone know why it wouldnt take everything? hi i am trying to use a query like this SELECT * FROM `fares` WHERE `KMS` >=201 LIMIT 0 , 30 which pops up a databse list of 30 rows which looks something like the one below KMS SL_AD SL_CH 205 122 120 210 124 120 215 125 120 220 127 121 225 128 122 all i want to do is somehow sort and get only the first row(205 122 120) in my php program/ Thanks in advance Me again.. I'm fetching data from MySQL but each person in people.php is displayed two times instead one.. The issue must be inside models_category_tbl table, because each person have more then one result. Any advice is appreciated. What should I do? I would like to display people something like that: 1 John Doe 1 IT developer, 3 mechanic models/model = person category = occupation/skills MySQL: model_index table +---------+------------+-------------+-------------------+---------------+----------------+ |model_id | model_name | model_title | model_description | model_country | model_slug | +---------+------------+-------------+-------------------+---------------+----------------+ | 1 | John Doe | Johns title | Johns desc | Hungary | john-doe | +---------+------------+-------------+-------------------+---------------+----------------+ models_category_tbl table +-------------+---------------------+---------------------+ | category_id | model_category_name | model_category_slug | +-------------+---------------------+---------------------+ | 1 | it developer | it-developer | | 2 | photographer | photographer | | 3 | mechanic | mechanic | +-------------+---------------------+---------------------+ models_category_ids table +----+----------+-------------+ | id | model_id | category_id | | 1 | 1 | 1 | | 1 | 1 | 3 | +----+----------+-------------+
PHP function to fecth data: public function getAllModels1 () { $sql = "SELECT * FROM models_index JOIN models_category_ids ON models_index.model_id=models_category_ids.model_id JOIN models_category_tbl ON models_category_tbl.category_id=models_category_ids.category_id" ; $stmt = $this->conn->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll((PDO::FETCH_OBJ)); return $result; }
people.php <?php declare(strict_types=1); ob_start(); include "includes/connect.php"; include "includes/functions.php"; $model = new ModelsData(); $models = $model->getAllModels1(); foreach ($models as $model) { ?> <a href="model.php?id=<?= $model->model_id ?>"><img src="<?= $model->model_img_path ?><?=$model->model_img_name ?>"></a> <?php echo $model->model_id . " " . $model->model_name . " " . $model->category_id . " " . $model->model_category_name ; ?> <h3><?= $model->model_name ?></h3> <?php } ?>
Output: 1 John Doe 1 IT developer 1 John Doe 3 mechanic
Hi, I am trying to verify if the given url exists or not, by using file_exists() function. It always returns 'FALSE' , according to my understanding it happening because the file to be checked on the given url is located in safe mode. Could anyone please suggest as to how this could be overcome, by similar function or by using alternative method. Regards Abhishek Madhani <?php if('00' == '000') echo 'weired'; ?> Please consider the code above. Strangely it echoes the word "weired". Also if('00' == '000000000000000') returns true and so forth. What is really going on? Thanks in advance By the way the php version is 5.3.9 on wampserver 2.2 When you do a search at (right sidebar): http://ourneighborhooddirectory.org the $_GET result is always blank. The site is hosted at Bluehost and this is a Wordpress site. You can see the result of the query because I write them out. Here is the form code: Code: [Select] <form method="get" action="http://ourneighborhooddirectory.org/search-results/" > Search the Directory: <input type="text" name="srch"/> <input type="submit" value="Search" /> </form> Here is the PHP code on the action page: <?php $srchVal = $_GET["srch"]; echo $_GET['srch']; echo "------------ ". $srchVal." ------------"; print_r($_GET); echo $_SERVER['QUERY_STRING']; ?> Any ideas on why $_POST and $_GET always return blank? Thanks for the help... Hi, I am trying to expand my very limited PHP by building a CMS. It's all new to me so please be courteous with your replies ? I little history of my problem. I am running xampp on a windows 10 running php 5.6.38
I have taken two video courses and on each I got stumped halfway through with a very similar problem. I am copying everything word for word and even downloaded the course file on the first one and still got the same problem so I'm wondering if there is something wrong with my setup - config?.
The problem.
This is the code: <?php error_reporting(E_ALL); ini_set('display_errors',1); include("includes/database.php"); include("includes/header.php"); if ($connection) { echo "Great, we're connected to the database<br><br>"; }else { echo "Bummer, it didn't connect to the database!"; } $query = "SELECT * FROM categories"; $result_set = mysqli_query($connection, $query) ; if (!$result_set) { die("Query Failed".msqli_error($connection)); } while ($row = mysqli_fetch_array($result_set)) { echo $row['cat_title']; } var_dump($result_set); ?>
Great, we're connected to the database object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(2) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }
Question:
Thanks for your help. Hi i am running a wamp server on windows 7 i am trying to php exec command to run a command but the output returns 1 does this mean the cmd failed to execute ? Code: [Select] $openssl_cmd = "($OPENSSL mime -sign -signer $MY_CERT_FILE -inkey $MY_KEY_FILE ". "-outform der -nodetach -binary <<_EOF_\n$data\n_EOF_\n) | " . "$OPENSSL smime"." -encrypt -des3 -binary -outform pem $PAYPAL_CERT_FILE"; Hi again everyone! I'm sitting here, developing the same site - on my localhost the site is working great! Then when I upload it, to another testserver (production for developers), I get following: Notice: Undefined variable: db in C:\xampp\htdocs\lucas\sidebar.php on line 3 Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\lucas\inc\functions.php on line 13 I'm wondering why I get it, because if I do a try / catch on the db connection, and do a select * on the content table, it returns results, the paths should be almost the same.. On localhost I run: localhost/lucas/, on the prod-server I run liveip/lucas/ Can any spot the problem? I'm not a big xampp dude myself, I've attached an dump of my PHP info also, just rename the .php to .html Thank you a lot! Lucas R / Zerpex Hi All, I am pretty new to PHP ( I am a C Programmer). I am trying a simple shopping cart website for my friend. IN one of the pages, I am submitting some info, which is actually not showing up in my server side php. Below is the HTML code to submit. <a href="cart.php?action=add&p=5&size=small"> <img src="images/add_to_cart.gif"/> </a> I tried to check the values of the parameters using $action = $_GET['action'] ; Action stored empty. Then I checked the whole array print_r($_GET) which returned Array( ) So, for some reason the value is always empty. I tried the declaration GLOBAL $_GET, but no use. Any help/directions - much appreciated! I am trying to unserialize this string: a:3:{s:3:\"zip\";s:5:\"55068\";s:4:\"city\";s:9:\"Rosemount\";s:5:\"state\";s:2:\"MN\";} but I unserialize is returning false. Why is it doing that? $info = unserialize($_COOKIE['zipcode']); var_dump($info); |