So, Im having a lot of trouble with executing a query in PHP. It executes well in phpmyadmin and gives me a neat list of results.
Here is the query I inserted into phpmyadmin:
SELECT RIGHT(`Pair`, LOCATE('_', REVERSE(`Pair`))-1)
FROM `poloniex`
WHERE LEFT(`Pair`, 3) = 'BTC';
For example an entry in the Column Pair: BTC_NXT
The query should return NXT (everything right of the "_").
Now, when switching over to php while I haven't edited the query at all, I don't get any result.
The dbconnection is already established; no problems on that front.
$query_get_pairs = "SELECT RIGHT(`Pair`,LOCATE('_',REVERSE(`Pair`))-1) FROM `poloniex` WHERE LEFT(`Pair`, 3) = 'BTC'";
$result_get_pairs = mysqli_query($dbc,$query_get_pairs);
var_dump($result_get_pairs) returns an empty array.
Summary:
poloniex is the table name.
Pair is the column name which
contains values like "BTC_NXT". The query should give me NXT.
You are not fetching anything, your code should be like:
$query_get_pairs = "SELECT RIGHT(Pair,LOCATE('_',REVERSE(Pair))-1) FROM poloniex WHERE LEFT(Pair, 3) = 'BTC'";
$result_get_pairs = mysqli_query($query_get_pairs);
$myResult = mysqli_fetch_assoc($result_get_pairs);
var_dump($myResult);
Related
I've run into some trouble trying to execute MSSQL queries using PDO.
I have a table called Computer with 92 computers listed. They all have a respective ID 1-92.
I am using the query: SELECT * FROM Computer to query the table. I want to add associated information to this query so I am outputting it inside a while-loop (this is eventually for table generation).
$queryDiskOperationLog = "SELECT * FROM Computer";
$queryHDDSResult = $connClient -> prepare($queryDiskOperationLog);
$queryHDDSResult->execute();
$queryDiskOperationLogResult = $connClient -> prepare($queryDiskOperationLog);
while ($rowHDDS = $queryHDDSResult->fetch(PDO::FETCH_ASSOC)) {
echo $rowHDDS['Id'];
}
This output works great. I get 1-92 outputted. Trouble begins when I start adding other queries.
Whenever I add another query inside the while-loop the output of the first query is altered and it only outputs uneven numbers (ie. 1,3,5,7...)
I am using the same PDO::FETCH_ASSOC method to get results from the other query and variable names are distinctly different. Do you know what could cause this?
My final code looks like: (note I don't even echo the second query yet.
$queryDiskOperationLog = "SELECT * FROM Computer";
$queryHardDisk = "SELECT * FROM HardDisk";
$queryHDDSResult = $connClient -> prepare($queryDiskOperationLog);
$queryHDDSResult->execute();
$queryHardDiskResult = $connClient -> prepare($queryHardDisk);
$queryHardDiskResult ->execute();
$queryDiskOperationLogResult = $connClient -> prepare($queryDiskOperationLog);
while ($rowHDDS = $queryHDDSResult->fetch(PDO::FETCH_ASSOC)) {
$rowHardDisk = $queryHardDiskResult->fetch(PDO::FETCH_ASSOC);
echo $rowHDDS['Id'];
}
It seems that this php code fetches all the rows in the database, but does not fetch the very first row. If you call to the procedure in the database (see the procedure below) it shows all the rows as expected. The database has four columns and in the code it's used echo to print out the data from the second row, What's happening is that the first record does not display, so it's not fetched I think. So, what could be the problem?
//this is the broken part of my code
$statement = $conn->prepare('call GetImage()');
$statement->execute();
while($row = $statement->fetch()){
$dest_slash = str_replace('-', '/', $row[2]);
$dest_slash_backslash = str_replace(';','\\', $dest_slash);
$replace_root = str_replace($_SERVER['DOCUMENT_ROOT'],'',$dest_slash_backslash);
$row[2] = $replace_root;
$images_paths_with_num[] = array($image_count,$row[2]);
$image_count++;
echo $row[1];
}
This is the stored procedure that I'am using:
CREATE DEFINER=`0901972749`#`%` PROCEDURE `GetImage`()
begin
select imageID,imageName,imagePath,imageText
from Images limit 500;
end
See this procedure (The same as in the question):
CREATE DEFINER=`0901972749`#`%` PROCEDURE `GetImage`()
begin
select imageID,imageName,imagePath,imageText
from Images limit 500;
end
It was needed to add an order by statement in this case "order by imageID" to it as things were not displaying in the right order. To be sure about the data will display in the right order, we need to use an order by clause. But, stored procedures may not be the right solution for this. Sorry about that :)
Like this:
CREATE DEFINER=`0901972749`#`%` PROCEDURE `GetImage`()
begin
select imageID,imageName,imagePath,imageText
from Images limit 500 order by imageID;
end
I have one problem here, and I don't even have clue what to Google and how to solve this.
I am making PHP application to export and import data from one MySQL table into another. And I have problem with these tables.
In source table it looks like this:
And my destination table has ID, and pr0, pr1, pr2 as rows. So it looks like this:
Now the problem is the following: If I just copy ( insert every value of 1st table as new row in second) It will have like 20.000 rows, instead of 1000 for example.
Even if I copy every record as new row in second database, is there any way I can fuse rows ? Basically I need to check if value exists in last row with that ID_, if it exist in that row and column (pr2 for example) then insert new row with it, but if last row with same ID_ does not have value in pr2 column, just update that row with value in pr2 column.
I need idea how to do it in PHP or MySQL.
So you got a few Problems:
1) copy the table from SQL to PHP, pay attention to memory usage, run your script with the PHP command Memory_usage(). it will show you that importing SQL Data can be expensive. Look this up. another thing is that PHP DOESNT realese memory on setting new values to array. it will be usefull later on.
2)i didnt understand if the values are unique at the source or should be unique at the destination table.. So i will assume that all the source need to be on the destination as is.
I will also assume that pr = pr0 and quant=pr1.
3) you have missmatch names.. that can also be an issue. would take care of that..also.
4) will use My_sql, as the SQL connector..and $db is connected..
SCRIPT:
<?PHP
$select_sql = "SELECT * FROM Table_source";
$data_source = array();
while($array_data= mysql_fetch_array($select_sql)) {
$data_source[] = $array_data;
$insert_data=array();
}
$bulk =2000;
foreach($data_source as $data){
if(isset($start_query) == false)
{
$start_query = 'REPLACE INTO DEST_TABLE ('ID_','pr0','pr1','pr2')';
}
$insert_data[]=implode(',',$data).',0)';// will set 0 to the
if(count($insert_data) >=$bulk){
$values = implode('),(',$insert_data);
$values = substr(1,2,$values);
$values = ' VALUES '.$values;
$insert_query = $start_query.' '.$values;
$mysqli->query($insert_query);
$insert_data = array();
} //CHECK THE SYNTAX IM NOT SURE OF ALL OF IT MOSTLY THE SQL PART>> SEE THAT THE QUERY IS OK
}
if(count($insert_data) >=$bulk) // IF THERE ARE ANY EXTRA PIECES..
{
$values = implode('),(',$insert_data);
$values = substr(1,2,$values);
$values = ' VALUES '.$values;
$insert_query = $start_query.' '.$values;
$mysqli->query($insert_query);
$insert_data = null;
}
?>
ITs off the top off my head but check this idea and tell me if this work, the bugs night be in small things i forgot with the QUERY structure, print this and PASTE to PHPmyADMIN or you DB query and see its all good, but this concept will sqve a lot of problems..
I'm in a bit of a pickle here, its just that I'm trying to enter some data that I get from users into a table, but for some reason it won't let me insert the data, however I have exactly the same query for another part of the table and that seems to work perfectly fine.
for example when I execute this query, it doesn't work:
$updateibtask2 = "UPDATE ibtask_task2_75beep SET
Trial1_tone_actual= '$taskerror[0]', Trial2_tone_actual= '$taskerror[1]', Trial3_tone_actual= '$taskerror[3]',
Trial4_tone_actual= '$taskerror[4]', Trial5_tone_actual= '$taskerror[5]', Trial6_tone_actual= '$taskerror[6]',
Trial7_tone_actual= '$taskerror[7]', ... WHERE user_id = '$memberid'";
However, when I try this query it works perfectly fine:
$updateibtask2_estimate = "UPDATE ibtask_task2_75beep SET
Trial1_tone_estimate= '$taskerror[0]', Trial2_tone_estimate= '$taskerror[1]', Trial3_tone_estimate= '$taskerror[3]',
Trial4_tone_estimate= '$taskerror[4]', Trial5_tone_estimate= '$taskerror[5]', Trial6_tone_estimate= '$taskerror[6]',
Trial7_tone_estimate= '$taskerror[7]', ... WHERE user_id = '$memberid'";
I'm just wondering where I'm going wrong?
Also if it helps the PHP code that I'm using to run these queries are:
$task2 = array();
$task2 = $_SESSION['task2'];
$task2estimate = array();
$task2estimate = $_SESSION['estimatedpress2'];
$task2actual = array();
$task2actual = $_SESSION['actualpress2'];
addacutalerror_75($memberid, $task2actual);
addestimatederror_75($memberid, $task2estimate);
Also to check whether there was data present for $task2actual I had done an echo ..[0], .. [1].. etc and there was data present in the array.
Updated
For those who are searching for solutions and have the same problem, here's what I did:
function addacutalerror_75($memberid, $task2actual) {
$insertmember = "INSERT INTO ibtask_task2_75beep (user_id, Trial1_tone_actual,
Trial2_tone_actual, Trial3_tone_actual, Trial13_tone_actual,
Trial14_tone_actual, ..., Trial40_notone_actual) VALUES ('$memberid', '$task2actual[0]', '$task2actual[1]', '$task2actual[3]', '$task2actual[18]', '$task2actual[21]', '$task2actual[22]', '..., '$task2actual[24]', '$task2actual[29]', '$task2actual[33]','$task2actual[38]' )";
mysql_query($insertmember) or die(mysql_error());
}
by the way, UPDATE is very different from INSERT.
UPDATE - modify the existing record(s) on the table.
INSERT - adds new record(s) on the table.
Your query is fine but you are doing update. But you want to insert record not to update record right? The query when you insert record looks like this,
$updateibtask2 = "INSERT INTO ibtask_task2_75beep
(Trial1_tone_actual, Trial2_tone_actual,
Trial3_tone_actual,...)
VALUES ('$taskerror[0]', '$taskerror[1]',...)";
and your query is vulnerable with SQL Injection. Please take time to read the article below to protect against SQL injection,
Best way to prevent SQL injection in PHP?
//conn to DB
echo $_POST['text'];
$filter = $_POST['text'];
$sql = "SELECT DISTINCT * FROM contents
WHERE
MATCH(content,title) AGAINST ('$filter')
";
$mksql=mysql_query($sql);
while($row = mysql_fetch_assoc($mksql)) {
echo $row['title']."<br />";
}
I send POST request to another page with the above code.
It echoes me what I wrote in the input field but it doesn't output any result.
When I run the query in phpmyadmin in works and outputs me 1 result.
Where's the problem?
Try this :
$sql = "SELECT DISTINCT * FROM contents
WHERE MATCH(content,title) AGAINST ('$filter' IN BOOLEAN MODE ) ";
Before query any fulltext search you must create mysql fulltext indexing.
Execute this query in phpmyadmin.
ALTER TABLE contents ADD FULLTEXT(content, title);
may be there is problem in the post data which is not matching with full text search. you can use mysql_escape_string(), htmlspecialchars() functions for the post data you are using for query. These functions will help avoiding html tags,quotes,etc. you can print the generated sql and run that query in phpmyadmin. and of course create mysql fulltext indexing for the field you are searching.