MSSQL PHP cannot delete rows - php

First of all sorry for my English.
Im trying to delete / insert some rows to a MSSQL Table but i get no error but it just doesnt work. But when I do a SELECT statement it works as well.
$dbhandle = mssql_connect("SERVERNAME", "username", "password") or die("Couldn't connect to SQL Server on $myServer");
$selected = mssql_select_db("myDB", $dbhandle) or die("Couldn't open database $myDB");
mssql_query("DELETE dbo.import");
The server SQL INSTANCE is installed on my local computer and I'm the Administrator of this SQL Server but the Webserver is on another PC.
I created the user and I assigned the role db_owner
When I try the same query on another SQL Server, it works.
I also tryed:
$dbhandle = mssql_connect("SERVERNAME", "username", "password") or die("Couldn't connect to SQL Server on $myServer");
$selected = mssql_select_db("myDB", $dbhandle) or die("Couldn't open database $myDB");
$result = mssql_query("DELETE dbo.import; SELECT * FROM dbo.import;");
$sqlRows = array();
while($row = mssql_fetch_array($result))
{
$sqlRows[] = $row;
}
echo sizeof($sqlRows);
and the echo return 0, but when I check from SSMS the records are still there.
DELETE SCHEMA.TABLE; SELECT * FROM SCHEMA.TABLE;
returns 0 rows. And then I try again
SELECT * FROM dbo.import;
returns 26 rows.

Ok I just found the answer searching on the Web.
There was a rollback as mentioned from Shnugo and adding SET IMPLICIT_TRANSACTIONS OFF; at the beginning of the query it solve the problem.
Thank you

EDIT 2
The solution was a not committed transaction. See comments...
EDIT
My answer is not right to the point... I must admit, that I never used DELETE without FROM, but this works obviously:
create table #tbl(id int);
insert into #tbl values(1),(2);
select * from #tbl;
delete #tbl;
select * from #tbl;
drop table #tbl;
I let the answer there due to the comments...

Related

MySql query not in working in PHP but works in phpMyAdmin

At first I'm sorry for posting duplicate question I always try to find answers and never to ask. But nothing solved my problem. I have a MySql DB with table named data. I cannot change the table name. When I execute SELECT * FROM `data` or SELECT * FROM data in phpMyAdmin, the query works correctly but when I execute it in PHP script the query() returns false
<?php
$conn = new mysqli('localhost', 'username', 'pswd', 'dbname');
if ($conn->connect_error) {
die('connection error');
}
$result = $conn->query("SELECT * FROM `data`");
var_dump($result);
echo "-".$conn->error."-";
I have looked at these questions:
Mysql query works in phpmyadmin but not in php (due to date)
Mysql query works in Phpmyadmin but not works in PHP
MySQL query working in phpmyadmin but not in php
and some others...
$result ="SELECT * FROM `data`";
$row=mysqli_query($conn,$result);
while($row_result=$row->fetch_assoc())
print_r($row_result);
With your replies I've got some idea what to try next.I've created copy of the table on my own server and tried changing data types. One of data types in original table is set as JSON, when i changed it to TEXT it started to work.

how i can Import data from Database ( My Sql) to another website

I need to show data from another website in my website,
i had control panel for tow site but i need it dynamic way to connect them
website 1 website 2
***** *****
* A * <<<< =Data== * B *
***** *****
so i write
$link = #mysql_connect("Ip Addres to another website","username for mysql 2","passowrd 2 ")or die("Couldn't make connection.");
#mysql_select_db("qatarlab_test",$link)or die("Couldn't select database");
$factorRes = #mysql_query("SELECT count(id) FROM `factor` ");
$factorRow = #mysql_fetch_array($factorRes);
echo $factorRow[0];
but nothing happen
Firstly, you must enable remote database connection in the original site so that the new site can connect to it. you can do this in the control panel->Database(Remote Database Connection) of the original site.
You could specify the IP or domain of the new site to that it allows only the new site to connect or you could just add wildcard (%) to allow any external connection to the database.
when this is done, run ur query again.
hope it helps
Hi Please try code given below,
$link = #mysql_connect("Ip Addres to another website","username for mysql 2","passowrd 2 ")or die("Couldn't make connection.") or die(mysql_error());
mysql_select_db("qatarlab_test",$link)or die("Couldn't select database", $link) or die(mysql_error());
$factorRes = #mysql_query("SELECT count(id) FROM `factor` " , $link) or die(mysql_error());
$factorRow = #mysql_fetch_array($factorRes);
echo "<pre>";
print_r($factorRow);
echo "</pre>";
Here $link will be for specific database connection and die(mysql_error()) for if any error in or query or database connection.
thanks
In your first line: mysql_connect you are passing the IP Address and Username for connecting the DB.
Make sure that the user has the privilege to connect to the DB remotely (i.e. via a machine other than the Localhost). In general, when a user is added in the DB its default access is for the localhost. Check out this link: http://dev.mysql.com/doc/refman/5.1/en/adding-users.html
You basically need to add user using: CREATE USER 'admin'#'IP Address of Website 1'; All the CPanel's have this provision in their interface as well.

Delete row from MySQL not working

I'm trying to delete a row from a table in MySQL using the PHP code below. The return value from mysql_affected_rows() is 1. There are no errors. However, the row still exits in MySQL. Not sure what I'm doing wrong. Any thoughts?
$db = array('host'=>'127.0.0.1',
'user'=>'root',
'pass'=>'',
'name'=>'testdb');
// CONNECT TO THE MYSQL SERVER
$connection = mysql_connect($db['host'], $db['user'], $db['pass']);
if(!$connection){
// HANDLE ERROR HERE
die('Unable to connect to MySql server : '.mysql_error($connection));
}
// SELECT THE DATABASE SCHEMA
if(!mysql_select_db($db['name'],$connection)){
// HANDLE ERRORS HERE
die('Unable to connect to database : '.mysql_error($connection));
}
$result = mysql_query("delete from photos where id=".$photo_id, $connection);
echo mysql_affected_rows($connection);
UPDATE
I added the following code to the end and that solved the issue -
mysql_query("commit", $connection);
Thanks for the comments!
Applied to innodb tables as in your case.
mysql_query("BEGIN", $connection);
// delete code
mysql_query("COMMIT", $connection);
What I typically do in this cases is to enable the general_log (set global general_log_file=general.log; and set global general_log=1;) and look what arrives in there.
Then it often becomes already obvious what is the problem.
If it is not clear yet what is going on you can run these queries on the mysql console...

mysql_select_db fail on somepage

I am making a social website for my company. The problem appears only in "diary" page. Most of time, my code connects to database successful, but sometime, it throws error:
Could not select database
This is my php code to connect to database on localhost:
function execute_action($query)
{
$link = mysql_connect("localhost", "root", "123456") or
die("Could not connect");
mysql_select_db("2t") or die("Could not select database");
$query = $query;
$result = mysql_query($query) or die("Query failed");
return $result ;
}
Do you have any solution to help me. Thanks for reading.
As you arent closing your database connections between querys, so, my first thought would be is you're reaching a max connection barrier to the mySQL server.
While this may not be the answer - showing the mysql_error would help.
As someone else pointed out, theres no need to connect and drop each time. Unless your page has incredibly long processing time, connecting at the start, closing at the end should be enough.
You need to pass the dbconfiguration $link while selecting the db and executing the query
mysql_select_db("2t", $link)
even in mysql_query( $query, $link)

Can I use MySql Create Select on two different servers with PHP?

