This question already exists:
Closed 10 years ago.
Possible Duplicate:
SQL compatibility issue
I have asked 1-2 questions here at stackoverflow, but apparently i'm on the wrong way approaching!
The issue is, i got this thing. I connect to the sql server like this:
odbc_connect("Driver={SQL Server};Server=$host;Database=$database;",$uid, $passVal ) or die("Connection could not established");
It's supposed to use the database i've selected.
Now, here is how i do to select the table columns.
$result = mysql_query("DESCRIBE TABLE users");
$data = mysql_fetch_assoc($result);
print_r($data);
Now, please someone guide me where i'm wrong, it's my first approach with this.
You may try to act "sarcastic, ironic" all you want, it would be rude.
My question is, why doesn't it recognize the database? apparently, that's the issue.
Thanks
The odbc_ methods have nothing to do with the mysql_ methods. If you connect using odbc_connect, you need to use the odbc_ methods to query the database. Calling any mysql_ function makes it establish some default connection to the nearest default database, which is entirely separate from the previously established odbc_ connection.
Guess what, you cannot query an MS SQL server using the mysql API.
Related
This question already has answers here:
MySQL and PDO: Could PDO::lastInsertId theoretically fail?
(2 answers)
Closed 7 years ago.
I am hoping to not have to write another request to mysqli to get the id of an inserted row.
I need to know if PDO::lastInsertId is an asynchronous thread safe method.
Since this is a functionality question, i will provide a schematic instead of a library i wrote that handles PDO; However, I will provide if necessary.
SCHEMATIC
$connect = mysqli->connect
$is_inserted = $connect->insert(...)
if($is_inserted){
$last_insert_id = $connect->lastInsertId();
}
I need to make sure that a another user will not get someone elses insert_id if they are running the script simultaneously.
The docs for LAST_INSERT_ID() say:
For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client.
So, you should have no issues using $connect->lastInsertId().
This question already has answers here:
MySQL error 2006: mysql server has gone away
(32 answers)
Closed 8 years ago.
There is an interesting case that I would like to solve, I've used exception notification via email and got the following mail that says:
MySql has gone away for query 'INSERT INTO sl_usermeta ( USER_ID, CREATED,DESIGNATION ) VALUES ( 7695, '2014-06-02 16:20:48', 'Manager')'.
This query executes in millisecond when I run this through MySQL. However the interesting fact to be noticed is there is a entry in database with the following values
UserID----CREATED------DESIGNATION
7695------2014-06-02-----16:21:16
I've checked the logs and found the user did not send the same request again. I want to ask if MySQL retries the same query once it comes back after going away?
MySQL itself cannot have done, since you weren't able to contact the server and it is therefore entirely unaware of your INSERT statement in the first instance.
However, whatever client you're using may well be retrying. Since you didn't tell us what that is, it's impossible to say.
This question already has answers here:
How to prevent duplicate usernames when people register?
(4 answers)
Closed 1 year ago.
I am having trouble with pdo and php because I am a php noob and dont know much. I am trying to connect to a database(which I did)then search the query (or something) for a matching string. Basically so a user cant make a account with the same username. This is the connection code.
$conn = new PDO("mysql:host=localhost;dbname=users;","root","");
and here is the confirmation code that is not working :(
$sql = 'SELECT username FROM users ORDER BY username';
foreach ($con->query($sql) as $row) {
if($username1 !== $row['username']) {
$acceptCounter++;
$username = $_GET['username'];
}
}
please help me :)
I have several suggestions to you.
First of all, add a unique index to your users table, like this:
ALTER TABLE users ADD UNIQUE(username)
After this you won't need to worry about name duplication: mySQL will throw an error (but please be careful and handle errors properly).
Use the WHERE clause as suggested in a comment. So you won't need to loop through a large number of rows to find one single row.
Use parameterized queries and prepared statements. As you're using PDO (which is, I believe, one of the best things you could do with PHP and MySQL), this shouldn't be a hard thing to do. So you'll be secure from (most of the) SQL injections.
Use PDO->fetch() and PDO->fetchAll(). Queries in loops are a really bad thing to do because this slows down your whole website.
This question already has answers here:
How to send a SQL query to database in PHP without waiting for result
(3 answers)
Closed 9 years ago.
Firstly I'm using the deprecated mysql functions (instead of mysqli) knowingly, so please do not tell me I should change to mysqli.
My question is: if I want to do an INSERT or UPDATE and continue processing the PHP script immediately, without waiting for MySQL to complete the task, can I use mysql_unbuffered_query (is that what is does?) or if not, how can I achieve that?
Sorry to break this to you :)
If you use mysqli, with the mysqlnd driver, you can pass a MYSQLI_ASYNC option to the query() method. Unbuffered queries do not help here.
Later on you can use the poll() and reap_async_query() to get to the result.
You can use INSERT...DELAYED for asynchronous insertions (1). I do not believe you can do asynchronous UPDATE's without resorting to spawning another process (2).
(1) but is not available to InnoDB tables
(2) if sticking to the old mysql extension is an absolute requirement
I'm sorry, i Know this must be a basic question, but i cant find the answer in my textbook- i guess i'm not looking in the right places.
If i have a mysql database that keeps track of members and new pages created by members, what php script do i use to append my mysql database dynamically so that the new entries are listed accordingly?
In order to update a database using PHP you need to connect to the database then issue a SQL statement - in your case most likely an INSERT statement which inserts a new row or rows.
There are 2 main ways to do this in PHP - using the built in mysql functions such as mysql_connect and mysql_query, or using the PDO library.
An example of using the native mysql functions can be found here: http://www.tizag.com/mysqlTutorial/mysqlinsert.php
An overview of PDO (and why it's a preferred solution over the native mysql functions) can be found here: http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/