Vocademy

PHP and MySQL Tips and Hacks

Renumber the primary index

SET @i=0;
UPDATE table_name SET column_name=(@i:=@i+1);

Using PDO in the same style as mysql_

If you need to convert a page from the defunct mysql_ method of communicating with a MySQL database to the PDO method, the following example shows how to use a similar syntax.

//First three lines are as usual
$query = SELECT * FROM contacts";

$stmt = $pdo->prepare($query);
$stmt->execute();                
$result = $stmt->fetchAll();      //fetchAll instead of fetch
$numrows = $stmt->rowCount();

for ($i = 0;$i < $numrows;$i++){
  $name = $result[$i]['name'];
  print "$name<br>";
}

Vocademy