I have a PHP file and a C# app.
In the php file it returns a Array of the rows that i selected from a mysql table.
I want to read that Array and deserialize it so the result of a row can be parsed inside a textBlock.
My PHP script:
<?php
mysql_connect("-----", "------", "-----") or
die("Could not connect: " . mysql_error());
mysql_select_db("------");
$list = array();
$query = "SELECT SongName,ArtistName,Thumbnail,MediaLink FROM Library";
$resource = mysql_query($query);
while($row = mysql_fetch_assoc($resource))
{
$list['SongName' . (1 + count($list))] = $row['SongName'];
}
$list = array('row' => count($list)) + $list;
echo json_encode(array($list));
?>
How do i get the ArtistName, Thumbnail and medialink inside the array?
How do i read this in C# so my textblock will show: ArtistName.
Technologies:
Windows 10 Store apps(C#, XAML)
If somebody could help that'd be great,
Christos K
Simple, you pull all the required data from the database and return it as an array, just put the whole $row into the array you are about to return
<?php
mysql_connect("-----", "------", "-----") or
die("Could not connect: " . mysql_error());
mysql_select_db("------");
$list = array();
$query = "SELECT SongName,ArtistName,Thumbnail,MediaLink FROM Library";
$resource = mysql_query($query);
while($row = mysql_fetch_assoc($resource))
{
$list[] = $row;
}
echo json_encode($list);
?>
Now you should receive all the data you want in the javascript. Just use the javascript debugger to see what it looks like
Related
How to find duplicate values in Excel and export rows to another sheet using power shell?
I had an Excel sheet with multiple rows and column lets say from "A" to "k". I need to find duplicate rows only if values in all the columns in a row are unique. And the script should ignore D,E,F columns even though those column's values are same.
The script should also copy all those duplicate rows and should paste in a new excel file.it should also copy header row and source of duplicate rows and Also attaching a sample image of input file(the output file should also be same as input in this input case because it should also copy source duplicate rows). I had tried a code but it is throwing an error..please look into that and give me a solution for the code.
code:
# The Text OleDB driver is only available in PowerShell x86. Start x86
shell if using x64.
# This has to be the first check this script performs.
if ($env:Processor_Architecture -ne "x86") {
Write-Warning "Switching to x86 shell"
&"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe"
"$PSCommandPath $args"; return
}
# Change to your CSV file name, must end in .csv or .tsv
$csvfile = "C:\files\A01modcsv.csv"
# Does the first row contain column names?
$firstRowColumns = $True
# What's the delimiter? Use `t for tabbed.
$csvdelimter = "`t"
$firstRowColumns = $true
$checkColumns = "A"
$datasource = Split-Path $csvfile
$tablename = (Split-Path $csvfile -leaf).Replace(".","#")
switch ($firstRowColumns) {
$true { $firstRowColumns = "Yes" }
$false { $firstRowColumns = "No" }
}
$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
[void][Reflection.Assembly]::LoadWithPartialName("System.Data")
# Setup OleDB using Microsoft Text Driver.
$connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=$datasource;Extended Properties='text;HDR=$firstRowColumns;FMT=Delimited($csvdelimter)';"
$conn = New-Object System.Data.OleDb.OleDbconnection
$conn.ConnectionString = $connstring
$conn.Open()
$cmd = New-Object System.Data.OleDB.OleDBCommand
$cmd.Connection = $conn
# Perform select on CSV file, then add results to a datatable using ExecuteReader
$sql = "SELECT $checkColumns, COUNT(*) as DupeCount FROM [$tablename] GROUP BY $checkColumns HAVING COUNT(*) > 1"
$cmd.CommandText = $sql
$dt = New-Object System.Data.DataTable
$dt.BeginLoadData()
$dt.Load($cmd.ExecuteReader([System.Data.CommandBehaviour]::CloseConnection))
$dt.EndLoadData()
$totaltime = [math]::Round($elapsed.Elapsed.TotalSeconds,2)
# Get Total Row Count
$cmd.CommandText = "SELECT COUNT(*) as TotalRows FROM [$tablename]"
$totalrows = $cmd.ExecuteScalar()
$conn.Close()[enter image description here][1]
# Output some stats
$dupecount = $dt.Rows.Count
Write-Host "Total Elapsed Time: $totaltime seconds. $dupecount duplicates found out of $totalrows total rows. You can access these dupes using `$dt." -ForegroundColor Green
I am getting an error in the menctioned code at "$dt.Load($cmd.ExecuteReader([System.Data.CommandBehaviour]::CloseConnection))" command....can any help me in solving this error.Thank you.
I simply want to execute the following Mysql statement
SET #a = 1;SELECT #a;
with MySql.Data.MySqlClient
[System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$password = 'mypassword'
$sql = "SET #a = 1;SELECT #a;"
$server = 'localhost'
$port = 3306
$database = 'test'
$User = 'root'
$conn=new-object MySql.Data.MySqlClient.MySqlConnection
$connectionstring = "Server=$Server;Port=$port;Database=$DataBase;Uid=$User;Pwd=$Password;allow zero datetime=yes"
$conn.ConnectionString = $connectionstring
$conn.Open()
$cmd=new-object MySql.Data.MySqlClient.MySqlCommand($sql,$conn)
$ds=New-Object system.Data.DataSet
$da=New-Object MySql.Data.MySqlClient.MySqlDataAdapter($cmd)
$da.fill($ds)
$conn.close()
$ds.tables[0]
I get a fatal error.
When I replace $sql by either
$sql = "SELECT DATABASE();"
or
$sql = "SELECT 1;"
I get the expected result.
I found this question, but it doesn't solve my problem.
I'm trying to port SQLIse (a part of the SQLPSX project ) to the MySQL version MySQLIse.
I want to process any simple valid mysql statements.
EDIT:
I was trying to run parts of the sakila-schema.sql the mysql demo database install script which runs by something like
mysql> SOURCE
C:/temp/sakila-db/sakila-schema.sql;
I found the solution in this blog
I have to add
;Allow User Variables=True
to the connection string:
$connectionstring = "Server=$Server;Port=$port;Database=$DataBase;Uid=$User;Pwd=$Password;allow zero datetime=yes;Allow User Variables=True"
works. I tested it with version 6.3.6.0. of MySql.Data.
I need to store on my SQL Server DB many records in the same column and I use XML field for this.On the backend I have code that reads/writes the xml records and inserts the data in a table.The table is of variable size. I have problem in reading the fields from the table and insert them into an xml variable.
What I need to get is :
<BigTag>
<SmallTagName1> something</SmallTagName1>
<SmallTagName2> thanksForanswering</SmallTagName2>
<SmallTagName3> youareawesome</SmallTagName3>
</BigTag>
The table is:
smalltagTable[] = {something,thanksForanswering,youareawesome,....}
here is my code:
XElement WidgetListE1 = null;
WidgetListE1 = new XElement("BigTag" );
string smalltagname = "smalltag ";
string smalltagdata;
for(int i=0;i<smalltagTable.Length; i++ )
{
SmallTagname = "smallTagName " + i;
smalltagdata = smalltagTable[i];
WidgetListE1.Add(new XElement(smallTagName.ToString(),SmalltagData.ToString() ));
}
string a2 = WidgetListE1.ToString();
The problem is that this code hits the error :
The ' ' character, hexadecimal value 0x20, cannot be included in a name.
on the line :
WidgetListE1.Add(new XElement(smallTagName.ToString(),SmalltagData.ToString() ));
I post this Just to make sure that the answer appears clearly. Credits go to Panagiotis Kanavos. The problem was that there was a space in the tag of the xml. instead of being :
<smalltag 1> something </smalltag 1>
It should be
<smalltag1> something</smalltag1>
how to check for duplicate entry on a form using php?my front end is c#windows application
I am adding a new user to database using c# and php file.
I have created the following:
my project in c# (form1):
c# coding:
private void btncreate_Click(object sender, EventArgs e)
{
var request = (HttpWebRequest)WebRequest.Create("http://***.**.***.***/response.php");
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
using (var stream = request.GetRequestStream())
{
var buffer = Encoding.UTF8.GetBytes("userid= " + txtUserid.Text + " & password=" + txtConformpassword.Text + " & first_name=" + txtFirstname.Text + " & last_name=" + txtLastName.Text + "& role= " + cmbRole.Text + "& active=" + cmbActive.Text + "");
stream.Write(buffer, 0, buffer.Length);
}
var response = (HttpWebResponse)request.GetResponse();
string result = String.Empty;
using (var reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
txtMessage.Text = result;
}
my PHP file :response.php
<?php
$uid = $_POST['userid'];
$pass = $_POST['password'];
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$rol = $_POST['role'];
$act = $_POST['active'];
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql= "INSERT INTO aster(userid,password,first_name,last_name,role,active)
VALUES('$uid', '$pass', '$fname','$lname','$rol','$act');";
if ($conn->query($sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
it add data successfully but when i enter the same userid in textbox and click create button i need to get an error message in txtmessage as userid already exists.
i search through googling but i did not got any thing about duplicate entry please refer me some coding
You could extend your response.php to do a select statement before the insert. If you are able to select a record with the given ID, then yield a response (essentially an error code) which your C# application will then interpret and display to the user.
As a side note, since the ID field appears to be numeric, you can simply omit it from your form and let the DB increment it automatically. This would also mean that the user will not have to guess which ID values are available.
you just want that the ID which you have entered should not be inserted at the next time . for that you just need to do one thing. make your id as a primary key in your database table creation , after doing this you will be not able to insert that id again or duplicate entries are not allowed in a primary key.
lemme know if it is useful.
It may not help as much, but I think my explanation will point you to the right direction.
What I would do is:
I would use SQL query to check if record exist and if exist then print "Record Exists" Else "Record does not exist". Below the code I post, 'Justin Yen' on PHP posting a quote, make sure you assign that to the field of inputting the username. In your case it's $uid.
On PHP, assign the result of sql query to a variable.
Create a string variable "Record Exists" and "Record does not exist".
compare the string variable you create on step 3 to the result on step 2.
I don't know any PHP coding, but you can try to find a way to compare string. But here's an example of checking record exist in SQL:
Declare #Username varchar(MAX) = 'Justin Yen'
IF EXISTS(select Username from EmployeTable where Username = #Username)
begin print 'Record Exists' end
else
begin print 'Record does not exist' end
Now all you have to do is if variable match then prompt "Cannot create record because Record created." Else use the add record codes you have in php.
You can simply try to add the contraints(UNIQUE/PRIMARY) to the column 'userid'.
ALTER TABLE aster ADD CONSTRAINT MyUniqueKey UNIQUE KEY(userid)
I am currently making a Register Page, I coded the layout with html, and I get value from the html input(<input)
and insert into database. It did sucessfully inputed into the database but when I "select * from dbo.user" the value are showing blank
<?php
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("mydbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
mysql_free_result($result);
?>
SOURCE: http://au2.php.net/manual/en/function.mysql-fetch-assoc.php