I tried and read for hours... I just cant get the last Echo to show a total id count. I tried insert conditions in Select... Nope... Please help...
As i said in the title i am new at this. Doing just for 'fun' If you could help a newbie out i would appreciate that.
<?php
$dt = new DateTime('');
$dt->setTimeZone(new DateTimeZone('Europe/Lisbon'));
echo $dt->format('d-m-Y | G:i:s');
// START CONECTION TO BD -->
if (isset($_POST['submitted'])) {
DEFINE ('DB_USER', 'YES_I_DID_THIS');
DEFINE ('DB_PSWD', 'THIS_TOO');
DEFINE ('DB_HOST', 'YEAP..I CAN USE THE TABLE FINE.. ITS NOT CONNECTION');
DEFINE ('DB_NAME', 'MYUSER');
$dbcon = mysql_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);
$pnome = $_POST['pnome'];
$unome = $_POST['unome'];
$contacto = $_POST['contacto'];
$morada = $_POST['morada'];
$stamp = $_POST['stamp'];
$sqlinsert = "INSERT INTO Contactos (pnome, unome, contacto, morada, stamp) VALUES ('$pnome','$unome','$contacto','$morada','DATE: Auto CURDATE($stamp)')";
if (!mysql_query($dbcon, $sqlinsert)) {
die('');
}
$newrecord = "1 Record added to the Database";
// END INSERT DATA SCRIPT -->
}
// START COUNT TOTAL TABLE ID's
// As i said i am Noob... So i repeat this because i copied it... :)
DEFINE ('DB_USER', '-----------');
DEFINE ('DB_PSWD', '-----------');
DEFINE ('DB_HOST', '---------------');
DEFINE ('DB_NAME', '-----------');
$con = mysql_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);
if (!$con) {
die("cant connect: " . mysql_error());
}
mysql_select_db("$con");
$sql = "SELECT id FROM Contactos";
count($t);
//help here please i can't show this '$t' to show at page. Thanks
echo $t ;
?>
You aren't instantiating the $t
variable. You kind of need to to be able to get the value and use it. You would've seen an error about that if you turn on error reporting:
ini_set('display_errors', 1);
error_reporting(E_ALL);
Now your issue. You aren't even running the query.. You need to mysql_query()
that $sql
. I'd suggest you actually use sql's COUNT()
function.
$sql = "SELECT COUNT(id) AS count FROM Contactos";
$query = mysql_query($sql);
if(!$query) {
die(mysql_error());
} else {
$count = mysql_fetch_assoc($query);
echo $count['count']; // should have your count in there.
}
mysql_*
functions. The library is deprecated.I do understand that you've just started learning PHP and all, but it's best to start with the right libraries as mysql_*
will be removed soon as it's an insecure library.
You should look into using PDO
or MySQLi
as they are more modern libraries and while you might have to overcome a hurdle to learn it, being competent in those libraries will do you the world of good!
Resources: