Jump to content

Jamdog

Members
  • Posts

    21
  • Joined

  • Last visited

    Never

About Jamdog

  • Birthday 07/06/1973

Profile Information

  • Gender
    Male
  • Location
    England

Jamdog's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. As far as I can tell, there is a fundamental flaw in your code - here's it in pseudo-code, as I see it While more CSV lines in file { Get line Add to Data Array } // Data array now contains the WHOLE CSV file! Foreach Line in the Data Array { If the 5th field on the line is set, reformat the date Now, hang on there a sec, you earlier said: So, how can there be a 5th field on that line? OK, I'll continue: Put together all the fields on the line, separated by commas and quotes Trim, add quotes to the start and end then add the new string to an array ($values) // At this point, $values is now an array of lines from the CSV, comma-seperated for SQL entry } Foreach Values { Add brackets and put in a new array, $v } Implode $v, comma-separated Throw the whole CSV file into the SQL database in one go! Sorry, I don't like that last line at all... I've never done it, and if a huge CSV file is encountered, something could go wrong (and possibly has). Why not simplify this a bit... Get rid of the values and v arrays, and from here, put the lines in one at a time: $sql = "INSERT INTO TableName (`Field1`,`Field2`,`Field3`,`Field4`,`Field5`,`Field6`,`Field7`) VALUES "; foreach ($data as $line) { if (isset($line[5])) { // format the date $date = explode('/', $line[5]); // put it back in $line $line[5] = $date[2].'-'.$date[0].'-'.$date[1]; } // make it a csv for VALUES $value = implode("','", $line); // there's always an extra space at the end for some reason - get rid of it - it bothers me $value = trim($value); // put quotes around each value $value = "'".$value."'"; // New Bit! // We should have 7 fields to put into the database if (count($line) != 7) { echo "Invalid line in CSV: Values = ".$value; } else { // Put this one line into the database $query = $sql."(".$value.")"; if (($res = $db->query($query)) === false) { echo "<p>Could not import data. ({$db->error})</p>"; } else { echo "<p>Data successfully imported!</p>"; } } }
  2. SQL is designed to be almost like reading plain English (although in many cases, it soon gets complicated). So, a simple SELECT query is: SELECT (fields) FROM (table) WHERE (field)=(value); So, looking at the query you currently have: $query = "SELECT Manufacturer FROM manufacturer WHERE id='".$id."'"; and your error message: We can see that 'id' is not the right field name to use in your query. You said that the ID in your table has the fieldname Model_id (and Manufacturer_id for the other table), so you should be able to just change your query from this: $query = "SELECT Manufacturer FROM manufacturer WHERE id='".$id."'"; to this: $query = "SELECT Manufacturer FROM manufacturer WHERE Model_id='".$id."'"; Hope that helps
  3. To be honest, I never use Boolean data types in MySQL - It's usually easier to use an ENUM type, with yes and no values instead. I've had problems with Boolean SQL values in the past, but the enum type works great. If you are setting up your database in phpMyAdmin, set the field type as ENUM, and in the 'Length/Values' box, enter: 'no','yes' I don't quite understand where the checkboxes you want are to go though. Do you want a checkbox after the Delete and Update links? One for each item in the $existingItems list? What are the checkboxes to do? As far as I understand, If it's ticked, you want to set the database field to 'yes', and if not set, you want to set the database field to 'no'? You can do something like: echo "<input type=\"checkbox\" name=\"c".$value['id']."\" value=\"1\">"; then, to read those values, you would do something like: for ($i=0; $i<$max_id; $i++) { $complete[$i] = ($_GET["c".$i] == 1) ? "yes" : "no"; } You would now have an $complete array of yes and no values, which you could put into your database
  4. Error messages are usually quite informative if you know how to read them. Unfortunately, it's quite hard to figure out what's going wrong when you say 'I get errors' but don't say what those errors are. Just to clarify though, the two functions should be in your PHP script before they are called (usually I put all functions at the top of the PHP script). What are the error messages you are getting?
  5. It seems what you are looking for is a simple function or two to grab the details from the database. These can sit at the top of your PHP script: function get_model_by_id($id) { $query = "SELECT model FROM mytable WHERE id='".$id."'"; $res = mysql_query($query) or die("ERROR: ".mysql_error()." (in ".__FILE__." at line ".__LINE__.")"); $c = mysql_num_rows($res); if ($c != 1) { die ("ERROR: Model not found in get_model_by_id (".__FILE__." at line ".__LINE__.")"); } $row = mysql_fetch_assoc($res); $ret = $row['model']; return $ret; } function get_model_by_id($id) { $query = "SELECT make FROM mytable WHERE id='".$id."'"; $res = mysql_query($query) or die("ERROR: ".mysql_error()." (in ".__FILE__." at line ".__LINE__.")"); $c = mysql_num_rows($res); if ($c != 1) { die ("ERROR: Make not found in get_model_by_id (".__FILE__." at line ".__LINE__.")"); } $row = mysql_fetch_assoc($res); $ret = $row['make']; return $ret; } NOTE: You may need to edit the $query lines to suit your own database Now you can just call these functions when you need to: $make_id = $_POST['make']; $model_id = $_POST['model']; $make_name = get_make_by_id($make_id); $model_name = get_model_by_id($model_id);
  6. So, you want it at the 1st, 5th, 9th, 13th, etc...? To do that, you would do something like: if ($count % 4 == 1) { or if (($count - 1) % 4 == 0) { or if (!(($count - 1) % 4)) {
  7. You are echoing the contents of your database, not an image... I assume that 'foto' in your database is the filename of the image, not the actual image data...? Instead of 'echo', try readfile: $content = $row['foto']; header('Content-type: image/jpg'); readfile($content);
  8. I don't see why you need the first 'if' - this does exactly the same as your code above: <?php // determine alpha / omega class // do this if ($count % 4 == 0) { // do that } ?> Another way of doing the same would be: if (!($count % 4)) { but either method is fine, and yours is easier for most coders to understand. You also didn't specify a loop in your code at all, so I'm assuming it's there...
  9. In your first bit of code: $query = "UPDATE ".$DBPrefix."users SET balance = balance + 5 WHERE refer = '".mysql_real_escape_string($referName)."'"; $res = mysql_query($query); $system->check_mysql($res, $query, __LINE__, __FILE__); $gateway_data = mysql_fetch_assoc($res); $referName = $gateway_data['refer']; $query = "UPDATE ".$DBPrefix."users SET refer = '' WHERE nick = '".mysql_real_escape_string($referred)."'"; $res = mysql_query($query); $system->check_mysql($res, $query, __LINE__, __FILE__); $gateway_data = mysql_fetch_assoc($res); $referred = $gateway_data['nick']; You seem to be setting the $query with variables that you create afterwards. So, in the first query: $query = "UPDATE ".$DBPrefix."users SET balance = balance + 5 WHERE refer = '".mysql_real_escape_string($referName)."'"; $referName doesn't exist, so if you were to echo $query; , it would show: UPDATE mydb.users SET balance = balance + 5 WHERE refer = '' What you need to do is to get the referrer from the database first, I'd do this by adding a function: function getReferName($n) { $query = "SELECT refer FROM ".$DBPrefix."users WHERE nick = '".mysql_real_escape_string($n)."'"; $res = mysql_query($query); $system->check_mysql($res, $query, __LINE__, __FILE__); /* Should only be one result */ $row = mysql_fetch_assoc($res); $referName = $row['refer']; return ($referName); } Now, you can use that in your script like: // Assume we have $userName $referName = getReferName($userName); // Who referred userName? $query = "UPDATE ".$DBPrefix."users SET balance = balance + 5 WHERE refer = '".mysql_real_escape_string($userName)."'"; $res = mysql_query($query); $system->check_mysql($res, $query, __LINE__, __FILE__); $query = "UPDATE ".$DBPrefix."users SET refer = '' WHERE nick = '".mysql_real_escape_string($referName)."'"; $res = mysql_query($query); $system->check_mysql($res, $query, __LINE__, __FILE__); Note that I removed the $gateway_data lines from each query - they seemed to be doing nothing useful. Put them back in only if you needed the variables after this point...
  10. Are you on a shared server? You said that you only have 8 people showing on your site, but you get the following, which all seem higher than they should be: Connections = 121771 Threads_connected = 41 Threads_created = 121750 Threads_running = 36 I don't know (and maybe someone else here could clarify) if SHOW STATUS gives results for the current SQL user, or for the SQL server (and therefore all users on the server). If your site host has a shared MySQL server, it could be possible that someone else has a busy website that's affecting yours. To explain the above values (as far as I understand them, but I'm not an expert): Uptime How long your server has been continually online (in seconds), so in your case, 19 hours, 16 minutes and 44 seconds. Connections How many times a connection has been made to the SQL server. Threads_connected How many threads (queries) are currently connected, usually a good indication of how busy the server is... Threads_created How many queries have been run since the server was last reset. This is usually slightly lower than 'Connections', as sometimes a connection is made without a query being done. Threads_running How many queries are currently being processed. This should be low (but at least 1, as your SHOW STATUS query will be counted) As a comparison, here's the info I just grabbed from my server: Connections 1012001 Threads_cached 52 Threads_connected 5 Threads_created 57 Threads_running 2 Uptime 478789 My uptime is 5½ days, so my 'connections' value is much higher, but as you can see, current threads (Threads_connected and Threads_running) are lower than yours. Sorry, this isn't my area of expertise, so I have no idea what's causing the problem or how to fix it. The SQL server being shared, and another site causing the problem is only a guess, but I can't think of anything else that might cause it.
  11. Grabbing (and outputting) all the SQL Status values whould be done like: <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); $result = mysql_query('SHOW STATUS', $link); while ($row = mysql_fetch_assoc($result)) { echo $row['Variable_name'] . ' = ' . $row['Value'] . "\n"; } ?> So, I believe the SQL you want to just get Threads_connected would be something like: $query = "SHOW GLOBAL STATUS LIKE 'Threads_connected'"; Of course, monitoring the Threads_connected value won't stop the 'Too Many Connections' error, but if it's getting close to max, you may be able to just output a 'Sorry the site is currently busy, try later' type message instead.
  12. How about something like: if ($pages >= 1 && $page <= $pages) { for ($x=1; $x<=$pages; $x++) { $link = '<a href="?page='.$x.'">'.$x.'</a>'; if ($x == $page) echo '<strong>'.$link.'</strong> '; // Current Page else if (($x == 1) && ($page>5)) echo $link.' ... '; // First page else if (($x == $pages) && ($page<$pages-5)) echo ' ... '.$link; // Last Page else if ($x >= ($page-5)) && ($x <= ($page+5)) echo $link.' '; // Within 5 pages } } So, if you are on page 12 of 482, it would display: and page 3 should display like:
  13. As I stated above, your problem is here: if ($pages >= 1 && $page <= $pages) { for ($x=1; $x<=$pages; $x++) { echo ($x == $page) ? '<strong><a href="?products='.$x.'">'.$x.'</a></strong> ' : '<a href="?products='.$x.'">'.$x.'</a> '; } } You are setting your page links as ?products=$x But, your PHP script, on this line: $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; is asking for a GET variable called page. This doesn't exist, so page is always equal to zero. You can either: Set your href urls to ?page=$x or $_GET['products']
  14. First place I'd look for a problem is here: $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; How are you passing 'page' to the PHP script? Is it possible that something is going wrong and page isn't being passed, so therefore you are always on page 1? Are your 'prev' and 'next' buttons correctly pointing to: myscript.php?page=x Personally, I always prefer to use $_REQUEST['page'], but that is just laziness, as it allows me to use GET or POST variables without code changes. Not suggesting you use $_REQUEST, just an option...
  15. This is probably a throw-back to C coding. If you suddenly begin using an uninitialised variable in C, it could contain anything (whatever happened to be stored at that memory location when the program began running). In C: void main(void) { int i; i += 1; printf("%d", i); } could output anything, so the correct method would be: void main(void) { int i=0; i += 1; printf("%d", i); } which would always output 1. Of course, PHP auto-sets all new variables to NULL when they are first used, so it's not so much of a concern. It is still considered good coding practise to initialise all variables though...
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.