DavidAM
Staff Alumni-
Posts
1,984 -
Joined
-
Days Won
10
Everything posted by DavidAM
-
The concept of altering a table in a loop is soo scary and soo crazy that I hesitate to even wade into this murky swirling pool ... but what's life without a little adventure, eh? Question: What are your values for $depth and $width? If they both go into double digits (or higher, you are going to run into duplicate column names here. When $w = 1 and $d = 11, column name = testb111. Then when $w = 11 and $d = 1, column name = testb111 ... oh, look, that's the same name! If you are seriously creating a table this way, YOU REALLY NEED TO RE-THINK YOUR DATABASE DESIGN!!!!!!!!!!!!!!! IT IS WRONG!!!!!!!!!!!!!!!!!!!
- 4 replies
-
- mysql alter
- while loop
-
(and 1 more)
Tagged with:
-
Sessions are stored on the server. More specifically, the data in a session is stored on the server. Generally, the client has only a cookie to identify what session it is using. The server does not know anything about what happens on the client (the browser) until the browser sends a request. Generally, closing a tab or window or the entire browser application does NOT send any kind of request to the server. So, if you want the server to destroy the session on a page close, you have to send a message to the server. The only facility for doing that is to use AJAX in the beforeUnload event. However, as Christian F. said, this event will be triggered when moving from one page to another on the same server; probably even on a hard refresh (F5 / CTRL-F5) of the same page. So, it is really not a good candidate for clearing the session.
-
How To Store Selected Record In Variables Before Releasing
DavidAM replied to yshua's topic in MySQL Help
The stated question is not very clear, and the provided snippets do not provide much insight. If you want to collect all of the data from the query ... $data = array(); // Where we will store the data $foundRows = 0; // Number of rows returned $sql = 'SELECT ...'; if ($res = mysql_query($sql)) { $foundRows = mysql_num_rows($res); while ($row = mysql_fetch_assoc($res)) { $data[] = $row; } mysql_free_result($res); } Although, doing $foundRows = count($data) after the loop will provide the same answer as mysql_num_rows() in this case. I don't know what error you might be getting for not freeing the result before you do the another query (to update). If all you want is the number of rows, then don't select the data back, just select a count: $sql = 'SELECT COUNT(*) FROM table WHERE condition = true'; if ($res = mysql_query($sql)) { $foundRows = mysql_result($res, 0, 0); mysql_free_result($res); } else { $foundRows = 0; } -
Duplicate Queries Crash When Not Seperated
DavidAM replied to justlukeyou's topic in PHP Coding Help
It would be really, really helpful if you would tell us what the error says. You are using $row['id'] in both queries. We have no idea where that value is coming from for the first query since you didn't show the code above it. However: Your WHILE loop for the first query is assigning the data retrieved from the database to an array named $row. The WHILE loop will terminate when mysql_fetch_array returns false. When it returns false the value of $row becomes false (because you assigned the result to it). This is pretty much the standard way to process a result set. HOWEVER, you are then using $row['id'] to build your second query. In the example causing the error, $row is NOT an array, it is the value false, so you will get an error making the assignment, and probably an error from mysql_query as well. In the example that does NOT cause the error, you did not use a loop to process the single-row result-set (again, this is standard practice), so $row is still an array when you build that second query. For future reference, you should build your queries into a variable, then execute the variable. That way you can print the query and see why the server is complaining: $sql = 'SELECT column1, column2 FROM table1 WHERE id = ' . $id; $res = mysql_query($sql); if (! $res) { print('Error: ' . mysql_error() . ' SQL: ' . $sql); // or something like that } else { while ($row = mysql_fetch_assoc($res)) { // and so forth I see Pikachu has beat me with a better solution. However, my post answers the question for anyone who might be curious. -
After building the correct SQL query, you need to execute the query. See the manual mysql_query. If the query succeeds, you can then fetch the data (see mysql_fetch_array or mysql_fetch_assoc). The fetch will return an array of columns selected. You can then assign the value from the email column to the variable -- $to = $row['email'] or you can use the array directly in the mail() function -- mail($row['email'], .... Note: You should not use SELECT * unless you actually need EVERY column in the table. Just select the columns you are going to use. All of the data is returned to the script, so you are wasting resources (time and memory) if you select more than you need. Also, you need to review your headers. I have never seen Od : in the spec. Is that supposed to be To:, or what?
-
Actually with that code: $row will be false after the loop, so the assignment $to = $row['email'] will be throwing an error since the index ("email") does not exist (because $row is not an array). As trq stated, the query has not been executed. And as Muddy_Funster pointed out, It is not clear what you are trying to do. You are selecting ALL ROWS in the table, but there is NOTHING inside the WHILE loop to collect any data. If you are trying to send email to a single user, you need to add a condition to the query (WHERE UserID = 4 -- or whatever) and then you do not need the WHILE loop.
-
While I agree with Jessica that a DOM parser is the best way to go, there are a couple of things you can try with the regexp. 1) The dot in your pattern will NOT match a newline unless you add the "s" modifier. 2) Your pattern will not match a paragraph tag that is in uppercase, unless you use the "i" modifier. 3) Since you are not trying to anchor at the beginning or end of the string, you do NOT really need the "m" modifier. 4) Since you are looking for the match of the entire pattern, you do not need the capturing parenthesis. /<p[^>]*>.*?<\/p>/si might give you better luck. P.S. If you are attempting to scrape a site without permission of the site owner, please ignore my advice and repent from your evil ways.
-
You are using a "relative" path in the link. So the browser is building the absolute url by appending the relative part that you specified to the path of the current page. As far as the browser knows, it is looking at a page at http ://localhost:8080/TST/item/view/19 -- filename: 19, path: /TST/item/view -- so it ends up with /TST/item/view/il. When using mod_rewrite, it is easier (IMO) to always specify full paths (although you can leave the domain part out): <A href="/TST/il">LI</A>
-
Call To Undefined Function Mysql_Connect() Revisited....
DavidAM replied to ack's topic in PHP Coding Help
In your code, you use the die("Couldn't select database") call after a couple of INSERT statements as well. Are you sure it is not one of them that is failing? You should display the mysql_error in the die message, since mysql will tell you why the call failed. die("Insert Failed: $query: " . mysql_error()); # # OR # die("Couldn't select database: " . mysql_error()); or something along those lines. Oh, and use code tags ... around any code you post, it makes it a lot easier to read. -
Yes, a mySql stored procedure returns two "results sets". So you have to call next_result to "clear the pipe", so to speak, and allow other data to come through. If you look at mysqli.multi-query the examples show one way to do it (generically). Basically, you can call $con->more_results() to see if there is more; and call $con->next_result() to get the "more results". I'm not sure how this is impacted by prepared statements, I haven't tried it that way.
-
True, provided you are using innoDB. Of course, not all engines support the TRANSACTION option, either. A while back, I wrote an application for the company where I work. After a while, I dumped the data to load on my development server to test some code changes I was making. When I loaded the dump, it created all of the tables as myIsam. Apparently, the (production) host did not support innoDB, and silently created all of the tables as myIsam. The dump did include all of my foreign key constraints and the server did create indexes for them, but the cascade deletes and updates were not active in production; and the dump specified the engine as myIsam. Personally, I would have preferred that the production server fail the create table statements rather than convert them to a different engine. So I would have known about the lack of support for FKeys. Fortunately, this particular application did not have any DELETEs in it (at that point). Just another "Gotcha" you have to watch out for when software is designed to "protect" the "script kiddies" from their own lack of knowledge. Along the same lines as "magic quotes" and "register globals" (IMO).
-
If you are using mysqli you can use the mysqli.multi-query function. In any case, I would create a PHP function; start a TRANSACTION; execute the DELETEs one-by-one checking for success or failure after each; if ALL succeeded, I would COMMIT the transaction, if any fail, I would ROLLBACK. I have never, and would never, try to delete from multiple tables in one statement. I'm just too paranoid that way. Another option is to create a STORED PROCEDURE to handle the TRANSACTION and the DELETEs.
-
There are various ways to work with that, depending on the application. For instance, in this particular case, I would be tempted to use an array for the fields: <input type="text" autocomplete="off" name="par[2]" value="" id="hole2B" /> Then you can check each hole in a for loop. for ($hole = 1; $hole <= 9; $hole++) { if (trim($_POST['par'][$hole]) == '') { // An error, this value is required } } If the fields were vastly different, say like a user profile page, I might check for required fields like this: // Create an array of the required field names $required = array('DisplayName', 'EmailAddress', 'IQ', 'Gender'); // Create an empty array to hold the error messages $errors = array(); foreach ($required as $field) { $test = (isset($_POST[$field]) ? trim($_POST[$field]) : null); if (empty($test)) $error[] = $field . ' is a required field'; } Or something along those lines. Generally, the validation is different enough for each field that I just validate one field at a time.
-
Are you sure it is the same error message? The message from your original quote was actually two error messages from mysqli and one from your application. You should not be getting the first error message since you are now providing two parameters. Post the actual error message that you received.
-
The mysqli_query() function requires the connection as a parameter. Which is what the error message is telling you. You've done it correctly later in the script, you will need to pass the connection as a parameter to the function and then use it in the function
-
This field is empty, and it is readonly, so the user can not put anything in it. Since you are using the loop to check all fields on the form, this field will trigger the message you are getting. Also, the suggestion in my previous post might not work if any of the fields might contain the number zero. The empty() construct considers a zero to be "empty". It might be better to check if trim($value) == "". However, this still leaves the empty readonly fields in your form. This is another reason for checking each field specifically rather than just testing all "submitted" fields. If these fields are populated by some Javascript function, it may be failing. Try printing the $_POST array to see what you are receiving when the form is submitted: if(isset($_POST['processForm'])) { printf('<PRE>%s</PRE>', htmlspecialchars(print_r($_POST, true))); die(); You might also post your code as modified so we can see what it looks like, now.
-
INPUT text fields will always be set in the POST array. You can use empty. However, if they put a space in the field, the field will not be empty. So, you have to trim the data and then test for empty. But you can't use if (empty(trim($value))) (unless you are using PHP 5.5 or later) because empty() is a construct not a function. You can use: foreach ($_POST as $value) { $test = trim($value); if (empty($test)) { echo "You haven't filled out all of the fields"; exit(); } } Note that this does not solve some other problems. 1) It is trivial for a user to modify a form to submit fewer or different field names than what you expect. Which would leave you with missing data. It is always better to specifically check to see if the fields you expect are set and have valid values. 2) If the user misses just one field, your design will leave them with a blank page. They will have to go back to the form and re-enter ALL of the information again. It is usually better to use a "sticky form". That is, if the fields are not valid or are missing, re-display the form with the data they did provide already filled in so they only have to enter the missing/invalid data.
-
Am I Completely Missing Something Or Is This Being Awkward For Me
DavidAM replied to drewwales's topic in PHP Coding Help
Why would you loop through an entire directory to look for one file when you know the name of the file you want? $imageDir = "../images/services/"; $pageFile = $strPageTitle . ".jpg"; if (file_exists($imageDir . $pageFile) { // Do your image output here } else { // Do your NO IMAGE output here } Or did I miss something? -
1) That is NOT PHP code, it is Javascript code. 2) INPUT elements do NOT have any innerHtml to be retrieved 3) You are trying to retrieve data ByID for ID's that do NOT exist in that code.
-
A single equals-sign "=" is the assignment operator. So the first IF statement is assigning "y" to $find and will always be true. To compare, use the comparison operator, "==" (two equals-signs). Also, you should check to see if the "x" variable is actually set in the URL. Also, if $find is equal to "y" then it can't be equal to "z", so there is no sense in doing the second IF. Use an "ELSE IF" $find = (isset($_GET['x'] ? $_GET['x'] : ''); if ($find == 'y') { sql = "SELECT * FROM table1"; } else if ($find == 'z') { sql = "SELECT * FROM table2"; } else { // Do something here because there is no $sql value since 'x' is not "y" or "z" } mysql_query($sql, $connection);
-
I think you need to learn more about SQL, and databases in general. I don't see any reason for the while loop to run twice. In fact, it is not running twice. The problem is that you are retrieving the data "twice". If you are not using data from the admin table, then take it out of the query. If you are using data from the table, then you need to join that table to one of the other tables in the query. As it is, you are getting a Cartesian product, which will cause the output to look like you are printing each row twice, when in fact you are retrieving all of the data twice. var_dump is a PHP function that prints the contents and data-type of a variable. It is a useful tool for debugging when you want to see what a variable contains. As I stated earlier, it is not going to help you in this case; 1) $result is a resource not all of the data from the query, and 2) the problem is not with any of the variables, the problem is your query. Try running that query directly in mysql and see what the output is.
-
@CPD $results does not exist in that script. $result is a mysql resource and var_dump'ing it will not show the duplicate rows. Other than that, you are on the right track. @EzwanAbid You have three tables in that query, but only one "join" condition. So every row that matches student and surat will be matched with every row in the admin table (a Cartesian product). My guess is you have two rows in that table. You need to fix the query to fix your problem.
-
In our world, it is Daemon ... Wikipedia - Daemon Demons, on the other hand, are the guys who run around injecting bugs into my perfectly good release code. Kind of like Gremlins, only meaner and smarter.
-
Be nice, and post the problem you found and your solution. Then the next guy (or gal) that searches the forum might find your solution and solve their own problem.
-
Try again fopen Hint: If you want to read data from a file, you need to start somewhere before the end of all of the data in the file. And, since you are not writing to the file in that code, you don't need to open it "for writing", just "for reading"