PHP - Setting A Variable Equal To The Result Of A Mysql Select Statement
When I run
'select 1700-price as blah from goldclose as t2 order by dayid desc limit 1' by itself in mysql I get a numerical result: one row, one column. In my php script, the 1700 is actually a variable. so here it is $changequery = sprintf("select $goldprice-price as change from goldclose order by dayid desc limit 1"); $change = mysql_query(changequery); while ($row = mysql_fetch_array($change)) { printf("$row[0]"); } mysql_free_result($changeresult); I get the following error, Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/root/fuzzy/htmlmain4.php on line 99 Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in <b>/root/fuzzy/htmlmain4.php on line 103 Not sure why? All i want is to get the result of that select statement into a variable such as $change Similar TutorialsHey guys, Currently Im using: $row = mysql_fetch_array($result) or die(mysql_error()); echo $row['user_family']. " - ". $row['user_registered']; $row['user_family'] = $fam; $_SESSION['family'] = $fam; to take data from a mysql table & set it as SESSION family. However, I cant seem to get this to set. The information IS being taken from mysql because its being echo'd earlier up in the code, but its just not passing to the session. Any ideas? Hi All, I'm working on PHP scripts to interact with a web hosted MySQL DB for an Android Application. Simply what I am trying to do is in the PHP script is run a SELECT statement which will return the value of a column, UserType, and compare the result of this to a string, which will then execute code depending on it's value. This user type can only be either 'student' or 'lecturer'. Any help with this would be much appreciated. <?php require "init.php"; $user = $_GET["userID"]; #Selects column account type where the idNum equals $user which is passed from my app. $sql1 = "select accountType from user_info where idNum = '$user'"; $result1 = mysqli_query($con,$sql1); $row1 = mysqli_fetch_assoc($result1); #This is where I am stuck. Simply, I am trying to run the code in the loop where the result of $sql1 equals 'Student'. The else will run if it is not #student and therefore is 'Lecturer'. I'm also not sure if my code inside the IF is fully correct either as it's not running that far. if($row1['accountType'] == 'Student') { $sql2 = "select courseCode from user_info where idNum = '$user'"; $result2 = mysqli_query($con,$sql2); $row2 = mysqli_fetch_assoc($result2); $sql3 = "select * from module_details where classListCourseCode = '".$row2['courseCode']."'"; $result3 = mysqli_query($con,$sql3); $response = array(); while($row = mysqli_fetch_array($result3)) { array_push($response,array("moduleID"=>$row[0],"lecturerID"=>$row[1],"moduleName"=>$row[2],"classListCourseCode"=>$row[3])); } echo json_encode(array("server_response"=>$response)); } Thanks in advance. What's the correct syntax for having a variable in a select statement? Here's an example of what I'm trying to do (after I'm already connected to the database). Code: [Select] $username = "thomas"; $query = mysql_query( "SELECT * from users WHERE username = $thomas" ); if( mysql_num_rows( $query ) > 0 ) { bla bla... It works if I don't put the WHERE part in, but I get an error if I use it, so I'm assuming I have the wrong syntax for using a variable in the select statement. I am trying to update a mysql table called AvItems with the value 'Torso' in the Equip "section?" I have been through the forums and cannot see anything to match. I dont mind if the page looses the onsubmit() and has a button instead. Though I would like to update the database and link back to the same page: There is a display that shows the item that is currently equiped, I have put this in to show it works, or doesn't as the case may be. Hope I got the code /code right this time. many thanks in advance Andy Curtis Code: [Select] create table Items( ItemID integer unsigned auto_increment primary key, ItemName varchar(20) not null, Type varchar(10), UsedOn varchar(10), ); create table AvItems( AvItemID integer unsigned auto_increment primary key, AvID integer unsigned, ItemID integer unsigned, Equip varchar(8)); <?php $username="root"; $password="MyPassword"; $database="MyDataBase"; $AvName = "AndyJCurtis"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $AvAccR = mysql_query( " SELECT AvID FROM AvAcc WHERE AvName = '$AvName' " ); $AvID = mysql_result($AvAccR, 0, 'AvID'); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $Torso = mysql_query(" select ItemName from AvItems, Items where AvItems.itemID = Items.itemID AND AvItems.AvID = '$AvID' AND UsedON = 'Body' "); $TorsoE = mysql_query(" select ItemName from AvItems, Items where AvItems.itemID = Items.itemID AND AvItems.AvID = '$AvID' And UsedON = 'Body' AND Equip = 'Body' "); if(mysql_num_rows($TorsoE) != 0) { $TorsoItem = mysql_result($TorsoE ,0,"ItemName"); //mysql_close(); ?> <title></title> <head></head> <body> <form action="http://localhost/CI/Equip2.php" method="post"> <table border=1> <tbody> <tr> <td>Torso<BR> <?PHP echo "$TorsoItem <BR>"; ?> <select name="Torso" onchange="submit();" value =" Update"> <?PHP while($TorsoRow = mysql_fetch_array($Torso)) { echo "<option value=\"".$TorsoRow['ItemName']."\">".$TorsoRow['ItemName']."\n </option>"; } ?> </select> </td> </tr> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// <?php if($_POST['Torso'] == 'Update') { mysql_query("update AvItems set Equip = '' where Equip='Torso'") or die("cant update unequip"); mysql_query("update AvItems set Equip = 'Torso' where ItemID='{$_POST['ItemName']}'") or die("cant update equip"); } ?> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// </tbody> </table> </.form> </body> </html> Hey guys. Me again... Essentially what i am doing is pulling data from a MySQL database about the number of thumbnails on a page. The user can then change this using a <select> dropdown menu. How ever, i want the <select> to default to the amount already specified by the Database. I know i can do this by inserting a Selected attribute to one of the <options> but what is the best way of doing this? Heres my code.. $NumberOfThumbnails = mysql_result($data, 0,"NumberOfThumbnails"); <select name="numberofthumbnails"> <option value="0">None</option> <option value="2">2</option> <option value="4">4</option> <option value="6">6</option> <option value="8">8</option> <option value="10">10</option> <option value="12">12</option> <option value="14">14</option> <option value="16">16</option> <option value="18">18</option> <option value="20">20</option> <option value="22">22</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> </select> Thanks - Danny Hello Everyone, I have a quick question for you all, I think its fairly simple... I have created a database and I am using PHP to grab the data: $usera = $_SESSION['username']; $query2 = "SELECT * FROM tracker WHERE id = '$usera', hidden = yes"; mysql_query($query2) or die('Error, query failed : ' . mysql_error()); This hopefully will return multiple rows which look like this in the database. id username date reps hidden 1 supremebeing 2011-01-02 30 yes 4 supremebeing 2011-04-02 46 yes How would i turn each result into a variable eg: $date1 = 2011-01-02; $date2 = 2011-04-02; $reps1 = 30; $reps2 = 46; I think i have explained that well enough for you to understand, please reply if not though and i will provide more information. Thanks in Advance Hi, I have come up with the following code, I need it to get the details of several scattered products and echo the results, the trick is I don't want it to echo the results one after the other... I want to have the products scattered between unique text on the page but don't want to run the query several times for performance reasons. E.g.- PAGE to look like this: $Product_1 unique text/images $Product_2 $Product_3 unique text/images $Product_4 Current Code: Code: [Select] <? $result = mysql_query("SELECT * FROM products where Product_ID IN (475, 465, 234, 567, 845)"); while($row = mysql_fetch_array($result)) { $x = "1"; while ($x<=3) { echo $x; $Product = "Product_"; $Product = $Product.$x; echo $Product; $Product = $row['Product_ID']; echo $Product; $x++; echo $x; } } At the moment it returns the following results: Quote 1 Product_1 465 2 2 Product_2 465 3 3 Product_3 465 4 1 Product_1 475 2 2 Product_2 475 3 3 Product_3 475 4 A few problems... In Blue... it duplicates for product 465 In Red... It repeats again for 475 Also.... it starts with 465, but I want it to go in order as how it appears - $result = mysql_query("SELECT * FROM products where Product_ID IN (475, 465, 234, 567, 845)"); so should start with 475 I want to get the following result: Quote 1 Product_1 475 2 2 Product_2 465 3 3 Product_3 234 4 4 Product_4 567 4 (and so on.....) If anyone could provide me assistance with my troubled 'while loop' statement that would be much appreciated! Hi I'm having a problem getting a query to work. I have a simple form with user input for start and end date with format: 2009-03-19 (todays date): $Startdate = $_POST['date']; This works well when something is entered into the form, and afterwards using my query: SELECT COUNT(*) as total FROM mydb WHERE Date BETWEEN '$Startdate' AND '$EndDate' ........ Problem is if user submits the form without entering anything in the date input fields, which makes sense. I want to check if inputs has been made, and if not set af default date, but can't make it work: if (isset($_POST['date']) && $_POST['date'] !='') { $Startdate = $_POST['date'];} else { $Startdate = '1980-01-01';} How can I set $Startdate to something that can be used in the query as below doesn't work? Hi Guys I don't know if this is possible but can someone point me in the right direction. I have a php function which takes two inputs and returns an output. for simplicity's sake let's say it's an addition function. What I want to do is use a mysql select statement to show all the rows from a database where field1 and field2 equal '3'. Here's the sort of thing I mean. function addNumbers($one,$two) { return $one + $two; } mysql_query("SELECT * FROM table WHERE 'addNumbers(field1,field2)' = '3'"); What I actually want to do is a lot more complex than this but I am trying to understand how to make the syntax work in simple terms first. Can anybody help? Many Thanks Dan Is it possible to have a loop inside a mysql select statement? If yes, can someone please give me an idea. I have found postings close, but not close enough to find my error. I am looking up data from a MySql table and putting it in a dropdown box on a form. I can select the item, but apparently not really. I am not able to echo it, or post it to a record. I'm sure I am missing something simple, but... Code attached if anyone can show me the errors of my ways. Thank you. Please take a look at the following code (this would work if the statement was SELECT instead of UPDATE) But I need to grab the following: Code: [Select] $result = mysql_query("UPDATE `AUCTIONS_MAIN` SET Current_Price = Current_Price + '$increase_price', Last_Bidder_Time = '$last_bidder_time' WHERE PId = '$id'"); //grab the new updated current price to save on different bidding history DB while($row=mysql_fetch_array($result)) { $current_price = $row['Current_Price']; } As you can see, I need to give the new Current_Price value to the $current_price variable. Obviously the while function is incorrect, I would really appreciate any ideas and/or suggestions on how I can grab this value. PS: I can't make a new query call with a select statement in order to know the new Current_Price. I need the direct updated value coming from the Update Statement, is this possible? Thanks a lot in advance!! Hi, i am trying to write a part of script for google shopping export, and all i want is that in this line, everything that is 0 or smaller, the line "pre-order" krijgt and everything that is 1 or greater the line "in stock" This is what i've got now, but it doesn't work I removed the rest of the long code. Code: [Select] $patterns = array(); $patterns[0] = '=<0'; $patterns[1] = '=>1'; $replacements = array(); $replacements[0] = 'vooraf bestellen'; $replacements[1] = 'op voorraad'; if(OPTIONS_TONEN_FEED_AVAILABILITY == 1) $output .= "~" . preg_replace($patterns, $replacements, $row->quantity); Hi All, I have a select statement $stmt = $conn -> prepare(' SELECT u.user_firstname, u.user_lastname, so.staff_id, r.role_name FROM ssm_staff_order so INNER JOIN ssm_user u on so.staff_id = u.user_id INNER JOIN ssm_role r on u.user_role_id = r.role_id WHERE job_id = ? '); $stmt -> bind_param('i', $jid); $stmt -> execute(); $stmt -> bind_result($fn, $ln, $id, $role); The result looks like this +----------------+---------------+----------+-----------+ | user_firstname | user_lastname | staff_id | role_name | +----------------+---------------+----------+-----------+ | FName | LName | 8 | Chef | | jon | smith | 15 | Manager | | Chelsea | Hockley | 2 | Manager | +----------------+---------------+----------+-----------+ I am wanting to build an array so that i can foreach the result with the people under the header of their role. I know this is not difficult but i am going around and around with it and clearly missing the key part. As always your help is appreciated. HI All,
I am creating a dropdown box on an edit user form. The box is populated from a table ssm_role and relates to the user which is another table. I am wanting the value of the dropdown to be set as whatever the loaded user has in their database table. See the images below. $sql = "SELECT * FROM ssm_role ORDER BY role_name ASC"; if($result = mysqli_query($conn, $sql)){ if (mysqli_num_rows($result)>0){ while ($row = mysqli_fetch_array($result)){ echo "<option value='".$row['role_id']."'>".$row['role_name']."</option>"; } } } This is what i am currently doing to make the dropdown but obviously this isnt even attempting to set the default. Your help is always appreciated
Okay so, I'm adding a reply feature to my text-board, and I'm wondering how I can select a single result from the DB I've tried this $board = mysql_real_escape_string($_GET['board']); $thread = mysql_real_escape_string($_GET['thread']); $result = mysql_query("SELECT subject, name, id, timestamp, body FROM $board LIMIT $thread,$thread"); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { printf("<div id='html1' style='background-color:#F0E0D6;border:solid 1px #800000;font-family:arial; font-size: 12px; padding:2px; margin:2px;'><font color='#CC1105'>%s </font><font color='#117743'>%s</font> <font color='#800000'>Post # %s - %s<br />%s<br /></div>", $row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6]); mysql_free_result($result); } ?> Kinda works but if I pu ?thread=56 then it would show thread 57, which in this example does not exist. Hi there
I am new to this forum and not much of a php expert. I hope you can assist me.
My problem is as follows:
I have a column consisting of several values (in this case: sceience, art, humanistics and music)
I would like to use the following mysqli query in order to count the appearances of each value in this column:
$query = "SELECT SUM (IF(department = 'science', 1, 0)) AS science, SUM(IF(department = 'art', 1, 0))-> AS art FROM new2"
I have tried lots of ways to loop the results but couldn't get the it working.
Can anyone suggest any idea?
Thanks
Hanan
$result = mysql_query("SELECT * FROM `friend_system` WHERE `friend_system_accepter` = '$myusername' "); echo mysql_num_rows($result);This displays the total rows in the table. $result = mysql_query("SELECT COUNT(friend_system_accepter) FROM `friend_system` WHERE `friend_system_accepter` = '$myusername' "); echo mysql_num_rows($result);This displays '1', which is incorrect. I want to echo out the number of rows where 'friend_system_accepter' = $myusername Thanks Hello.
I'm having some issues with a select statement containing ABS().
Here's the select query:
"SELECT gadenavn, husnr, ABS(husnr - $husnr) AS husnr_range, postnr,db, shaping, dsl_node, ctr_node, db_ctr_luftlinje, bynavn FROM `TABLE 1`, Post_numre WHERE `postnr`=`postnummer` AND `gadenavn`=`$vejnavn` AND `postnr`=`$postnr` AND `husnr`>0 ORDER BY husnr_range ASC LIMIT 5"Now lets asume my variable "$husnr" is a value of 15. In my table I have the values for coulmn "husnr": 13,12,11,10,9 What really bugs me is the outcome doing af simple "while loop" is returning the values 12,11,10,9. What happened to 13?! printing the "husnr_range" values shows "3,4,5,6". What puzzles is the row missing, as I only get 4 results, with a "LIMIT 5". Can anyone explain why the last resulst isn't included? |