jcbones
Staff Alumni-
Posts
2,653 -
Joined
-
Last visited
-
Days Won
8
Everything posted by jcbones
-
Do you have mysql installed? Should reside at C:\\wamp\bin\mysql Do you have phpmyadmin installed? Should reside at C:\\wamp\www\phpmyadmin
-
Try putting your events rule above your index rule. The rule with the broadest brush should reside at the bottom so it is parsed last.
- 3 replies
-
- mod_rewrite
- htaccess
-
(and 2 more)
Tagged with:
-
1. Is your file extension ".php"? 2. What exactly does "neither of these worked" mean? A. Page loads, but no data from MySQL? a. Have you tried the query in mysql console, or software like phpmyadmin? b. Have you tried debugging with mysqli_error? B. Link fails to locate the page? a. Have you tried setting a full URI? b. Are you sure the file resides where you are pointing to?
-
Dumping into a database, you would think along the lines of: A. Using PHP, create a SQL file with the values correctly formatted to insert into the database via a 'local data infile'; B. Using PHP, create a CSV file, and insert with a 'local data infile'; C. Use PHP to build a SQL query string with the JSON data, and insert via a call to the database. Example of C: (untested, and noting that this file may take a long time to execute, but you only have to do it once, right?) <?php $path = '/path/to/json/files'; $files = glob($path . '/*.json'); //get all json file names. $chunks = array_chunk($files,500); //split these filenames up into groups of 500 (you said 11k+ right). foreach($chunks as $file) { //for each group of 500 files. $queryParts = array(); //set an empty array. foreach($file as $filename) { //foreach file $data = file_get_contents($path . '/' . $filename); list($firstname, $lastname, $height, $rating, $pace, $shooting, $passing, $dribbling, $heading) = json_decode($data); //I'm supposing that there are only person per file? $queryParts[] = "('$firstname','$lastname','$height','$rating','$pace','$shooting','$passing','$dribbling','$heading')"; //build a value string. } //through with all the current files. (up to 500). $queryStrings[] = "INSERT INTO table (firstname,lastname,height,rating,pace,shooting,passing,dribbling,heading) VALUES " . implode(' , ',$queryParts); //build a query string, this should contain at most 500 rows of data. } //all files have been looked at. include('connection.php'); //make a database connection. foreach($queryStrings as $value) { //go through the query strings should be 23 strings. mysql_query($value); //execute each one. (by this time, you should be staying clear of mysql in favor of mysqli or PDO). }
-
Barands version is a much better way, as it uses only one pass. Try this one: Make sure you put an index on 'type'. SELECT id, name, @num := if( @type = type , @num +1, 1 ) AS row_number, @type := type AS dummy FROM products FORCE INDEX ( type ) GROUP BY type, name HAVING row_number <=2 I do believe this is a one pass solution, so it should be the fastest. Hoping Barand will approve.
-
I wouldn't use md5 as it is easily broken through look up tables. I think todays standards are sha1 or better, some even going to crypt. Whatever you decide, use a salt. Very important.
-
I claim no authorship of this Expression. I got it from the public domain at Regular Expression Library, and provided by Steve Smith. $pattern = '#^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,25}$#'; //modified to handle from 6 to 25 characters. $password = $_POST['password']; if(preg_match($pattern,$password)) { echo 'Passwords good!'; } else { echo 'Invalid password!'; } Description: Password matching expression. Password must be at least 4 characters, no more than 8 characters, and must include at least one upper case letter, one lower case letter, and one numeric digit.
-
Don't ask me why or how it works... $sql = "SELECT type, name FROM products WHERE ( SELECT COUNT(id) FROM products AS p WHERE p.type = products.type AND p.id < products.id ) < 2"; //change the last number (currently 2) to the number of desired results per group.
-
1. For relational database design, I suggest watch . It shouldn't take long to watch them, but they will help you in future projects. For JOIN syntax, look at MySQL Manual, although it may seem a little confusing at first, this is the most valuable site for MySQL syntax, and how it works. 2. I don't recommend mysql_fetch_array for anything beyond very special cases. The reason is that it returns redundant data, both associative and numerical array indexes. Since you are only using an associative array, I changed that to mysql_fetch_assoc. 3. The reason we check if the category name is NOT in the cat array, is that it will only be there if we have echo'd it. As we put it in the array right after we do. When you join the tables for a query, the category name will appear for each and every item in the items table. So since you don't want the category name to appear more than once, we echo it, then prevent it from echo'ing (by making sure it is not in the $cat array) until it changes. Hope that helps you understand a little bit more.
-
This is so you do not get warnings about undeclared variables. You could just set both variables to null at the very top of your script, then populate them as needed. That would remove the 'else' clause on the first if/elseif/else statement. I will give this as an example so you can see it. I also took the liberty to point out some other items as well. NOT TESTED! <?php require_once("includes/connection.php"); //include connection. $sel_category = null; //declare variable. $sel_items = null; //declare variable. if(isset($_GET['category_id'])) // This grabs the category_id from the URL from $categories_fetch_array below. { $sel_category = $_GET['category_id']; // If it is set then $sel_category will be assigned to the category ID } elseif (isset($_GET['item_id'])) // This grabs the item_id number from the $items_fetch_array below. { $sel_items = $_GET['item_id']; // Then the ID gets assigned to the vairbale $sel_items } //Here is where we will the cataegories by ID //First we write the query statement // Return data from the DB //Running queries in loops is a really bad idea. Most of the programmers around here will try to steer you clear //of this bad practice. It leads to horribly inefficent code, and loads of problems with data collision. //So, I urge you strongly to learn about MySQL JOIN syntax, and relational database design. $category_query = "SELECT c.category_name, c.id AS category_id, i.id AS item_id, i.item_name FROM catagories AS c JOIN items AS i ON c.id = i.category_id"; $category_result = mysql_query($category_query,$connection); //run the query. if(!$category_result) { die("mySQL DB read failed" . mysql_error()); //error handling. } $cat = array(); //array to hold all categories processed. //use the return data while($categories_fetch_array = mysql_fetch_assoc($category_result)) { //I don't suggest using fetch_array(), use either fetch_row(), or fetch_assoc(). fetch_array() just uses more overhead. //using some logic, we can find out if we already printed the categories to the window. if(!in_array($categories_fetch_array['category_name'],$cat)) { //if the category name is NOT found in the $cat array, then run this block of code. echo "<b>Catgeories:</b>" ."<a href=\"content.php?category_id=" . urlencode($categories_fetch_array['category_id']) . "\">" . $categories_fetch_array['category_name'] . "</a>" . "<br />"; // Links get outputed here $cat[] = $categories_fetch_array['category_name']; //add the category name to the $cat array. } if(!empty($categories_fetch_array['item_name'])) { //if the item name is NOT empty, then run this block of code. // Links get outputed here echo "<b>Items:</b>" . "<a href=\"content.php?item_id=" . urlencode($categories_fetch_array['item_id']) . "\">" . $categories_fetch_array['item_name'] . "</a>" . "<br />"; } }// End of while statment ?> <p> <?php echo $sel_category;// This varible $_GET['category_id'] echo $sel_items; // This varibale echo's out the $_GET['item_name'] mysql_close($connection); //this function is only needed if you desire to change the mysql connection, or the script is so memory intensive that you wish to clear some room. The connection will be closed when the script ends. ?>
-
To show archive's just change the query: $sql = "SELECT * FROM `table` WHERE `archive` = 1";
-
Post the error messages.
-
Just add an archive COLUMN, not a separate table. You would modify this with an update, just as you are with your 'edit' buttons.
-
Christian's way is how I always handle this situation. I will vote it up on that basis.
-
Looping Through Arrays And Finding The Unique Ones
jcbones replied to jattsurma's topic in PHP Coding Help
I would build it a little different, which would help with the sorting issues. $count = 0; $bundle_pickup[$_SESSION['bundle'][$bundle_ID]['pickup_date']][$_SESSION['bundle'][$bundle_ID]['pickup_time']]['bundle' . ++$count] = $bundle_ID; Run that, and you should get an output similar to: Array ( [2012-11-08] => Array ( [9 AM to 12 PM] => Array ( [bundle1] => 3 [bundle2] => 4 [bundle3] => 5 [bundle4] => 6 ) ) ) Does this look something like what you are looking for?- 2 replies
-
- php arrays
- multidimensional arrays
- (and 3 more)
-
If you go with the regex route, don't forget to change the function to preg_replace.
-
Load data local infile syntax. If your fields are quoted in the CSV, you should be using ENCLOSED BY also. This would make your query: $sql = "LOAD DATA LOCAL INFILE '$location' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' (firstname, lastname, rank, era)";
-
Constants are considered strings when inside of quotes. Correct would be: echo CNT_TXT_EMPTYMESSAGE; Are you sure this constant doesn't contain an empty message?
-
Clean Urls - Replacing '%20' With A '-' Help!
jcbones replied to SimonBruce's topic in PHP Coding Help
function Title(){ return htmlspecialchars($this->data['n_title']); } -
Alternating Style While Fetching Database Info
jcbones replied to MrXander's topic in PHP Coding Help
Try this: <?php $sql2 = mysql_query("SELECT * FROM `comments` WHERE `relatingto`='".$_GET['id']."' AND `hidden`='No' AND `deletedbyadmin`='No'"); while($dat2 = mysql_fetch_array($sql)){ $comment = ($comment == 'comment_odd') ? 'comment_even' : 'comment_odd'; //if $comment is 'comment_odd, change to comment_even, else change to comment_odd. Numbers start off odd right? ?> <li class="<?php echo $comment; ?>"> <div class="author"><img class="avatar" src="images/avatar.gif" width="32" height="32" alt="" /><span class="name"><a href="#"><?php echo $dat2['poster']; ?></a></span> <span class="wrote">wrote:</span></div> <div class="submitdate"><a href="#"><?php echo $dat2['time']; ?></a></div> <p><?php echo $dat['comment']; ?></p> </li> <?php } ?> -
You can store objects in sessions, have you tried serialize() and unserialize()?
-
After 3 Fail Login Attemp User Block For 3Mins
jcbones replied to daikumi's topic in PHP Coding Help
You need to create a session variable that stores the time on the third submission (which needs to also be tracked). When the difference between that time, and the current time is greater than 180 seconds(current - oldtime), then you can allow the next submission. An example would be: if((time() - $_SESSION['time_of_block']) > 180) { //allow submission } else { echo 'You have attempted to login 3 times, please try again later!'; } Now this will work on only this session, closing the browser, and reopening will let someone login before the 3 minutes is up. However, changing it from a session to a cookie would breach the "close and reopen" thing. To get even more secure, save this to the database in a new table, joining it to the user table on the checkin. Now, if you would allow me to give you some pointers in your script, I would be happy to do so. <html> <head> <title>LOG IN</title> </head> <?php session_start(); //this call is required to be made before headers are sent to the browser, but it isn't in this case. Move it to the very top of the script. $username = (@$_POST['username']); //don't suppress errors, verify that the index exists *isset() or empty()*. $password = (@$_POST['password']); //why even set this at all, if you use the $_POST array for everything below? $error['alert'] = ''; $error['username'] = ''; $error['password'] = ''; $input['username'] = ''; $input['password'] = ''; if (isset($_POST['submit'])) { if ($_POST['username'] == '' || $_POST['password'] == '') //I'm not sure which is faster, this way or empty()? { if ($_POST['username'] == '') { $error['username'] = 'required'; } if ($_POST['password'] == '') { $error['password'] = 'required'; } $error['alert'] = 'Please fill in required fields!'; $_POST['username']; $_POST['password']; include('v_login.php'); } else $error['alert'] = "Username or Password is incorrect"; { if ($username&&$password)//since these variables are not explicitly 'true' or 'false', I personally don't like using them in this context. I prefer empty(), as it returns true or false. { $connect = mysql_connect("server","dbusername","dbpassword"); //mysql libraries are out of date, I suggest using the prefered mysqli libraries. mysql_select_db("dbname") or die ("Couldn't find db"); $query = mysql_query("SELECT * FROM users WHERE username='$username'"); $numrows= mysql_num_rows($query); if($numrows!==0) { while($row=mysql_fetch_assoc($query)) { $dbusername= $row['username']; //to save on memory (which isn't a problem in small scripts), I suggest using the array indexes, instead of saving to a variable. $dbpassword= $row['password']; //this might not make a difference on a script this size, but is a good coding habit to form. $activated= $row['activated']; //and will save your hands from typing that little bit extra . if($activated=='0') { header("location:not_active.php"); //headers are already sent, this should cause an error. exit(); //bravo, this is a commonly missed necessity. } } if($username==$dbusername&& md5($password)==$dbpassword) { header("location:member.php"); //headers are already sent, should error out. $_SESSION['username']=$username; } else $error['alert'] = "Username or Password is incorrect"; include('v_login.php'); } else { $error['alert'] = "Username or Password is incorrect"; include('v_login.php'); } } else { echo ""; //no need to echo an empty string. } } } else { include('v_login.php'); } ?> </html> -
Put this at the top of your paypal IPN return script: $posted_data = print_r($_POST,true); file_put_contents('IPN_data.txt',$posted_data); This will create a file (in the same directory)named "IPN_data.txt" with the contents of Paypals POST array. Verify that the expected data is arriving correctly, and indexes are not mispelled.
-
PHP is stateless, and is run on the server. After a client (browser) calls the server, it translates the PHP, and sends results to the browser. PHP is then finished with it's project, and forgets everything it just did. There is no constant "connection" between the client and the server. In order to update a "cell" in a table on-click or otherwise, you will have to use a client side scripting language (javascript) to call the server, and ask for the data. This method is called Asynchronous Javascript (AJAX). You will need to know a little bit of PHP, Javascript, and HTML in order to get it to work correctly. More specifically: How to use PHP's $_GET array. How to make a HTTP request with Javascript How to receive a HTTP request with Javascript How to take the results, and put them into the HTML table There are libraries like Jquery, which make AJAX a little easier. Although, I even find their manual a little bit confusing. I hope this answered your last question.