-
Posts
5,450 -
Joined
-
Days Won
175
Everything posted by mac_gyver
-
Column count doesn't match value count at row 1
mac_gyver replied to Chrisj's topic in PHP Coding Help
the error means that the list of columns in an INSERT query doesn't match the list of values you are supplying in the INSERT query. either the list of columns is wrong or the list of values is wrong or there's something wrong with the syntax so that the number of columns or values is ambiguous. -
there are two possibilities, either the ->get_result() method is not available (your php version is right, but the driver that php uses must also be the mysqlnd driver) or your code has some non-printing/character-encoded characters as part of that statement. 1) for the first possibility, does the output from a phpinfo() statement have a main section for the mysqlnd driver? the msyqlnd name and version number should also show up under the msyqli main section as part of the Client API library version information. 2) if you copy/pasted the code or are typing code on a non-English keyboard/operating system, i would delete and retype the entire line of code where the error is occurring at, making sure that only ASCII characters are being used. as to PDO examples, your previous code using PDO would be where to look. all you are doing is preparing the sql statement, binding any input data, executing the query, and fetching any result.
-
the information you just posted is for your mysql server. what matters, and what i asked about, is your php version.
-
what php version are you using? and for the the code in post #3, is that code being ran on the same server as the current problem code and are you sure it is actually taking the if ($result) { execution path? i can think of at least one case where the INSERT query in the code in post #3 is failing due to an error, when there is already a row in that database table due to previous code-version/testing, and it isn't trying to run the code where the ->get_result() method call is at. are you open to using the PDO database class instead or the mysqli class? PDO is much more constant and easy to use, especially with prepared queries. edit: i just looked at your previous threads. you are/were using PDO. why did you take a step backwards with this code and use mysqli? edit2: and the Call to undefined method mysqli_stmt::get_result() error is exactly the same error you were getting in a thread on November the 2nd. so, i doubt your code in post #3 in this thread is actually doing what you think or if it is running, it's not running on the same server as the current code.
-
see the three different 'version A, B, C' examples in this code - <form action='inc_artist_search_box.php' method='get'> <p>Search for your favorite artist, song, or label</p> <input type='text' name='userSearch' value='<?php echo $_GET['userSearch']; ?>' /> <input type='submit' value='Search' /> </form> <hr /> <?php // make the database connection first. a database connection is required for the mysqli_real_escape_string() function that's being used when the sql search term is being built $servername = '127.0.0.1'; $username = 'root'; $password = 'Conquest1'; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; //select database if(mysqli_select_db($conn,"Artist")) echo "connection successful"; else echo "connection failed"; $k= isset($_GET['userSearch']) ? trim($_GET['userSearch']) : ''; //get user search term // your code implies that a search term is required (an empty WHERE in the sql statement would result in a query error) // if you instead want to match all rows when there isn't a search term, the WHERE keyword in the sql statement would need to be added only if there is a where term or you would need to supply a true value after the empty WHERE keyword if($k == ''){ echo 'A search term is required'; } else { $terms = array(); $search=explode(" ", $k);//user search term into array //loop through search term and find terms that are like query request // if the columns to include in the search is selectable (checkboxes?) see the 'version C example code' below // you would build the $search_columns variable at this point in the code with whatever columns you want // if you are just asking to always search a list of columns, you can either use the 'version B example code' below // or you could build the $search_columns variable at this point with the columns hard-coded in it and use the 'version C example code' below foreach($search as $each){ $each = trim($each); if($each != ''){ $each = $conn->real_escape_string($each); // if not using a prepared query, escape the string data // **** version A example code - duplicates what you posted at the start of the thread $terms[] = "first_name LIKE '%$each%'"; // **** version B example code - // to unconditionally search multiple columns // replace the above line of code with this - $terms[] = "CONCAT_WS(' ',first_name, last_name, groups) LIKE '%$each%'"; // **** version C example code - // if the columns to be included in the search are picked via some section process (checkboxes?), // you would build the list of column names in a php variable before the start of this loop, // then just use that variable in this code. // note: do NOT put raw external user data into a query. if the column names are coming directly from user supplied data, you MUST validate that they are exactly and only permitted column names. no amount of escaping the values will protect against sql injection because these are not string data values. // replace the above line of code with this - $terms[] = "CONCAT_WS(' ',$search_columns) LIKE '%$each%'"; } } $query = "Select first_name, last_name, groups, era FROM artist_table WHERE " . implode(' OR ', $terms); $query=$conn->query($query);//perform the query $numrows=mysqli_num_rows($query);//number of rows in query results found //loop throw rows and extra data if($numrows > 0){ while($row=mysqli_fetch_assoc($query)){ $first_name=$row['first_name']; $last_name=$row['last_name']; $group=$row['group']; $era=$row['era']; echo $first_name; echo $last_name; echo $group; echo $era; } } else echo"No search results found for \"<b>$k</b>\""; }
-
Warning: Cannot modify header information
mac_gyver replied to moosey_man1988's topic in PHP Coding Help
the <head></head> section of your html document doesn't have anything to do with this. this concerns the http protocol and what gets sent back and forth between the browser and the server before you send any character of data between the two. -
that's because you are asking the query to search for either anything that contains a space - %space here% or anything %%. you should probably trim() the $each value and only use it if it is not an empty string, which is different than using empty(), which would treat a search for a 0 (zero) as empty. the coding suggestions that have been made will actually reduce and simply the amount of code it takes to build the sql statement.
-
if you are going to be doing this in the sql statement, here's another tip that will simplify the sql and may improve the performance of the query (your LIKE term, with a leading % isn't using an index anyway.) you can concatenate the database columns you are searching, and use one LIKE comparison for each of the search keywords (the following would be what gets imploded/joined with the ' OR ' between them.) the sql would look like - CONCAT_WS(' ',first_name, last_name, groups) LIKE '%$each%' in this case, you would also dynamically build the list of column names to put into the CONCATE_WS(' ',...) syntax based on whatever inputs you are testing.
-
as you are testing the inputs to determine what terms should be part of the sql statement, you need to add each OR'ed term to an array, then just implode/join the array elements with the ' OR ' condition between them. this will simplify your code (you won't have to test if any term is the first one.) you should also use a prepared query, rather than to put your $each search term directly into the sql statement. see the following thread - http://forums.phpfreaks.com/topic/299482-php-pdo-how-to-bind-values-to-varibale-contains-concatenated-string/
-
Warning: Cannot modify header information
mac_gyver replied to moosey_man1988's topic in PHP Coding Help
this is a very common error. if you had searched for it (there's even a thread from this time yesterday with the same error - http://forums.phpfreaks.com/topic/299626-cannot-modify-header-information/ ) you would have found what it means. YOU CANNOT SEND ANYTHING* TO THE BROWSER BEFORE YOU USE A HEADER(), SESSION_START(), OR SETCOOKE() STATEMENT. the http protocol REQUIRES that any 'control' headers be sent to the browser before you send anything else to the browser. * - anything includes a BOM (Byte Order Mark) character that your editor saved at the start of your file, any characters outside of <?php ?> tags, even if those characters are white-space characters that you cannot see or are not rendered for display by the browser, the <doctype tag and any other html/javascrpt/css markup are CHARACTERS being sent to the browser, anything your php code echos, prints, var_dump()s, or print_r()s, or anything that php outputs, such as a php error message. your php header() statement must be located in your code before you start sending the html/javascript/css to the browser. your page control logic that determines what to do on the page, or even if you are going to stay on the page, should be near the start of your code. you should only produce and send html/javascript/css to the browser after you have determined if you are even going to stay on the page. edit: see the following post for a suggested page layout that will help make your code fool proof - http://forums.phpfreaks.com/topic/297824-database-issues-and-working/?do=findComment&comment=1519095 your page access level check/header() redirect would be part of item #3 in that page layout. all your code for producing and outputting the web page would fall under items 5-8 in that list. -
similar to Barand's reply, you have both too much program logic and it's not organized. it's also doing things that don't make sense in the context where you are doing them (htmlentities() is an output function and should only be used on values being output to the browser, not on values that you are using in a database query.) for your actual problem, you are dong a header redirect after you have output an error message for a failed prepare() statement (you have a sql syntax error in the update query.) you will never see that error message, because php/most of the all-in-one development systems have output_buffering turned on by default and any output your code produced is lost when the redirect occurs. prepare() and execute()/query() errors are fatal problems for code that's database dependent. your code should have logic that handles all database errors. the database error information should be displayed when learning, developing, or debugging code, and it should be logged when your code is running on a live server. when there is a database error, your code should not continue running as though no error occurred. it's easiest to handle fatal errors within your code by using exceptions. you won't have to put a conditional statement around each database function that can fail (you don't have any conditional test around your ->execute() statements anyways.) for code that will never be used on a live server, you can just enable exceptions for the database statements, without catching them or supplying your own exception handler, to get the error information to be output (uncaught exceptions are fatal php errors and php will stop program execution if one occurs..) to enable exceptions for the mysqli_ statements, add the following two lines of code - $driver = new mysqli_driver(); $driver->report_mode = MYSQLI_REPORT_ALL; // note: this will also throw exceptions for queries that don't use an index. if you only want excpetions for actual errors, use MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT
-
we know what you are trying to do, the point is, in order for you to do it, and not just us fixing each thing that's wrong with your code, you must know what the output is supposed to be and when it isn't the correct output, to debug what your code is doing to find why it isn't the expected output. i recommend that you look at the 'view source' of your page in your browser. you will see a php/mysql error at the bottom that would help you determine why it currently isn't working. if you had organized your code as suggested in post #40 in this thread, with any database dependent code separate from any html markup, the php error wouldn't be buried in the 'view source'.
-
you need to determine what your script is doing, in order to pin down where and what the problem is, before you can fix it. just randomly changing things without any purpose or plan won't produce any result. is the html markup correct for the form, the select menu, and option choices within the select menu?
-
the problem is line 88 in your REAL code, along with including/defining/making two database connections. the code in post #24, that you stated was your real code, had lines 69-89 removed. it turns out on line 88 you are closing the database connection, so, any use of the database after that point will produce the error in question. rather than to piece together blocks of code to build your web page, you need to organize your program logic so that any database specific code is grouped together and it all uses a single database connection. see the following recommended layout - http://forums.phpfreaks.com/topic/297824-database-issues-and-working/?do=findComment&comment=1519095 either don't close your database connection and let php close it for you or close it after the point where you have finished with database queries.
-
Using Ajax GET Value in another form
mac_gyver replied to MartynLearnsPHP's topic in PHP Coding Help
your database design is storing same type of data, that only differs in its value, spread out in columns, which makes any code to use that data overly complicated. you are looping over all the row(s) the first query matches (is there even more than one row per adminid?), to get a value out of the correct column, just to use it in the second query to find the actual data. if your database is designed correctly, ALL that code would go-a-way and be replaced with a single JOINed query. i reviewed your two most recent threads. it turns out i provided help in both of them. this problem of writing out huge quantities of repetitive code, was in the way, before, of getting your code to do what you want, and it is still getting in the way, now, of producing code that works or debugging why the code isn't working. i would start by getting your database table(s) designed correctly. this will make writing the code and queries easy. -
most of your Arr column values are empty, and the ones that are not have a carriage-return \r appended to them and won't match what you are using as values in the php variable. you should remove the carriage-return from the database values.
-
your code actually works for me. the only way i could reproduce your implied errors (if you post the actual errors, they may contain information that may help) is if the connection is failing and your connection code isn't exiting when there is a connection error. the only way you can get the - Warning: mysqli_query(): Couldn't fetch mysqli in your_file on line 22 and Warning: mysqli_error(): Couldn't fetch mysqli in your_file on line 22 errors, is if $conn exists, but isn't a valid connection. ^^^ given the name of your include file, do you have two different files, one for the localhost and one for a live server, and your actual code is including the live server version and it doesn't contain either any connection error checking or it doesn't contain the exit; statement in the connection error logic?
-
here's one method - 1) take all the rows that have departure/arrival times between the earliest/latest departure/arrival time entered for the trip. 2) produce all possible combinations, taken 1 at a time, 2 at a time, .. up to the maximum number of segments you are willing to use in one trip (three would probably be a good maximum.) 3) throw out any routes that don't have an overall start and end point that matches the trip's start and end point and that don't have matching endings of one segment the same as the start of the next segment for all segments in the route. 4) for the remaining routes, calculate the layover time between each segment and the overall time of each route. 5) throw out any routes where any layover time between any segments is below a reasonable amount. 6) order the remaining routes by the number of segments, from least to most, and the overall time of each route, from least to most within the the same number of segments. you may in fact be able to do this in an sql query. producing all the possible combinations, taken 1 at a time, 2 at a time, and 3 at a time, would (likely, just proposing this as a possibility) involve doing a UNION between the single segments, the segments joined to themselves once, and the segments joined to themselves twice, with JOIN, WHERE, and HAVING conditions to only include rows in the result set that have the correct overall start and end points, matching segment-to-segment end/start points, and minimum layover times between segments.
-
edit: and you already have an existing thread for this question - http://forums.phpfreaks.com/topic/298576-session-expires-too-soon/ what exactly are you trying to accomplish by extending the session? by definition and design, sessions expire when the browser is (completely) closed or when the garbage collection runs and removes old session data (the default of which is just 1440 seconds old.) you can change these, but you must have a good reason for doing so and understand under what conditions it will work.
-
if you want to test if a value is (or is not) one of several choices, define the different choices as elements in an array and use in_array() or !in_array() to perform the test.
-
the current error - Couldn't fetch mysqli in... means that the connection variable doesn't contain a valid (procedural) database connection. either your database connection code is failing or your code is closing the connection at some point. does your database connection code have error checking logic in it and when there is a connection error, does your code take an appropriate action, to prevent the rest of the code from trying to use a connection that failed? the current error is a 'follow on' error. it's not where the problem is at. do you have php's error_reporting set to E_ALL. you should be getting more (types of) php error messages that would help pin down the cause of the problem.
-
Session data outside root directory not available
mac_gyver replied to tsangaris's topic in PHP Coding Help
are you changing the master php.ini (this would require owner/root access to the server) or a local php.ini (this would be the case if you only have an account on a shared web server), what is your php version, and is php running as a CGI/FastCGI application on the web server? the most likely reason is you have a case were you are using a local php.ini and it only affects the folder that it is in. try copying the php.ini to the ajax folder or put any files that you are going to be making http requests to in the public_html folder. you may want to see the following section of the php.net documentation for the different server/version configurations that determine how php finds the settings it uses - http://php.net/manual/en/configuration.php -
the problem's not in the actual code. it's in the environment where the code is running. the code suddenly didn't alter itself and start including/requiring the same class definition multiple times just because you moved where it is running. the code is probably trying to load same-name classes, but from different locations, and it or php cannot build or resolve the different locations correctly. some suggestions - 1) make sure that the php include_path setting is the same between the old and new system and that is whatever is suggested by magento. 2) the code may be trying to use set_include_path()/ini_set() statements to control where it's telling php to search for files, but those functions are either disabled on the new server or the path separator character being used is hard-coded and doesn't match the usage for the operating system (this would only be the case if you switched to/from a Windows/non-windows operating system.) 3) php has had a few bugs involving file paths for included/required files. check the php bug reports for the php version being used and the php change log for versions higher than your version, to see if this may be the case. 4) i saw references to caching/'compiled' code when doing a search for this general error. make sure that any sort of php bytecode caching/accelerator/APC files that came from the old system have been deleted. perhaps there's a bug in the cache 'hit' logic and the actual php file and the cached/'complied' php code are both being included/required.
-
i think (it's not entirely clear what you are asking or what problem you are having with your code) you are building up the entire content in a single variable and have a 'cart before the horse problem'. you should instead build up each different piece of the content in a separate variable, then combine the variables at the end or just output each separate variable in your html document at the appropriate place. build up the navigation in its own variable, so that all the logic that contributes to the navigation can add to just the navigation.