Category Archives: MySql

Open source database and at the same time the most popular in the world

MySQL rand function

If you need to add some random number to some of the mysql columns then here it goes…

UPDATE tableSET column = FLOOR( 1 + RAND( ) *100 )

The last number (100) means you are about to add any value until 100. So 99, 1, 5, 33, 45…any between 1 and 100.
If you put 10 instead of 100 then you will get between 1 and 10 (2, 5, 6).

Returns a random floating-point value v in the range 0 <= v < 1.0.

RAND

That’s it.

MariaDB/MySql server – load data infile

When using php7.2 and onward make sure you are having:
mysqli.allow_local_infile = On
in php.ini setup or uncommented.

Otherwise this will not work. I’ve spent 2 days trying to make it working on mariaDB config file because few years ago I had the same problems and most of it were on DB end (optimization) but now this totally surprised me. It was on php end.

This is really nice stuff. Check it

https://stackoverflow.com/questions/13016797/load-data-local-infile-fails-from-php-to-mysql-on-amazon-rds?rq=1

Please not…ini_set is not always working so you might need to change allow_local_infile directly to the server’s php.ini for specific php version. I’m using directadmin almost every day. Have 3 php versions installed and testing everything there.

PHP Mysql extending class

So, lets say you want to extend php/mysql class we were talking about earlier.

class nastavak extends baza {

private $nast;
private $drugi;

public function __construct(){
echo "This is the extended class";
parent::__construct("","root","","test");
parent::sqlquery("select * from imena");
parent::stampaj();
}
}

What happened here? Well, we have added another class on top of baza class and that way we have extended it. It means new class (extended) has everything from parent class (baza) and what we have added inside of it (nastavak).

Overriding in the next article.

PHP Mysql class for websites

I had some free time and decided to write one simple php class for manipulating mysql. It’s really simple code, it can help new programmers learn how to build their own classes.

<?php

class baza {

 private $username;
 private $password;
 private $local;
 private $errors;
 private $resource1;
 private $db_sel;
 private $sqlupit;

 public function __construct($l="localhost",$u, $p, $db_selected){
 $this->username=$u;
 $this->password=$p;
 $this->local=$l;
 $this->db_sel=$db_selected;
 $this->conn();
 $this->db_selected();
 $this->listaj_db();
 }

 private function conn(){
 $this->resource1=mysql_connect($this->local,$this->username,$this->password);
 if (!$this->resource1){
 echo "Postoji problem sa konekcijom <br />";
 }
 else {
 echo "Sve je super <br />";
 }
 }

 private function db_selected(){
 mysql_select_db($this->db_sel);
 }

 private function listaj_db(){
 $result = mysql_query("SHOW DATABASES"); 
 while ($row = mysql_fetch_array($result)) { 
 echo $row[0]."<br>"; 
 }
 
 }
 
 public function sqlquery($n){
 $this->sqlupit=mysql_query($n);
 }
 
 public function stampaj(){
 //$result = mysql_query("SHOW DATABASES"); 
 while ($row = mysql_fetch_array($this->sqlupit)) { 
 echo $row[1]."<br>"; 
 }

 }
}

$n=new baza("","root","","test");
$n->sqlquery("select * from imena");
$n->stampaj();

?>

Please comment if you have some questions.