Page 1 of 1

Uniq ID via PHP Script

Posted: Mon Aug 18, 2014 10:18 pm
by jkdev
Following the hints given in
http://forums.nubuilder.cloud/viewtopic.p ... qid#p12662

I wanted to create a generic form where I can specify the table name to be modified via PHP script.

1. Criteria form looks like this
Code: addunique
Description: select the table to be modified for uniqid
Table: addunique
Primary Key: addunique_tablename
SQL: select * from addunique
2. PHP Code
Code : AlterUniqID
Description : Alter a table to add uniqid field
Group : admin
Criteria Form : addunique
Allow Non Secure Access : Yes
3. PHP Code

Code: Select all

echo 'Starting to alter the table';		

nuRunQuery('ALTER TABLE #addunique_tablename#  ADD #addunique_tablename#_id VARCHAR(25) NOT NULL FIRST,  ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY AFTER account_id');  //-- this auto populates id with numbers then
		
$t = nuRunQuery('SELECT * FROM #addunique_tablename#');
		
while($r = db_fetch_object($t)){
		
	$id = nuID();
	nuRunQuery("UPDATE #addunique_tablename# SET #addunique_tablename#_id = '$id' WHERE id = '$r->id'");              //--  update account_id to a unique string (better in so many ways than using numbers)
		   
}
		
nuRunQuery("ALTER TABLE #addunique_tablename# DROP id");
nuRunQuery("ALTER TABLE #addunique_tablename# ADD PRIMARY KEY(#addunique_tablename#_id)");
		
print 'Done!';

I am trying to use the name of the table as a Hash Variable.

When I "Run PHP Code" nothing happens. No error message either.


Can you help me to figure out what I am doing wrong here please?

Re: Uniq ID via PHP Script

Posted: Tue Aug 19, 2014 1:03 am
by admin
jkdev,

Comment out each line until it works, then you'll find the bug.

Steven

Re: Uniq ID via PHP Script

Posted: Tue Aug 19, 2014 8:24 am
by jkdev
Steven,
Thanks for the advice.

I wanted to understand wheher I am doing something fundamentally wrong.

Update:
Never mind. The errors below were due to extra (non visible) characters that got introduced in to PHP code when doing cut and paste. The eval() of PHP code throws out errors due to them.

I finally managed to get it working after retyping the code cleanly.

For the benefit of others I am giving the code in a separate posting at the end of this thread (Now I really understood how to make nuBuilderPro run my custom PHP scripts).


==================================================================================

1. I managed to get the Hash Variables sorted out

example: following worked

Code: Select all

$s="ALTER TABLE ".#addunique_tablename#." ADD ".#addunique_tablename#."_id VARCHAR(25) NOT NULL FIRST,  ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY AFTER ".#addunique_tablename#."_id";	
echo $s."<br>";
2. However I am still getting an error when trying to execute nuRunQuery. Even a direct query like this fails.

Code: Select all

$t = nuRunQuery("SELECT * FROM district");

Code: Select all

[Tue Aug 19 11:40:30 2014] [error] [client ::1] PHP Parse error:  syntax error, unexpected $end in /var/www/alu/nurunphp.php(18) : eval()'d code on line 4, referer: http://localhost/alu/
I want to know whether I am doing something now allowed ie. to run a query against just about any table in the code when the code is associated with a criteria table name which is different?

Some further hints will be much appreciated.

Re: Uniq ID via PHP Script

Posted: Tue Aug 19, 2014 10:15 am
by jkdev
Here is the code which will alter any table and add <tablename>_id field with a unique key value.

You can now import a CSV file with data (remember to add header row with field name in the CSV file) using phpmyadmin and then use this PHP script to modify the table to be suitable for nuBuilderPro.

Code: Select all

echo 'Starting to alter the table '.#addunique_tablename#."<br>";

$sql="ALTER TABLE ".#addunique_tablename#." ADD ".#addunique_tablename#."_id VARCHAR(25) NOT NULL FIRST,  ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY AFTER ".#addunique_tablename#."_id";
echo $sql."<br>";
nuRunQuery($sql);

echo "Reading the table again ...<br>";

$sql="SELECT * FROM ".#addunique_tablename#;
echo $sql."<br>";
$t = nuRunQuery($sql);

while($r = db_fetch_object($t)){
  $id = nuID();
  $sql="UPDATE ".#addunique_tablename#." SET ".#addunique_tablename#."_id = '$id' WHERE id = '$r->id'";
  //echo $sql."<br>";
  nuRunQuery($sql);//--  update <tablename>_id to a unique string
}

$sql="ALTER TABLE ".#addunique_tablename#." DROP id";
echo $sql."<br>";
nuRunQuery($sql);

$sql="ALTER TABLE ".#addunique_tablename#." ADD PRIMARY KEY(".#addunique_tablename#."_id)";
echo $sql."<br>";
nuRunQuery($sql);

print 'Done!<br>';
Thanks Steven for all the help and the hints.

Re: Uniq ID via PHP Script

Posted: Wed Aug 20, 2014 10:03 am
by admin
Cool.