DavidAM
Staff Alumni-
Posts
1,984 -
Joined
-
Days Won
10
Everything posted by DavidAM
-
Select where every row on a given date is null mysql
DavidAM replied to Drew106's topic in MySQL Help
If you are wanting rows where the COUNT is zero, then you need to use a HAVING clause SELECT COUNT(column1) AS tot, DATE_FORMAT(date, '%Y-%m-%d') AS dt FROM lists WHERE column1 IS NULL GROUP BY dt HAVING COUNT(column1) = 0; -
If you are talking about adding literal values, then yes, you can do that too. You can add literals to any select: INSERT INTO tableName (col1, col2, col3, col4) SELECT someCol1, someCol2, 'Literal Value', 123 FROM tableName2 For every row in tableName2, insert a row into tableName; assigning someCol1 to col1, and someCol2 to col2; and also assign "Literal Value" to col3 and 123 to col4 in every row inserted
-
I'm no expert at rewrite, but you could put another rule BEFORE your existing one: RewriteRule ^shops/([^/\.]+)/([^/\.]+)/?$ ./product_details.php?shopurl=$1&produrl=$2 [L] I'm not sure why you are excluding the period between the slashes.
-
YES! Use == for that while loop as well.
-
Select where every row on a given date is null mysql
DavidAM replied to Drew106's topic in MySQL Help
I'm not sure I understand. You want to count all rows where column1 is NULL or is equal to zero? If so, you can choose one of these: SELECT COUNT(ID) AS tot, DATE_FORMAT(date, '%Y-%m-%d') AS dt FROM lists WHERE column1 IS NULL OR column1 = 0 GROUP BY dt; SELECT COUNT(ID) AS tot, DATE_FORMAT(date, '%Y-%m-%d') AS dt FROM lists WHERE IFNULL(column1, 0) = 0 GROUP BY dt; The first one just checks for either condition. If you are running against a large table, and column1 is indexed, this will probably have better performance than the second one. The second one just considers a null value to be zero and checks if the value is zero. You should get the same results from both queries, it's just a matter of performance and preference. If I missed the boat here, then try explaining a little more and maybe show some sample data. -
Select where every row on a given date is null mysql
DavidAM replied to Drew106's topic in MySQL Help
I'm pretty sure that COUNT ignores NULLs, so in your original query, try counting the primary key or some other column that is sure to have a value. SELECT COUNT(ID) AS tot, DATE_FORMAT(date, '%Y-%m-%d') AS dt FROM lists WHERE column1 IS NULL GROUP BY dt; @bepai: You cannot use 'equals' with NULL. You have to use IS NULL. -
$last = end($array); $beforeLast = prev($array); See the manual for end() and prev()
-
Actually, $GLOBALS is a predefined array that references all variables in global scope. The manual provides more information on it. But basically, if you define a variable (in global scope) you can get to it through this array. $myVar = "This is a globally defined variable"; echo $GLOBALS['myVar']; // will output: This is a globally defined variable
-
The HTML code in my example is INSIDE the ELSE part of the IF statement. It will only be reached when the form is NOT submitted. When you drop out of PHP code (using ?>) the PHP engine just sends everything it finds to the browser. When you jump back into PHP (using <?php), the PHP engine starts interpreting what it finds as PHP code. In the example, we jumped back into PHP just to close the curly-brace for the ELSE, otherwise (I think) PHP will throw a parse error because we opened a curly-brace after the else, and PHP needs to find a closing one.
-
You can do exactly that: <?php // you'll have a submit button named submit, eg. <input type='submit' name='submit' /> if ( isSet ($_POST['submit'] ) ) { $post_field = $_POST['field']; $post_field_two = $_POST['field_two']; } else { // DROP OUT OF PHP FOR RAW HTML ?> <HTML> <HEAD> <!-- ALL OF MY HTML --> <?php // JUMP BACK INTO PHP TO CLOSE THE else OFF }
-
Did you take out the FOREACH? Unless you are adding the location phrase to your where array more than once, you should not get it in the query more than once. Post you code so we can see where the problem is.
-
You could simplify this code and use fewer queries with: $q1 = "UPDATE Events SET HPselected = 'yes' WHERE ID IN ( '{$eID}', '{$eID2}', '{$eID3}')"; $mdb2->exec($q1); $q4 = "UPDATE Events SET HPselected = 'no' WHERE ID NOT IN ( '{$eID}', '{$eID2}', '{$eID3}' )"; $mdb2->exec($q4); You could actually do it in a single query: $q1 = "UPDATE Events SET HPselected = CASE(ID IN ( '{$eID}', '{$eID2}', '{$eID3}'), 'yes' , 'no')"; $mdb2->exec($q1);
-
The problem is with the multiple possible values for location. SELECT * FROM `property` WHERE location = 'town1' AND location = 'town2' AND cat_id = '12' AND status = 'Yes' location cannot be "town1" AND "town2" at the same time. You need to use OR for those, or use IN which would be easier to build: SELECT * FROM `property` WHERE location IN ('town1', 'town2' ) AND cat_id = '12' AND status = 'Yes' The quickest approach to do that would be changing this section of code: if(!empty($_GET['location'])) { $param = preg_replace('/[^á é í ó ú ñ ü Á É Í Ó Ú Ü a-zA-Z0-9]/', '', $_GET['location']); /* TAKE OUT THE foreach ... foreach ($param as $location) { $where []= "location = '$param'"; //THIS IS THE PROBLEM LINE } */ // AND USE impode $where[] = 'location IN ("' . implode('", "', $param) . '")'; } Note: You need to sanitize your inputs to prevent SQL injections. Have a look at mysql_real_escape_string()
-
having a for loop inside a mysql SELECT statement
DavidAM replied to micmola's topic in PHP Coding Help
Oops , Thanks sasa. I was originally going a different route and didn't think to change that. -
Backslash in a double-quoted string escapes the next character, so you would escape the backslash that you want with a backslash: elseif($_GET['f']=="\\") I'll admit it looks confusing, but that's they way it works. (Also, you should put quotes around the array keys when they are strings.)
-
File_Exists Returns False Even Though the File is There
DavidAM replied to chaseman's topic in PHP Coding Help
^^What he said ... and you are not using the same function call to get the name to test as you are to get the name to display $logo_dir = get_template_directory_uri() . "/images/logo.png"; if (file_exists($logo_dir)) { ?> <li><img src="<?php bloginfo ('template_directory'); ?>/images/logo.png" alt="<?php bloginfo('description'); ?>" /></li> I don't know the WordPress API, but I would guess that get_template_directory_uri() is going to return a value suitable for sending to the browser; and that bloginfo('template_directory') is going to return a file path suitable for use in PHP. Check for the proper usage of those functions. -
Isn't default a reserved word? If you are using it as a column name, you have to put back-ticks around it. ... AND `default` = '1' ...
-
getting this url variable $GET["price[min]"]
DavidAM replied to beanymanuk's topic in PHP Coding Help
Wouldn't that be something like: $_GET["price"]['min'] -
having a for loop inside a mysql SELECT statement
DavidAM replied to micmola's topic in PHP Coding Help
You want to build a query string using a for loop, not actually run a for loop inside a SELECT statement. Actually, you can do it without a for loop, just use implode(). Something like this should work: $query = "SELECT * FROM car WHERE offer_type = '" . $transaction_type . " AND "; $query .= "( car_name IN ('" . implode("' OR '", $sentence_array) . "')"; $query .= " OR car_color IN ('" . implode("' OR '", $sentence_array) . "')"; $query .= " OR car_seats IN ('" . implode("' OR '", $sentence_array) . "')"; $query .= ')'; Note: Untested. (I don't have an environment here to test with). Note: Make sure you sanitize those inputs if they are coming from a user. (see mysql_real_escape_string()) -
having a for loop inside a mysql SELECT statement
DavidAM replied to micmola's topic in PHP Coding Help
I'd strike that last part; I've seen a lot of things done in programming that made absolutely no sense. -
Actually, the first two lines of your "first set of code" turns on error reporting. So now you see an error in your second set of code. That error was there before you combined the two sets of code, you just did not have error reporting on and did not see the error. P.S. The solution is NOT to remove the error reporting. The solution is to fix the error.
-
First, ALWAYS put an exit after any header() redirect. Without the exit(), php will continue running code until the browser actually receives and acts on the redirect. That could be the problem here. loginscript.php <?php session_start(); $pass = strtolower($_POST['password']); $name = strtolower($_POST['username']); if($name == "test" && $pass == "test"){ $_SESSION['myuser']=$name; header ("location: select.php"); exit; } else{ // invalid login, so go back ... header ("location: index.php"); exit; } ?> select.php <?php session_start(); if(isset($_SESSION['myuser'])){ } else{ header ("location: index.php"); exit; } ?> If that does not solve it. Put an echo before each header() that says something like: "In login redirect to index". This will break the redirect, but at least we can figure out whether it is the loginscript header or the select header that is causing the redirect to index.php.
-
if($name == "test" && $pass == "test"){ header ("location: select.php"); } if(isset($_SESSION['myuser'])){ You never actually set a value for $_SESSION['myuser'].
-
No apology is necessary, I'm glad to help. I know to check for the basic stuff because it has bitten me before as well. Just a note about mysql_real_escape_string(). It is not intended to be a general escaping mechanism. It should be used immediately before going to the database. Carry the data values in the code "raw" and escape them on the way out. Otherwise, you run into problems trying to send it to HTML or MYSQL or anywhere else. $input = $_POST['txtInput']; // Of course, if magic_quotes_gpc() is on, we need stripslashes() if (get_magic_quotes_gpc()) $input = stripslashes($input); // USE $input FOR WHATEVER // NOW PREPARE $input FOR THE DATABASE $sql = "INSERT INTO myTable (UserText) VALUES ('" . mysql_real_escape_string($input) . "')";
-
That query looks like it would work (as long as the username used to login has the correct permissions in each db). Did you try it? Are you asking what value you would supply for mysql_select_db()? This function sets the default database that is used if you don't specify a database in the query. Since your query is qualifying each table with the db name, it should not matter what db you specify to mysql_select_db(). I would use the one that most of my queries will be accessing. You could leave the call out completely. Then any query you submit without qualifying the tablename with the database name will produce an error about not having a default db; which would remind you to add the call in with the appropriate database. Note: I have not tested to verify the part about leaving it out. It's pretty much a guess based on my understanding of the purpose of that function.