PHP - Not Printing Mysql Select Statement To Html Page?
Similar TutorialsHi 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. 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 The code is posted he http://codepad.org/Fck5s2zz The attached file (delete_user.php) is the same as the code in the link above. The same code is also posted below. [text] What I am trying to do is create a function that will allow me to delete a user from my mysql database. I have an HTML select that outputs the username(s) using the function get_admin_username() (The function is posted below) There is also a HTML form I am using to make this happen. (Also posted below) In other words I would select the user I would like to delete, and click submit which will then delete the user from the database. The problem I am having is I do not know how to pass values from the HTML select, or the form, or even use the submit button value. I am not sure If I need to create a function to achieve this. Basically I don't know what to do this at all for that matter. I thought I might have gotten close but I thought wrong, and failed. I was told (in the php irc) that my method was way off and wrong but was given no help after that. I do have an example to show you of how I tried to accomplish this, but being told that I was wrong I did not feel that it was even worth posting it. I am lost and have been at this for two days now. I am a noobe but I do understand allot (I think). Someone, anyone, please help. Thank You, Ryan [/text] Code: Code: [Select] <?php // Session's Functions require_once("../includes/sessions/session.php"); // Establish A Connection To The Database require_once("../includes/connection/connection.php"); // User Defined Admin Functions require_once("includes/functions/admin_functions.php"); // New User Functions //require_once("includes/users/delete_user.php"); // Confirms If The User Is Logged In confirm_logged_in(); // Document Header include("includes/templates/default/header.php"); ?> <?php // Gets The Admin Username function get_admin_username() { global $connection; $query = "SELECT * FROM administration_users "; $query .= "ORDER BY username"; $admin_user_set = mysql_query($query, $connection); confirm_query($admin_user_set); while ($admin_users = mysql_fetch_array($admin_user_set)) { echo "<option value=\"username" . "\">" . $admin_users['username'] ."\n "; } } ?> <table id="structure"> <tr> <td id="navigation"> <a href="../staff.php">Return To Staff Menu</a> <a href="admin_content.php">Return To Admin Menu</a> <br /> <br /> <ul class="menu"> <li class="pages"><a id="page_menu" href="new_user.php">Add New User</a></li> <li class="pages"><a href="edit_user.php">Edit User</a></li> <li class="pages"><a href="list_users.php">List Users</a></li> <li class="pages"><a href="delete_user.php">Delete User</a></li> </ul> </td> <td id="page"> <h2>Remove User</h2> <br /> <form action="delete_user.php" name="delete_user" method="post"> <table> <tr> <th class="field_name field_padding_user_name">User Name: </th> <td> <select id="select_username" name="username_select"> <?php echo get_admin_username();?> </select> </td> </tr> <tr> <td class="delete_user_submit" colspan="2"><input type="submit" name="submit" value="Delete User" onclick="return confirm('Are You Sure You Want To Delete This User?');"/></td> </tr> </table> </form> </td> </tr> </table> <?php // Document Footer include("includes/templates/default/footer.php"); ?> MOD EDIT: [code] . . . [/code] tags added. I have two tables...one is scheduled_umps and one is games. Both tables have a column 'game_id'. I need to select how many games a person is scheduled for in a given time frame. Here's what I have for the select...and it's not working (You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE scheduled_umps.ump_id = '34' AND games.game_id = scheduled_umps.game_id AN' at line 1) Code: [Select] <?php $today_is_this_date = date('Y-m-d'); $four_months_ago = date('Y-m-d', strtotime('-4 months', strtotime($today_is_this_date))); $four_months_from_now = date('Y-m-d', strtotime('+4 months', strtotime($today_is_this_date))); /// How many games have you worked? //$query = "SELECT COUNT(ump_id) FROM scheduled_umps WHERE `ump_id` = '$_SESSION[ump_id]'"; ///this one selects how many games you've worked NO MATTER WHAT THE DATE $query = "SELECT scheduled_umps.game_id, games.game_id, COUNT(scheduled_umps.ump_id) WHERE scheduled_umps.ump_id = '$_SESSION[ump_id]' AND games.game_id = scheduled_umps.game_id AND games.game_id BETWEEN $four_months_ago AND $four_months_from_now"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "You have been scheduled on <b>". $row['COUNT(ump_id)'] ."</b> dates for the $_SESSION[association_name]."; } ?> [\code] Any ideas where I'm going wrong? Thanks for the help...I HATE multi-table selects! Hi All, I have the following Query: Code: [Select] $query = 'SELECT `BookingID`,`OfferID`, `name`, `OfferCount`,`OfferPrice` FROM `Offers_Finance` JOIN offers_details ON `OfferID` = `o_d_id` WHERE `BookingID` ="' .$bookerid. '"'; if ( !$result = $mysqli->query( $query ) ) { die( $mysqli->error ); } $field = $result->fetch_object(); This will return anywhere between 0 and 50 Rows of Data. For Each Row returned, I want to do the following: Quote echo '<tr>'; echo '<td>' . $field->name.'</td>'; echo '<td></td>'; echo '<td>£' . $field->OfferPrice.'</td>'; echo '<td></td>'; echo '<td>' . $field->OfferCount . '</td>'; echo '<td></td>'; echo '<td>£' . $field->OfferPrice * $field->OfferCount . '</td>'; echo '</td>'; echo '</tr>'; The problem I have is - how do I loop through the results returned from the query to output multiple rows? Sorry if this doesn't make much snese, I'm new to PHP and the whole web development world, but have been landed with finishing a project someone else started! If someone would be kind enough to turn this into a working example, it would help no end as I have about 20 of these things to figure out across the project! $sql=mysql_query("SELECT * FROM `buds` WHERE `level`<='$user_level' UNION SELECT * FROM `buds`, `unlocked_buds` WHERE buds.`id` = unlocked_buds.`bud_id` ORDER BY buds.`id` ASC") or die("A MySQL error has occurred.<br />Your Query: " . $sql . "<br /> Error: (" . mysql_errno() . ") " . mysql_error()); I have been trying to learn about UNION select statements. I ran the query above and got this response: Quote Error: (1222) The used SELECT statements have a different number of columns I think I know what the problem is, but not sure how to fix it. There is two columns, one is "buds" which holds the flowers seeds info. The seconds is "unlocked_buds" which just links the "id" from buds to "user_id" to the user table. Both buds and unlocked_buds are both 8 columns. What do I need to learn? Hey guys, I'm trying to get this working... No errors right now, but I'm not returning any results :/ been messing with it for days. Code: [Select] <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" /> <label for="search_by">Search By</label> <select name="search_by"> <option value"player">Player</option> <option value"city">City</option> <option value"alliance">Alliance</option> <option value"browse">Browse</option> </select> <input type="text" name"search"> <input type="submit" value="search" name="search"> <?php $search_by = $_POST['search_by']; $search = $_POST['search']; echo "<table><tr><td>Player</td><td>city</td><td>alliance</td><td>x</td><td>y</td><td>other</td><td>porters</td><td>conscripts</td><td>Spies</td><td>HBD</td><td>Minos</td><td>LBM</td><td>SSD</td><td>BD</td><td>AT</td><td>Giants</td><td>Mirrors</td><td>Fangs</td><td>ogres</td><td>banshee</td></tr>" ; $dbc = mysqli_connect('xx', 'xx', 'xx', 'xx') or die ('Error connecting to MySQL server'); $sql = "SELECT * FROM players WHERE ('$search_by') LIKE ('$search') "; //problem is here^^?? $result = mysqli_query($dbc,$sql) or die("Error: " .mysqli_error($dbc)); Not sure, any help would be greatly appreciated. I have a Select statement that relies on two session variables to complete the statement. One variable, $egroup, supplies a column name. I cannot make the SELECT work. It works fine if I substitute the real name of one of the columns such as "egroup1", or "egroup6", but that's what the variable $egroup is for. Can anyone tell me where I'm wrong? I guess the answer is in the syntax, but I'm not very good at this and I've been on it for too long. Any help gratefully taken. Code: [Select] <?php $query1 = mysql_query("SELECT * FROM topics WHERE managerId='".$managerId."' AND "$egroup"= 1 ORDER BY title ASC"); ?> So I have a jobs database with the following columns: id, jobtext, jobdate, and id. This is how it looks right now: http://prahan.com/jobs/display.html.php I have another table called author. In the authorid column in need the results of this query, SELECT name FROM author WHERE id = (SELECT authorid FROM job) , to be displayed for each row. I also want to be able to customize the header title for each column. Thanks in advance! Hi everybody i am beginnet with working with sql in php..so i confused.....i made one scripte and i am making the secound and now i am facing a problem....i hope u could help!! i have a dat=abse of my chatroom....this is the code i typed...please have a look at it! Code: [Select] $usern=$_SESSION['etchat_'.$this->_prefix.'username']; $con1 = mysql_connect("localhost","manosir_main","********"); if (!$con1) { die('Could not connect: ' . mysql_error()); } mysql_select_db("manosir_manoshos_main", $con1); $users= mysql_query(" select etchat_user_online_user_sex from db1_etchat_useronline where etchat_user_online_user_name = $usern "); in my database, i ahve a table called db1_etchat_useronline that keeps record of online users in chatroom. as u usee i qant to to get the user sex ( etchat_user_online_user_sex) of th current user!!! as u see i conected to database correctly but now i dont know how to echo the $users . before this i echo my sql results with mysql_fetch_array ..but now i cant.......i got nothing!!!! can someone help me!!!!! This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=322930.0 here is some simple code for getting and displaying fata from a database Code: [Select] $sql="SELECT * FROM messages WHERE m_id = '".$id."'"; $result = mysql_query($sql); <table border='0' cellspacing="4"> while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<th>Message</th>"; echo "</tr>"; echo "<tr>"; echo "<td>" . $row['message'] . "</td>"; echo "</tr>"; } now ive used that while loop to display those results how can i do that again without having a new SQL statement. i cant do 2 while loops because the first one has already got to the end of the amount of rows basically how can i have another while loop displaying those same results again? I have got a set of data for a country for each week of the year, but want to display the data a quarter at a time, using a form which posts a value into LIMIT, which was OK for the 1st quarter, but for subsequent quarters I needed to use LIMIT in the form LIMIT start, rows.
I found by experiment that this had to be the last statement in the query.
when I added the start I got an error:
Parse error: syntax error, unexpected ',' in /homepages/43/d344817611/htdocs/Admin/Visitdata.php on line 10
Here is the SQL part of my code:
$Visit_data="SELECT WeekNo.WNo, WeekNo.WCom, Countries.Country, ctryvisits.CVisits FROM ctryvisits LEFT JOIN Countries ON ctryvisits.country=Countries.CID LEFT JOIN WeekNo ON ctryvisits.WNo=WeekNo.WNo WHERE Countries.Country = '".$_POST["Country"]."' ORDER BY ctryvisits.WNo LIMIT '".$_POST["QUARTER"]."'" - 13, '".$_POST["QUARTER"]."'";Is this the best way of doing what I require or is there an easier way of achieving what I want? This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=327011.0 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 need to convert the following select statement to a pdo->query but have no idea how to get it working: SELECT t.id FROM ( SELECT g.* FROM location AS g WHERE g.start <= 16785408 ORDER BY g.start DESC, g.end DESC LIMIT 1 ) AS t WHERE t.end >= 16785408; Here's the code I'm trying: <?php $php_scripts = '../../php/'; require $php_scripts . 'PDO_Connection_Select.php'; require $php_scripts . 'GetUserIpAddr.php'; function mydloader($l_filename=NULL) { $ip = GetUserIpAddr(); if (!$pdo = PDOConnect("foxclone_data")) { exit; } if( isset( $l_filename ) ) { $ext = pathinfo($l_filename, PATHINFO_EXTENSION); $stmt = $pdo->prepare("INSERT INTO download (address, filename,ip_address) VALUES (?, ?, inet_aton('$ip'))"); $stmt->execute([$ip, $ext]) ; $test = $pdo->prepare("SELECT t.id FROM ( SELECT g.id FROM lookup AS g WHERE g.start <= inet_aton($ip) ORDER BY g.start DESC, g.end DESC ) AS t WHERE t.end >=inet_aton($ip)"); $test ->execute() ; $ref = $test->fetchColumn(); $ref = intval($ref); $stmt = $pdo->prepare("UPDATE download SET ref = '$ref' WHERE address = '$ip'"); $stmt->execute() ; header('Content-Type: octet-stream'); header("Content-Disposition: attachment; filename={$l_filename}"); header('Pragma: no-cache'); header('Expires: 0'); readfile($l_filename); } else { echo "isset failed"; } } mydloader($_GET["f"]); exit; It gives the following error: QuoteFatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.144.181) ORDER BY g.start DESC, g.end DESC ) AS t WHERE t.end >=inet_aton(7' at line 1 in /home/foxclone/test.foxclone.com/download/mydloader.php:19 Stack trace: #0 /home/foxclone/test.foxclone.com/download/mydloader.php(19): PDO->prepare('SELECT t.id FRO...') #1 /home/foxclone/test.foxclone.com/download/mydloader.php(38): mydloader('foxclone40a_amd...') #2 {main} thrown in /home/foxclone/test.foxclone.com/download/mydloader.php on line 19 How do I fix this? //DATABASE CONNECTION VARIABLES $myserver ="localhost"; $myname = "myname"; $mypassword = "mypassword"; $mydb ="mygamedb"; /*SQL CONNECTION*/ // Create connection $conn = new mysqli($myserver, $myname, $mypassword, $mydb); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } else { //variables $username = $_POST["username"]; $password = $_POST["password"]; $ip = $_SERVER['REMOTE_ADDR']; //INSERT USER //prepare and bind $stmt = $conn->prepare("INSERT INTO Players (Username, Password, IP) VALUES (?, ?, ?)"); //bind parameters $stmt->bind_param("sss", $username, $password, $ip); //set parameters and execute $stmt->execute(); //close $stmt->close(); //FETCH ID $resultnews = mysql_query("SELECT * FROM Players WHERE Username ='$username'"); $rownews = mysql_fetch_array($resultnews); $user_id = $rownews["ID"]; }After having suffered an SQL injection into one of my sites, I figured it was time to overhaul it and use prepared statements. I am new to this. I figured out how to an INSERT with an example, but now I need to fetch an ID and cannot get it to work. Any help much obliged. All I need is just one good example. Looked all over the place, but all I get are insert examples, which is NOT what i need. Really need one with a select and fetch. how do i handle single quotes in sql query Code: [Select] " SELECT name from phrase WHERE name='$stitle' ";this returns an error because the name contains single quotes like this: Johnson's. I'm able to use PHP-ODBC to read the tables in an MDB file, as well as the column names on the tables. But when I try to read data from a particular table, it fails (I don't get an error, instead the browser pops up a message asking if I want to save the file "mdbtest.php" which is 0 bytes in size)! Any idea what's wrong? Code: [Select] //THIS WORKS $conn = odbc_connect( 'TestDB', '', '' ); if ( !$conn) exit("Connection Failed: " . $conn); //STILL FINE HERE $sql = 'SELECT * FROM Customers'; //INCLUDING THIS LINE OF CODE CAUSES THE PROBLEM $rs = odbc_exec( $conn, $sql ); |