MySQL problem connecting

Codes here !

Moderators: egami, macek, gesf

Post Reply
varrey
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Mar 12, 2018 2:19 am
Location: Singapore

Mon Mar 12, 2018 10:10 pm

<?php
// The global $_POST variable allows you to access the data sent with the POST method by name
$name = trim(strtoupper(htmlspecialchars($_POST['name'])));
$icno = trim(strtoupper(htmlspecialchars($_POST['icno'])));
$pwd = trim(htmlspecialchars($_POST['pwd']));
echo $name, ' ', $icno, " ", $pwd;
//Step1
$db = mysql_connect('localhost','trccoop_varrey','singrao802505','trccoop_memberInfo');
// Check connection
if ($db)
{
echo "Successful";
}
else
{
die("Failed to connect to MySQL: " . mysql_error());
}

$sql = "SELECT icno, name FROM PersonalData WHERE icno = $icno";
$result = $db ->query($sql);

if ($result->num_rows = 1)
{
echo "1 result";
}
else
{
echo ('0 results');
}
$db->close();

?>

it echoes as successful ....But how do i cheque whether the SQL is working or not....there is no echo after the first one

chorn
php-forum Fan User
php-forum Fan User
Posts: 558
Joined: Fri Apr 01, 2016 2:18 am

Tue Mar 13, 2018 2:37 am

1. Do not post passwords in open forums; just put them in a config file and use constants

2. You are mixing up procedual mysql_* function with OOP, $db will contain a ressource, according to the manual:

http://php.net/manual/en/function.mysql-connect.php

3. The mysql_* functions are removed from current versions of PHP; use PDO or MySQLi instead

4. Use "Prepared Statements" for your queries. It's really easy if you start early - and it will prevent people from deleting your whole database

5. Define "working".

5.1 Look for errors (PDO): errorInfo()

http://php.net/manual/en/pdo.errorinfo.php

5.2 It does return nothing or not what you expected? Run the query in a seperate tool like PHPMyAdmin; check if the database really contains those values; remove any WHERE condition until you see results

Post Reply