Asked  7 Months ago    Answers:  5   Viewed   32 times

I'm uploading image file to storage server. Before uploading I should compose filename, which contains AUTOINCREMENT VALUE in it (for example, 12345_filename.jpg).

How could I get autoincrement value before inserting into DB?

I see only one solution

  1. insert empty row
  2. get it's autoincrement value
  3. delete this row
  4. insert row with real data using autoincrement value from p.1

Is there any other solutions?

Thank you

 Answers

25

The autoincrement value is generated by the database itself, when the insertion is done ; which means you cannot get it before doing the actual insert query.

The solution you proposed is not the one that's often used -- which would be :

  • insert some half-empty data
  • get the autoincrement value that's been generated
  • do your calculations, using that autoincrement value
  • update the row to put the new / full data in place -- using the autoincrement generated earlier in the where clause of the update query, to identify which row is being updated.

Of course, as a security precaution, all these operations have to be made in a transaction (to ensure a "all or nothing" behavior)


As pseudo-code :

begin transaction
insert into your table (half empty values);
$id = get last autoincrement id
do calculations
update set data = full data where id = $id
commit transaction
Wednesday, March 31, 2021
 
BrunoRamalho
answered 7 Months ago
31
<?php
  $host = 'localhost';
  $user = 'root';
  $pass = '';
  $db = 'mydatabase';

  $connection = new mysqli ($host, $user, $pass, $db) or die ("Databse connection failed");
  $sql = "SELECT waitlist_text FROM texts";

  $waitlist_text = $connection->query($sql);

  while($row = $waitlist_text->fetch_assoc()) {
      echo $row["waitlist_text"];
  }
?>
Wednesday, March 31, 2021
 
KouiK
answered 7 Months ago
66

No quotes around $field.

"SELECT $field FROM users WHERE email='$email' LIMIT 1";
Friday, May 28, 2021
 
alez
answered 5 Months ago
71

you can't print the result from mysqli_query, it is mysqli_resource and for dumping the error you need to change mysql_error() to mysqli_error()

$username = "bob";
$db = mysqli_connect("localhost", "username", "password", "user_data");
$sql1 = "select id from user_information where username='$username'";
$result = mysqli_query($db, $sql1) or die(mysqli_error());
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
    echo $row['id'].'<br>'; 
} 
Saturday, May 29, 2021
 
pamelus
answered 5 Months ago
39

yo need create the user "pma" in mysql or change this lines(user and password for mysql):

/* User for advanced features */
$cfg['Servers'][$i]['controluser'] = 'pma'; 
$cfg['Servers'][$i]['controlpass'] = '';

Linux: /etc/phpmyadmin/config.inc.php

Tuesday, July 13, 2021
 
ShadowZzz
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :