PHP - Basic Query Problem
I have a table 'combo' with two fields, id and char.
I want to populate a table, with the following query which fails.
$sql = mysql_query("SELECT id,char FROM combo ORDER BY id DESC LIMIT 28");
I a bit rusty on sql but can't see what is wrong with this.
Can anyone help please.
Similar TutorialsOkay, So I'm trying to write a query that will update a certain value in my database, but I want it to update only when 3 conditions are met. Basically, I want to update a column of ints in my DB (mysql) from 0 to a 1, if these criteria are met. 0 and 1 are the only values that are valid in this particular column. dbquery("UPDATE times SET approval = '1' WHERE id = '$id' AND time <= '$currentTime' AND project = '$project'"); that is my query. Now, I'm not even sure if THIS part works. It is however, slightly more complicated in that this query is actually embedded in a foreach loop: $result = $_REQUEST["projects"]; $delimiter - ","; $projectArray = explode($delimiter, $result); foreach($projectArray as $project){ dbquery("UPDATE times SET approval = '1' WHERE id = '$id' AND time <= '$currentTime' AND project = '$project'"); } If you can make any sense of that, and if you can point out any glaringly obvious mistakes here, I would appreciate it. If you need any other information let me know. Thank you all for your help. Ok, I know this is a really simple one, but I haven't done any coding for a couple of month and i'm a bit rusty. I'm trying to add a field from a MySQL query to a Google calendar add button link so I can create a dynamic 'add to calendar' button from my gig list. So on the gig list page that echo's all the dates from the database, I want to add the venue name to the 'text' section of the google URL - Code: [Select] <a href=\"http://www.google.com/calendar/event?action=TEMPLATE&text=VENUE HERE&dates=20060101T000000Z/20060101T000000Z&details=gig%20text&location=gig%20town&trp=false&sprop=www.the-guards.co.uk&sprop=name:The%20Guards\" target=\"_blank\"><img src=\"http://www.google.com/calendar/images/ext/gc_button6.gif\" border=0></a> if I echo the field like so - Code: [Select] echo"{$row['gl_venue']}"; I get the correct result - I just can remember how to embed the php within the html, everything i've tried has failed so far. Once i've got this sorted i can go ahead and add the date, location fields etc. Hello there. Well this problem maybe basic but is kind of annoying me. I havea solution to it which I wil lexplain. I have created a .php document which has html codes as well. It is a form that provides validation, for arguments sake lets say it is a form that validates if a user has entered their username and password, if not it will produce an error. The problem I am having is I have embedded the code inside the echo " code is here "; Now I have looked at escape characters and tried afew things, yet it still produces the same error on the same line. Example echo " !!!!!!!<form action='book.php' method='post' onsubmit="return checkForm('flogin');">!!!!!! The reset of the form is here ..... ""; echo The area highlighted in exclamation marks is where the area occurs, due to the quotes. Any suggestions. Thanks. Hi, I'm very new to PHP so I hope this is not frustrating. I'm having trouble understanding how "isset" works. I'm trying to get it so that when I click a button, it returns two variables(in an array). This works, but I tried to use "Isset" to detect if a array was created, then alter the variable. It's not working... I'm not too sure where I'm going wrong, could anyone give me some advice? Thanks
<?php if(isset($_POST['name'])) { $userAnswer = $_POST['name']; if(isset($_SESSION['test'])){ $_SESSION['test'][$userAnswer]['var1']++; $test = $_SESSION['test'][$userAnswer]; foreach ($test as $key){ echo json_encode($key); }; } else { $test = $_SESSION['test'][$userAnswer]=array("var1" => 1, "var2" => 2); foreach ($test as $key){ echo json_encode($key); }; } } ?> $(document).ready(function(){ $("button").click(function(){ var test = "here"; $.ajax({ url: 'info.php', type: "POST", dataType:'json', data: ({name: test}), success: function(data){ $("#click").fadeOut(function() { $(this).append(data); }).fadeIn(); } }); }); }); http://i.imgur.com/6olCy.png The picture (in the imgur link) pretty much describes what I'm trying to do. As it stands, it would take too long to simply do everything manually with html and css (both of which I'm pretty good at I think), moving all img src and dates from one div tag to another whenever I wanna add something. So yeah, what's the most efficient way to simply submit a picture, and all my other pictures will move? I know some of you guys will say to just learn php, but quite frankly, I don't have the time... and all the php books are ~1000pgs long which seems really daunting. I know, my problem's pretty basic and it'll probably be covered in the first 100pages or so, but still, like I said I don't have that much time for the moment. When I do, however, can you guys recommend me the quickest guide/video series/book to learn practical php? Not the most comprehensive, as the stuff I'm going to be doing's not that complex. One last thing, I've been hearing about CakePHP; what is it? I read that it's an "open source web application framework", but I still have little clue. I'm currently using dreamweaver for all my web stuff (no I do not use the WYSIWYG feature, I just grown accustom to using it). Should I "learn cakephp"? Please answer any one of those questions (particularly the first one). Many thanks and I'll be sure to reply! Ok first of all I want to apologize for asking what I'm about to ask. I understand it should be some stupidly easy to find mistake but considering the fact I have to send the code tomorrow leaves me with little to no time to try and find the solution so I'll be asking here and hope that by the time I wake up tomorrow an answer will be here and I'll be able to resume scripting. So my problem is that I just can't find how to make php commands run at all. I tried just randomly inserting them into my html code then found out it needs a separate file. So I created the basic.php with this code: <html> <body> <?php echo "Hello World"; ?> <div>adgaeganerhaohn</div> </body> </html> tried opening it through a form in my html code but it wouldn't print the Hello world line, then I tried running it straight into firefox but I only got adgaeganerhaohn again (without that line I get a white page) So I need to know what am I doing wrong? I have php5.301 installed with default options (no server). thanks for your help beforehand, once I get a bit more into php I'll be coming back with serious questions but for now I really need someone to tell me how php is supposed to work and what I'm doing wrong Hi, I have just started using PHP again after a long break and am very rusty. I am having a few problems with some basic things: I have defined a function to connect to my MySQL database and it doesn't work. My $hostname, $username, $password variables are stored in a separate php file which I am including first and the standard mysql_connect function works ok, but I wanted to put it inside another user defined function called "condb" to make things quicker later. my function looks like this: function condb(){ mysql_connect($hostname, $username, $password) OR DIE ('Unable to connect to database! Please try again later.'); } and I am trying to run it like this: condb(); Unfortunately it is not working. Probably a basic error but any help appreciated. Thanks im joining a table onto the query to show how many likes a post has but it is displaying the post x amount of times depending on how many likes there are. I think its something to do with the way im joining onto the pid of the post table: $post_query = $link->query("SELECT p.*, u.*, l.* FROM ".TBL_PREFIX."posts as p JOIN ".TBL_PREFIX."users as u ON (u.u_username = p.p_poster) LEFT JOIN ".TBL_PREFIX."post_likes as l ON (l.l_pid = p.p_pid) WHERE p_tid = '$tid' ORDER BY p.p_time_posted ASC")or die(print_link_error()); $likes = array(); while($post_info = $post_query->fetch()) { // SOF LIKE LIST if (count($post_info['l_pid']) >= 1) { $likes[] = $post_info['l_username']; $amount_likes = count($likes); $slice = array_slice($likes, 0, 4, true); $remain = array_slice($likes, 4, $amount_likes, true); $remain_num = count($remain); $like_list = ''; if($amount_likes > 4) { for($i=0; $i<=3; $i++) { $like_list .= profile_link($likes[$i]).', '; } $like_list .= 'and '.$remain_num.' other like this'; } elseif($amount_likes <= 3) { for($i=0; $i<3; $i++) { $like_list .= profile_link($likes[$i]).' '; } $like_list .= 'like this'; } } the likes are stored in their own database table seperate from the posts any advice? Im just trying to get this query to retrieve one peice of data about a user if they are logged in however instead of producing any sort of mysql errors or anything it just stops the page from loading properly. Can anyone help solve this? here is my query code: $query = "SELECT credits FROM users WHERE username = $session->username"; $res = mysql_query($query) or die (mysql_error()); $row = mysql_fetch_assoc($res); $ans = $res['credits']; Hello everybody. I have a query-question... I have a form that writes data into the database. BUT... I want to verify that data if it allready exists. There are 3 fields that needs to be checked: 1. Type; <- if yes, check 2. 2. Coordinates1 (+ or - 2 from existing record) <- if yes check 3. If no: insert record and echo: "Mine posted succesfully in the database!" 3. Coordinates2 (+ or - 2 from existing record) <- if yes echo: "There is allready a mine of this type in the database on that location!". If no: insert record and echo: "Mine posted succesfully in the database!" (One coordinate uses this format: xxxxxx,yyy (Where xxxxxx is coordinates1 and yyy is coordinates2). A comparison for 1 variable I can find, but not for three and also not how to incorporate the + or - 2. Any help is greatly appreciated. Christiaan code I have so far: Code: [Select] <? include 'mining_cfg.php'; // html collect variable $type = $_POST['type']; $level = $_POST['level']; $coordinates1 = $_POST['coordinates1']; $coordinates2 = $_POST['coordinates2']; if ($type == ".$type") AND if ($coordinates1 == ".$coordinates1") AND if ($coordinates2 == ".$coordinates2"){ echo "Mine posted succesfully!"; } else { echo "There is allready a mine of this type in the database on that location!"; } // sql insert into database $sql ="INSERT INTO mining(type, level, coordinates1, coordinates2) VALUES ('".$type."', '".$level."', '".$coordinates1."', '".$coordinates2."')"; //uitvoeren van de query : if (!($temp = mysql_query($sql,$connection))) showerror(); header ( "Location: http://swr.infiniteserve.com/swr-site/test/post_mining.php" ); ?> I have a running total that I am pulling from a database that I need to sort by year. I have tried adding WHERE YEAR(datepicker) = '2015' but keep getting an error. Here is the original query that works fine when I run it. Just don't need all the records.
SELECT id, datepicker, startmiles, endmiles, O.totalMiles, car, section, (select sum(totalMiles) FROM vehicle WHERE id <= O.id) 'runningTotal' FROM vehicle O ORDER BY datepicker DESC Hi Im just working on a wordpress pugin at the moment and Iw as trying out this piece of code $mylink = $wpdb->get_row("SELECT * FROM $wpdb->testtable WHERE id = 1"); echo $mylink->id ; there is data in the table there is an id equal to one but it not coming up with anything I tried it with anouther pable and it works fine I can for example $mylink = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE id = 1"); echo $mylink->post_content ; That comes up fine with the data But the first one I dont know why its not working can anyone help Thanks James When I use this query $sql1="select * from fagudtryk_muskler where latinsk like \"$trimmed%\" order by latinsk UNION ALL select * from fagudtryk_knogler where latinsk like \"$trimmed%\" order by latinsk"; //-run the query against the mysql query function $result=mysql_query($sql1); //-count results $numrows=mysql_num_rows($result); I get this error Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given How can I fix this? Hi all, Could someone have a look at the stuff below. I am able to show the $_POST variables, but from the moment i use mysqli_real_escape_string it goes wrong ( as in the variable seems empty) // mysqli connection file is above here // $clean_name = mysqli_real_escape_string(trim($_POST['name'])); $clean_email = mysqli_real_escape_string(trim($_POST['email'])); echo 'clean_name = '.$clean_name; //outputs noting echo 'clean_email = '.$clean_email; //outputs noting $query_control = "SELECT email FROM my_table WHERE email = $clean_email"; $result = mysqli_query($dbc, $query_control)or die(mysqli_error($con)); // <------- spotted: should have been $dbc //check for duplicates $number = mysqli_num_rows($result); echo $number; if($number === 0){ $output = 'valid'; }else{ $errormessage .= 'duplicate entry'; } } any help would be welcome edit- Oh i spotted some stuff (after changing it becamce: 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 '' at line 1) But not able to fix it really. <form method="post" > <fieldset> <table> <tr> <td>naam van het gerecht:</td> <td><input type="text" name="nieuwgerecht" /></td> </tr> <tr> <td></td> <td></td> <td>ingredient 1:<input type="text" name="ingredient1" /></td> </tr> <tr> <td></td> <td></td> <td>ingredient 2:<input type="text" name="ingredient2" /></td> </tr> <tr> <td></td> <td></td> <td>ingredient 3:<input type="text" name="ingredient3" /></td> </tr> <tr> <td></td> <td></td> <td>ingredient 4:<input type="text" name="ingredient4" /></td> </tr> <tr> <td></td> <td></td> <td>ingredient 5:<input type="text" name="ingredient5" /></td> </tr> </table> <input type="submit" name="cmdvoegtoe" value="voeg toe" /> </fieldset> </form> <?php if(isset($_POST['cmdvoegtoe'])) { if (empty($_POST['ingredient1'])) { echo "<script>alert(\"ingredient nummer 1 moet zeker ingevuld zijn!\");</script>"; } else { $query="INSERT INTO tblgerecht VALUES(NULL, '$_POST[nieuwgerecht]')"; mysql_query($query, $connectie) or die('Error, insert query failed'); } } ?> Parse error: parse error in ... on line 118 problem with mysql query can someone help me with it Having some odd issues with a simple query :S. The first problem is that i have selected all records but it miss's out records in a pattern (the first, fifth, ninth and so on). The second issue is i am trying to select by ascending order but it's just not doing it. The QUERY Code: [Select] $comments = "SELECT * FROM $commentsTable LIMIT $start, $per_page "; $commentResults = mysql_query($comments); $commentRows = mysql_fetch_array($commentResults); The while loop displaying the records Code: [Select] <?php while($commentRows = mysql_fetch_array($commentResults)){?> <div id="comments"> <div id="CommentWrapper"> <div id="comment"> <div id="UserName"><? echo $commentRows['id']." "; ?><? echo $commentRows['name'];?></div> <div id="UserComment"><? echo $commentRows['comment'];?></div> <div id="UserEmail"><a href="<? echo $commentRows['email'];?>"><? echo $commentRows['email'];?></a></div> <div id="PostDateTime"><i>Posted: </i><? echo $commentRows['datatime'];?></div> </div> </div> </div> <?php } mysql_close(); ?> http://dvplus.webuda.com/oophptest/database.query.php <<< the project I tired something earlier, with no luck... so I'm taking a few steps back. I've been working this one complicated page for days... Here's what used to work: Code: [Select] //the current URL $page_name = $_SERVER["REQUEST_URI"]; //remove any old limit from query before requesting a new limit $tmp = array(); foreach ($_GET as $fld => $val) if ($fld != 'limit') $tmp[] = $fld . '=' . $val; $page_name = $_SERVER['SCRIPT_NAME'] . '?' . implode('&',$tmp); //sets the number of records to be displayed on the page <form> <select name="link"> <option value="<? echo "$page_name" ?>&limit=25">25 records per page</option> <option value="<? echo "$page_name" ?>&limit=50">50 records per page</option> <option value="<? echo "$page_name" ?>&limit=100">100 records per page</option> </select> </form> This grabs the current URL, and clears the &limit= string before a new limit is introduced to avoid: www.example.com/test.php?sub=math[]&sub[]=science&limit=25&limit=50&limit=100 The problem, is that my URL is already querying an array: www.example.com/test.php?sub[]=math&sub[]=science So now, when I apply the form above to this URL set the new limit to 50 records per page the URL actually becomes: www.example.com/test.php?sub=Array&limit=50 Warning: Invalid argument supplied for foreach() ... on line 147 I guess I've broken the array? Code: [Select] //line 147 echo "subject:<BR />"; if(!empty($_GET['sub'])) foreach($_GET['sub'] as $sub) { echo "$sub <BR />\n"; } ~Wayne Hi there I am using a query to find what user made a comment. The problem I am having is I get the wrong user from the query. Everything seems fine in the database and I just can't seem to track the problem $query = "SELECT * FROM messages"; $result=mysql_query($query) or die("Invalid Query : ".mysql_error()); while ($row = mysql_fetch_assoc($result)){ $pid=($row["pid"]); $id=($row["id"]); $query = "SELECT * FROM users WHERE id='$pid'"; $rresult=mysql_query($query) or die("Invalid Query : ".mysql_error()); while ($row = mysql_fetch_assoc($rresult)){ $user=($row["user"]); } } The query is working in reverse I.E when I search for what messages are from the user. Can anyone see my problem. Hey everyone, i keep having this error: Code: [Select] Could not connect: 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 'desc,mg,pg) VALUES ('1','0','Snoozy','teata','1','White','atata','atatata'' at line 1 This was just a example as I've been lately just trying and trying with no luck, The script part is: $appquery = mysql_query("INSERT INTO applications (aid,cid,name,charactername,gender,race,desc,mg,pg) VALUES ('".$_COOKIE['ID']."','$insertid','".$_COOKIE['Username']."','$name','$sex','$race','$description','$mg','$pg')") or die('Could not connect: ' . mysql_error()); |