I am trying to use PHP and MySQL's Create Table Select between two different MySQL servers. I am not sure this can be done like this with SQL. I get no errors but I get nothing done either:
<?php
$dbname = 'cms';
$dbmaster = 'cmsms';
$db1 = mysql_connect('localhost', 'root', 'secret');
if (!$db1) {
echo 'Could not connect to mysql';
exit;
}
$db2 = mysql_connect('server2', 'root', 'secret');
if (!$db2) {
echo 'Could not connect to mysql';
exit;
}
mysql_select_db("$dbname", $db1) or die ("Unable to select database");
mysql_select_db("$dbmaster", $db2) or die ("Unable to select database");
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql, $db1);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
$sql = "DROP TABLE `$row[0]`";
mysql_query($sql, $db1);
echo "Table rows: {$row[0]} Deleted <br/>";
$sql = "CREATE TABLE $row[0] SELECT * FROM $db2.$dbmaster.$row[0] ";
mysql_query($sql, $db1);
echo "Table: {$row[0]} created <br/>";
}
echo "<br/>done...";
mysql_free_result($result);
?>
That line:
$sql = "CREATE TABLE $row[0] SELECT * FROM $db2.$dbmaster.$row[0] ";
just doesn't work. the $db2 doesn't make it go to the other server and select.
From doing some reading on SO I found someone similar and someone said it could not be done and to look at federated tables, which will not work for me.
If this cannot be done above does anyone know a way to do what I am doing? I am dropping the tables on the copy and re-creating them based on the table in the master. Then I am selecting the data in the master to put in the re-created tables. Thank you
Update:
Just so I can be clear. The code works if everything were on the same server and I only had one database connection. It is because of the create table select that I have problems, I believe. This SQL needs to use two servers at the same time. The create table is for one database that just dropped it's tables but the select is selecting from the database of the second connection - two connections in the same SQL statement.
Yes, on your PHP script you can perform all queries you want on all different servers you have access and permission to.
You don't specify your database on the $sql though. You specify it when you run the mysql_query function.
So, your code:
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql, $db1);
Is wrong. It should only be:
$sql = "SHOW TABLES";
$result = mysql_query($sql, $db1);
By using $db1 on the second parameter of mysql_query you are already specifying that you want to show tables from that database only.
Following the same reasoning on the other sentences, you should be able to get results you want.
You won't be able to do this:
$sql = "CREATE TABLE $row[0] SELECT * FROM $db2.$dbmaster.$row[0] ";
If you want to insert rows from $db2 into newly created tables into $db1, you need to:
Run a query on $db2 to get results
Create table on $db1
Iterate $db2 results
Insert records on new table on $db1
There is no magic way of doing this in only one sentence.
You're on the right track but missing an important point. Your PHP code has opened a connection to db1 and db2 but that doesn't mean that the db1 server knows about the db2 server. Therefore you can't pass db1 a query with db2 in it and expect it to get rows from it. Your PHP script has to act as an intermediary.
You need to issue a select statement to select rows from db1's table, then loop over the results issuing insert statements for db2. Of course you do this after you issue a create table statement to create the blank table. I'm also assuming you're trying the 'create table select from' trick because you don't know how to issue the create table to copy the layout of the table in db1. Have you seen this?
http://dev.mysql.com/doc/refman/5.0/en/show-create-table.html
remove the $db2 database connection from your query
If you want to run SQL on $db1 use this
mysql_query($sql, $db1);
If you want to run SQL on $db2 use this
mysql_query($sql, $db2);
I don't think this is the best database design Johnny, depends of course on what you're trying to achieve. If you simply want to store data on both servers for redundancy, you should go with MySQL Replication. If you're separating data for high availability purposes, then consider data sharding, partitioning and definitely take a look at MySQL Proxy.
These will let you do all the work outside your application, so you wouldn't have to alter your application in case your sharding/partitioning/backup/redundancy scheme changes. Such tools make this completely invisible to your application.
Sorry if this is off-topic.
There are a few choices you have in regards to setting this up.
1) Implement mysql asynchronous replication. This would do the same thing you are trying to do in PHP but would be much more efficient.
Mysql Documentation: Replication
2) Fix your script to stream data from master server to backup server.
This would be done by creating the table and then loading the result row by row into the backup server's table.

Categories