Mostrando entradas con la etiqueta PHP. Mostrar todas las entradas
Mostrando entradas con la etiqueta PHP. Mostrar todas las entradas

sábado, 5 de mayo de 2012

Check if record exists in MySQL table with php

So Hello again,

this time I come with a little nice code snippet, that will help you to easily check if a record exists in a MySQL table, so for example I will take the task "check if a username exists in the users table"

for this you will need two functions:


function connect(){ 
  include('config.php'); //where you have your db stuff saved
  $link = mysql_connect($db_host, $db_user, $db_pass);
  if(!$link){
   die('ERROR: 001');
  }
  return $link;
 }


this can be replaced by your favourite way to connect to the database, so next function:



function check($table, $fields, $values) { 
  if((count($fields) > count($values)) || count($values) > count($fields)) die('ERROR 004');
  include('config.php');
  $link = connect();
  mysql_select_db($db_name);
  $check_query = 'SELECT ';
  for($i=0;$i<count($fields);$i++){
   $check_query .= $fields[$i].',';
  }

  $check_query = rtrim($check_query, ',');
  $check_query .= ' FROM '.$table.' WHERE ';

  for($i=0;$i<count($fields);$i++){
   $check_query .= $fields[$i].'=' .$values[$i] . ' AND ';
  }

  $check_query = substr($check_query, 0, -4).';';
  $result = mysql_query($check_query);
  if(!$result){
   die('ERROR: 005 ');
  }

  $count = mysql_num_rows($result);
  if($count > 0) die('username taken!');
}

here there are 3 parameters:

  • Table: the table name you want to query
  • Fields: the fields you want to query
  • Value: the values you want to query
notice that fields and values must have the same count() otherwise it wont work!

so the usage should be:


check('users', array('username'), array('\''.$username.'\''));

if you want to compare more values, just make the arrays bigger and dont forget the " ' " in varchar or texts.

this is an easy way to kill the pain of writting querys over and over again. I hope it helps you out.

Happy codding.


sábado, 19 de noviembre de 2011

Extremely large data bulk via php

Hello,

after a long time without posting anything here, I come with a post that might result usefull to some people.

The thing is that I found myself with a xls sheet with about 50,000 registers, that my client needed uploaded to his personal server, he gave extra-restricted access to a ftp server and phpMyAdmin, and he just wont understand how hard is to upload that amount of registers without seeing a "fatal error" about memory limits or timeouts.

Maiking it quick and to the point, if you have a large execution php script, and by large I mean by size and time, this two codelines can save your life:


ini_set('memory_limit', '-1');
set_time_limit(0);


yes, you gussed it, the first line is to override and set no memory limit, and the second one is to override the time limit.

you need to be responsable using this, because depending on the power of your server your site can slow down significantly while processing the bulk load.

the other option is to get navicat pro this little badass will transform your xls sheets to a MySQL valid file, oh yeah!, then you can use phpMyAdmin, to upload it, for this I recomend to zip the generated script, and use the import option, of course you will get a timeout, memory exausted and shit, BUT the cool thing about phpMyAdmin is that it will automatically continue where it left if you upload the file again without navigating to other page.

Well, I hope you can migrate your data succesfully :)

Happy Coding!