I'm writing an application using PHP, to test database connection using PDO and just querying "SHOW DATABASES".
However if I try to connect to database with an unexisting user I get connection as well...
for example if I try this, with the correct user (root):
$conn = new PDO('mysql:host=localhost', 'root', '');
$result = $conn->query("SHOW DATABASES");
while($row = $result->fetchAll()){
var_dump($row);
}
and echo the result I get this:
array(5) { [0]=> array(2) { ["Database"]=> string(18) "information_schema" [0]=> string(18) "information_schema" } [1]=> array(2) { ["Database"]=> string(5) "mysql" [0]=> string(5) "mysql" } [2]=> array(2) { ["Database"]=> string(18) "performance_schema" [0]=> string(18) "performance_schema" } [3]=> array(2) { ["Database"]=> string(10) "phpmyadmin" [0]=> string(10) "phpmyadmin" } [4]=> array(2) { ["Database"]=> string(4) "test" [0]=> string(4) "test" } }
however if I use a random string for the username, like this:
$conn = new PDO('mysql:host=localhost', 'sdghasdgas', '');
$result = $conn->query("SHOW DATABASES");
while($row = $result->fetchAll()){
var_dump($row);
}
I still get results but only this:
array(2) { [0]=> array(2) { ["Database"]=> string(18) "information_schema" [0]=> string(18) "information_schema" } [1]=> array(2) { ["Database"]=> string(4) "test" [0]=> string(4) "test" } }
Thanks for the help.
Related
I have an array that I exploded by Regex, here is a part of the array named $data;
array(6) {
[0]=>
array(1) {
[0]=>
string(77) "Achnanthes brevipes C.Agardh, Syst. Alg.: 1 (1824). / Küçük sucıncığı."
}
[1]=>
array(1) {
[0]=>
string(10) "Achnanthes"
}
[2]=>
array(1) {
[0]=>
string(8) "brevipes"
}
[3]=>
array(1) {
[0]=>
string(8) "C.Agardh"
}
[4]=>
array(1) {
[0]=>
string(21) "Syst. Alg.: 1 (1824)."
}
[5]=>
array(1) {
[0]=>
string(22) "Küçük sucıncığı"
}
}
array(6) {
[0]=>
array(1) {
[0]=>
string(89) "Achnanthes cocconeiformis Mann, U.S. Nat. Mus., Bull. 6: 182 (1925). / Top sucıncığı."
}
[1]=>
array(1) {
[0]=>
string(10) "Achnanthes"
}
[2]=>
array(1) {
[0]=>
string(14) "cocconeiformis"
}
[3]=>
array(1) {
[0]=>
string(4) "Mann"
}
[4]=>
array(1) {
[0]=>
string(36) "U.S. Nat. Mus., Bull. 6: 182 (1925)."
}
[5]=>
array(1) {
[0]=>
string(17) "Top sucıncığı"
}
}
array(6) {
[0]=>
array(1) {
[0]=>
string(108) "Achnanthes gibberula Grunow, Kongl. Svenska Vetensk.-Akad. Handl. 17(2): 121 (1880). / Kambur sucıncığı."
}
[1]=>
array(1) {
[0]=>
string(10) "Achnanthes"
}
[2]=>
array(1) {
[0]=>
string(9) "gibberula"
}
[3]=>
array(1) {
[0]=>
string(6) "Grunow"
}
[4]=>
array(1) {
[0]=>
string(55) "Kongl. Svenska Vetensk.-Akad. Handl. 17(2): 121 (1880)."
}
[5]=>
array(1) {
[0]=>
string(20) "Kambur sucıncığı"
}
}
and many more..
my regex is:
$turRegex = '/^([^\s]+)[\s]([^\s]+)[\s]([^\s]+)[,][\s]([A-Za-z].+)[\s][\/][\s](.+)[.]/m';
my foreach loop:
foreach ($data as $data) {
if (!empty(preg_match_all($turRegex, $data, $matches))) {
echo "<pre>";
var_dump($matches);
echo "</pre>";
}
}
My table name is "algeaSpecies" and columns are; "id","genusName","speciesEpiteth","author", "publication","TurkishName"
I want to insert this to genusName;
[1]=>
array(1) {
[0]=>
string(10) "Achnanthes"
this to speciesEpiteth;
[2]=>
array(1) {
[0]=>
string(8) "brevipes"
}
and others...
I'm using MySQLi
Thank you
First I noticed you used $data both as array and item in your loop.
In the following code, I assumed that you don't want the first index (the query wasn't clear).
Also, replace the DB connection credentials.
$con = new mysqli('db_host', 'username', 'password', 'db_name');
$boundArray = array_fill(0, 5, null);
$query = "INSERT INTO `algeaSpecies` (`genusName`, `speciesEpiteth`, `author`, `publication`, `TurkishName`) VALUES (?,?,?,?,?);";
$stmt = $con->prepare($query);
$stmt->bind_param("sssss", ...$boundArray);
foreach ($data as $item) {
$i = -1;
foreach ($item as $index) {
if ($i === -1) {
$i++;
continue;
}
$boundArray[$i++] = $index[0];
}
$stmt->execute();
}
$stmt->close();
$con->close();
I need to get some data out of my influxdb database.
My current query is:
SELECT value FROM first,second,third WHERE location = 'somewhere' ORDER BY time DESC LIMIT 1
With this result:
array(3) {
[0]=>
array(2) {
["time"]=>
string(30) "2020-02-06T12:44:49.461551353Z"
["value"]=>
float(8.7572979625)
}
[1]=>
array(2) {
["time"]=>
string(29) "2020-02-06T12:44:48.70683539Z"
["value"]=>
float(22.5172978864)
}
[2]=>
array(2) {
["time"]=>
string(30) "2020-02-06T12:44:48.711272393Z"
["value"]=>
float(43.0572978868)
}
}
To process this information i have to use a while loop of some sort, i am unsure since i cannot find an
example of this online anywhere related to this type of data.
But to make the loop useful i need to know what the measurement name is, if i dont have that the result is quite unusable.
I would require this to be:
array(3) {
[0]=>
array(2) {
["time"]=>
string(30) "2020-02-06T12:44:49.461551353Z"
["measurement"]=>
string(5) "first"
["value"]=>
float(8.7572979625)
}
[1]=>
array(2) {
["time"]=>
string(29) "2020-02-06T12:44:48.70683539Z"
["measurement"]=>
string(6) "second"
["value"]=>
float(22.5172978864)
}
[2]=>
array(2) {
["time"]=>
string(30) "2020-02-06T12:44:48.711272393Z"
["measurement"]=>
string(5) "third"
["value"]=>
float(43.0572978868)
}
}
How can i achieve this and process the results correctly?
This solved my issue:
<?php
include "vendor/autoload.php";
$columns = array('first', 'second', 'third');
$cstrings = implode(",",$columns);
$database = InfluxDB\Client::fromDSN(sprintf('influxdb://xxx:xxx#%s:%s/%s', 'localhost', 8086, 'xxxx'));
$client = $database->getClient();
$result = $database->query("SELECT value FROM $cstrings WHERE location = 'xxx' ORDER BY time DESC LIMIT 1");
foreach ($columns as $column) {
$points = $result->getPoints($column);
print($column." ".$points[0]['value']."\n");
}
I have several file input fields with a common name and a unique name for each file input .The unique name is for some validation purposes.
<input name="file12 ftr_file_uploads[]" class="multi_files file " type="file">
<input name="file10 ftr_file_uploads[]" class="multi_files file " type="file">
<input name="file10 ftr_file_uploads[]" class="multi_files file " type="file">
...............
When trying to upload files in PHP form submit,
the content of $_FILES is as following.
array(2) {
["file1_ftr_file_uploads"]=> array(5)
{ ["name"]=> array(1) { [0]=> string(13) "Jellyfish.jpg" }
["type"]=> array(1) { [0]=> string(10) "image/jpeg" }
["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpx7iId2" }
["error"]=> array(1) { [0]=> int(0) }
["size"]=> array(1) { [0]=> int(775702) } }
["file2_ftr_file_uploads"]=> array(5)
{ ["name"]=> array(1) { [0]=> string(12) "Penguins.jpg" }
["type"]=> array(1) { [0]=> string(10) "image/jpeg" }
["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpN6QWoD" }
["error"]=> array(1) { [0]=> int(0) }
["size"]=> array(1) { [0]=> int(777835) } }
}
The array key name changed to the concatenated names of the file input field.I need the names to be ftr_file_uploads rather than fileIDnumber_ftr_file_uploads.
I have done like the following.
foreach($_FILES as $keyval=>$value)
{
$_FILES['ftr_file_uploads'] = $_FILES[$keyval]; //removed
$_FILES['ftr_file_uploads'][] mentioned in the answer
unset($_FILES[$keyval]);
}
When i am using like this,I am getting the result like this.
array(1) {
["ftr_file_uploads"]=>
array(5) {
["name"]=>
array(1) {
[0]=>
string(14) "Lighthouse.jpg"
}
["type"]=>
array(1) {
[0]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(1) {
[0]=>
string(14) "/tmp/phpLdslxb"
}
["error"]=>
array(1) {
[0]=>
int(0)
}
["size"]=>
array(1) {
[0]=>
int(561276)
}
}
}
I need the result like this.
array(1) {
["ftr_file_uploads"]=>
array(5) {
["name"]=>
array(2) {
[0]=>
string(14) "Hydrangeas.jpg"
[1]=>
string(5) "w.jpg"
}
["type"]=>
array(2) {
[0]=>
string(10) "image/jpeg"
[1]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(2) {
[0]=>
string(14) "/tmp/phpKMwmH1"
[1]=>
string(14) "/tmp/phpwwHU9G"
}
["error"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
["size"]=>
array(2) {
[0]=>
int(595284)
[1]=>
int(879394)
}
}
}
foreach ($_FILES as $name => $file) {
$_FILES['ftr_file_uploads'][] = $file; // [] means add $file to $_FILES['ftr_file_uploads'] array
unset($_FILES[$name]); // remove element from $_FILES
}
need some help with splitting mysql single column query array into different php variables here.
example:
here's the query, it's pretty simple to be honest.
but, i'm running out of ideas right now.
$string = "select Description from tblQuestion
where Employeeid = '$param'"
$query = $this->db->query($string);
$result = return $query->result_array();
btw, i am using Codeigniter and i tried to var_dump and the results are like this.
array(9) { [0]=> array(1) { ["Description"]=> string(5) "tidak" } [1]=> array(1) { ["Description"]=> string(5) "tidak" } [2]=> array(1) { ["Description"]=> string(5) "tidak" } [3]=> array(1) { ["Description"]=> string(5) "tidak" } [4]=> array(1) { ["Description"]=> string(5) "tidak" } [5]=> array(1) { ["Description"]=> string(5) "tidak" } [6]=> array(1) { ["Description"]=> string(5) "tidak" } [7]=> array(1) { ["Description"]=> string(5) "tidak" } [8]=> array(1) { ["Description"]=> string(5) "tidak" } }
i tried to use json_encode and the result is
[{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"}]
the question is.
how do i convert this stack of arrays into different variables like this?
$var0 = "tidak";
$var1 = "tidak";
$var2 = "tidak";
$var3 = "tidak";
and on and on....
thanks in advance.
cheers!
Put the results in a foreach loop and assign the values to a dynamic variable...
sample code like,
foreach($results as $key=>$val){
$str = 'var'.$key;
$$str = $val['Description'];
}
echo $var0;
I am obtaining a json object using the following:
$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test
var_dump($data);
This gives me something like this:
array(2) { ["ok"]=> bool(true) ["result"]=> array(1) { [0]=> array(2)
{ ["update_id"]=> int(44893465) ["message"]=> array(5) {
["message_id"]=> int(16) ["from"]=> array(3) { ["id"]=> int(29595794)
["first_name"]=> string(3) "Bob" ["username"]=> string(14) "Bobo" }
["chat"]=> array(3) { ["id"]=> int(29595794) ["first_name"]=>
string(3) "Bob" ["username"]=> string(14) "Bobo" } ["date"]=>
int(1435354253) ["text"]=> string(7) "/q 3.33" } } } }
I would then like to add certain values into variables. For example I would like to extract username, text, message_id, etc
But no matter what I try my variables are empty:
//let's test it
echo "Username: " . $data[1][0]["username"];
//another test
echo $data->username;
Neither of these are working and my research has not helped me find a solution. I am stumped on this one.
Any pointers in the right direction would really be appreciated.
array(2) {
["ok"]=> bool(true)
["result"]=> array(1)
{
[0]=> array(2)
{
["update_id"]=> int(44893465)
["message"]=> array(5)
{
["message_id"]=> int(16)
["from"]=> array(3)
{
["id"]=> int(29595794)
["first_name"]=> string(3) "Bob"
["username"]=> string(14) "Bobo"
}
["chat"]=> array(3)
{
["id"]=> int(29595794)
["first_name"]=> string(3) "Bob"
["username"]=> string(14) "Bobo"
}
["date"]=> int(1435354253)
["text"]=> string(7) "/q 3.33"
}
}
}
}
You are using wrong array index. $data[1][0]["username"]; not exists.
$data["result"][0]["message"]["from"]["username"];
$data["result"][0]["message"]["chat"]["username"];
This will give you the proper username
$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test
echo $data["result"][0]['message']['from']['username'];
output
Bobo