odbc_exec - no error or results - php

I'm new to using odbc functions. I'm trying to simply execute a simple query, but I get not error or results back. I'm not sure what's wrong.
$server = [hidden];
$database = [hidden];
$user = [hidden];
$password = [hidden];
$connection = odbc_connect("Driver={ODBC Driver 13 for SQL Server};Server=$server;Database=$database;", $user, $password);
if ($connection) {
$mail = 'email#email.com';
$queryc = "SELECT COUNT(*) AS [Found] FROM [Table].[dbo].[Persons] WHERE [Address] = '$mail'";
$resultsc = odbc_exec($connection, $queryc);
if( !$resultsc ) {
die( print_r( odbc_error())));
} else {
'hi!';
}
}

Figured out the problem. It was fixed doing this:
$server = [hidden];
$database = [hidden];
$user = [hidden];
$password = [hidden];
$connection = odbc_connect("Driver={ODBC Driver 13 for SQL Server};Server=$server;Database=$database;", $user, $password);
if ($connection) {
$mail = 'email#email.com';
$queryc = "SELECT COUNT(*) AS [Found] FROM [Table].[dbo].[Persons] WHERE [Address] = '$mail'";
$resultsc = odbc_exec($connection, $queryc) or die(odbc_errormsg());
if( $resultsc ) {
'hi!';
}
}

Related

Check to see if any of inputs has value

Check to see if any of inputs has value? If input has the value null/empty don't update them to DB?
Here is my code and when I have empty input I lost previusly data in DB.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$mysql_host = "localhost";
$mysql_username = "root";
$mysql_password = "";
$mysql_database = "medvedgrad";
// First we create the connection
$pdo = new PDO("mysql:host=".$mysql_host .";dbname=".$mysql_database .";charset=utf8", $mysql_username, $mysql_password);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = json_decode(file_get_contents("php://input"));
$zm = $data->zlatni_medvjed;
$ck = $data->crna_kraljica;
$gv = $data->gricka_vjestica;
$dk = $data->dva_klasa;
$fk = $data->fakin;
// Then we prepare, and execute the query
$stmt = $pdo->prepare("UPDATE `stanje_piva`
SET
`zlatni_medvjed`=`zlatni_medvjed`+:zm, `crna_kraljica`=`crna_kraljica`+:ck, `gricka_vjestica`=`gricka_vjestica`+:gv, `dva_klasa`=`dva_klasa`+:dk,`fakin`=`fakin`+:fk WHERE `id`=1");
$stmt->execute(array("zm" => $zm, "ck" => $ck, "gv" => $gv, "dk" => $dk, "fk" => $fk));
?>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$mysql_host = "localhost";
$mysql_username = "root";
$mysql_password = "";
$mysql_database = "medvedgrad";
// First we create the connection
$pdo = new PDO("mysql:host=".$mysql_host .";dbname=".$mysql_database .";charset=utf8", $mysql_username, $mysql_password);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = json_decode(file_get_contents("php://input"));
$zm = $data->zlatni_medvjed;
$ck = $data->crna_kraljica;
$gv = $data->gricka_vjestica;
$dk = $data->dva_klasa;
$fk = $data->fakin;
//Let's do like Jack the ripper, lets divide into parts :p
$updateparts = array();
$zm = trim($zm);
if($zm != "")
{
$zmpart = "`zlatni_medvjed`=`zlatni_medvjed`+:zm";
$updateparts[] = $zmpart;
}
$ck = trim($ck);
if($ck != "")
{
$ckpart = "`crna_kraljica`=`crna_kraljica`+:ck";
$updateparts[] = $ckpart;
}
$gv = trim($gv);
if($gv != "")
{
$gvpart = "`gricka_vjestica`=`gricka_vjestica`+:gv";
$updateparts[] = $gvpart;
}
$dk = trim($dk);
if($dk != "")
{
$dkpart = "`dva_klasa`=`dva_klasa`+:dk";
$updateparts[] = $dkpart;
}
$fk = trim($fk);
if($fk != "")
{
$fkpart = "`fakin`=`fakin`+:fk";
$updateparts[] = $fkpart;
}
$updatepartstring = implode(",",$updateparts);
$update_query="UPDATE `stanje_piva`
SET
$updatepartstring
WHERE `id`=1";
// Then we prepare, and execute the query
$stmt = $pdo->prepare($update_query);
$stmt->execute(array("zm" => $zm, "ck" => $ck, "gv" => $gv, "dk" => $dk, "fk" => $fk));
?>

how to convert my sql query into json using php?

I have created a text box in which a query is entered and I need to run the query to display the values stored in the mysql table using php. Kindly help me with the approach of the project or any links that can help me. I have tried doing it by taking single query in php:
<?php
$username = "username";
$password = "password";
$host = "localhost";
$database="test";
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
$myquery = "SELECT `date_joined`, `city` FROM `users`";
$query = mysql_query($myquery);
if ( ! $query ) {
echo mysql_error();
die;
}
$data = array();
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}
echo json_encode($data);
mysql_close($server);
?>
But I want to get the json for every query that is being entered in the textbox. How do I handle that ?
Try this:
<?php
$username = "username";
$password = "password";
$host = "localhost";
$database="test";
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
$myquery = "";
$myquery = $_POST["id_of_the_text_box"];
$query = mysql_query($myquery);
if ( ! $query ) {
echo mysql_error();
die;
}
$data = array();
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}
echo json_encode($data);
mysql_close($server);
?>
But try to validate first the SQL entered in the textfield.

My page crashes when I try to retrieve information from MySQL

I'm having some issues with my PHP script.
I have a PHP function which is stored in a PHP file, and I'm trying to run that PHP function from another script.
So to explain myself with code:
like.inc.php:
function post_exists($id) {
$host = "example";
$username = "example";
$password = "example";
$database = "example";
$connection = new mysqli($host, $username, $password, $database);
$id = $connection->real_escape_string($id);
$query = $connection->query("SELECT COUNT(`id`) AS `count` FROM `posts` WHERE `id` = '$id'");
while ( $row = $objQuery->fetch_object() ) {
if ( $row->count == 1 ) return true;
}
}
profile.php:
include ( 'like.inc.php' );
if (post_exists(70) === true) {
echo 'Exists!';
}
A post with ID 70 exists, so it should echo Exists! but instead it just crashes half of my page. So maybe it's not loaded correctly?
Any help would be highly appreciated!
Where you have $objQuery must by $query. Try with this code:
function post_exists($id) {
$host = "example";
$username = "example";
$password = "example";
$database = "example";
$connection = new mysqli($host, $username, $password, $database);
$id = $connection->real_escape_string($id);
$query = $connection->query("SELECT COUNT(`id`) AS `count` FROM `posts` WHERE `id` = '$id'");
while ( $row = $query->fetch_object() ) {
if ( $row->count == 1 ) return true;
}
}
You can write the function in a different way like so:
function post_exists($id) {
$host = "example";
$username = "example";
$password = "example";
$database = "example";
$connection = new mysqli($host, $username, $password, $database);
$id = $connection->real_escape_string($id);
// Instead of getting count, just get the row and
// do the count with PHP
$query = $connection->query("SELECT * FROM `posts` WHERE `id` = '$id' LIMIT 1");
if($query){
return $query->num_rows > 0;
}
return false;
}

PHP Insert Query Using Prepare Statement

I have created an insert form. I'm doing an insertion operation into MySQL using prepare statement but it's not working. I don't understand what's wrong. Please help me to solve this issue. Is this what I did correct?
insert.php
<?php
include('dbconn.php');
session_start();
$_SESSION['example']='Session Created';
$srn = $_POST['srn'];
$client = $_POST['client']; // required
$category = $_POST['category'];
$sd = $_POST['sd']; // required
$fd = $_POST['fd'];
$host = "localhost";
$user = "root";
$pwd = "root";
$db = "eservice";
$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
$sql = "Insert into main(client,category,sd,fd) values(:client,:category,:sd,:fd)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':client',$_POST['client'],PDO::PARAM_STR);
$stmt->bindParam(':category',$_POST['category'],PDO::PARAM_STR);
$stmt->bindParam(':sd',$_POST['sd'],PDO::PARAM_STR);
$stmt->bindParam(':fd',$_POST['fd'],PDO::PARAM_STR);
$stmt->execute();
?>
dbconn.php
<?php
$host = "localhost";
$user = "root";
$pwd = "root";
$db = "eservice";
$mysqli = new mysqli($host,$user,$pwd,$db);
/* ESTABLISH CONNECTION */
if (mysqli_connect_errno()) {
echo "Failed to connect to mysql : " . mysqli_connect_error();
exit();
}
?>
Its always good to put up the errors you are having.
You are using two different database connection types pdo, and mysqli. You only need one.
I stripped your code down to the minimum.
<?php
$host = "localhost";
$user = "root";
$pwd = "root";
$db = "eservice";
$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
//$srn = $_POST['srn'];
$client = $_POST['client']; // required
$category = $_POST['category'];
$sd = $_POST['sd']; // required
$fd = $_POST['fd'];
// make sure client and sd are set here
$stmt = $pdo->prepare("
INSERT INTO
main
(client,category,sd,fd)
VALUES
(:client,:category,:sd,:fd)
");
$success = $stmt->execute([
'client' => $client,
'category' => $category,
'sd' => $sd,
'fd' => $fd
]);

Having trouble pushing data from a sql query to an array for comparison

So I am trying to compare user input from a form with data from a database, first name, last name, and email. My problem has been comparing my results with the ones that the user put in. What I am trying to do is put the results from my query into an array and then compare each array item against the input of the user. Yet I can't get through my process. What am I doing wrong?
Thank you all in advance.
P.S. I am a php newbie so any suggestions would also be appreciated
<?php
$servername = "localhost";
$username = "jon";
$password = "test";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
//test connection
if($conn -> connect_error) {
die("Connection Error: " . $conn -> connect_error);
}
//input from the user
$firstname = $_POST['first'];
$lastname = $_POST['last'];
$email = $_POST['email'];
//query for the database to select the columns
$queryFirst = "SELECT firstname FROM users";
$queryLast = "SELECT lastname FROM users";
$queryEmail = "SELECT email FROM users";
//query results
$resultFirst = $conn -> query($queryFirst);
$resultLast = $conn -> query($queryLast);
$resultEmail = $conn -> query($queryEmail);
$firstResult = array();
$lastResult = array();
$emailResult = array();
array_push($firstResult, $resultFirst);
array_push($lastResult, $resultLast);
array_push($emailResult, $resultEmail);
$firstValid = mysqli_result::fetch_array($firstResult);
$lastValid = mysqli_result::fetch_array($lastResult);
$emailValid = mysqli_result::fetch_array($emailResult);
//comparing query results to user input
foreach($firstResult as $comp) {
if(strpos($firstname, $comp) !== false) {
$firstname = true;
} else {
return false;
}
}
foreach($lastResult as $comp) {
if(strpos($lastname, $comp) !== false) {
$lastname = true;
} else {
return false;
}
}
foreach($emailResult as $comp) {
if(strpos($email, $comp) !== false) {
$email = true;
} else {
return false;
}
}
//redirection if successful or if failure
$success = "../loggedin.php";
$failure = "../fail.php";
if($firstname && $lastname && $email = true) {
header($success);
exit();
} else {
header($failure);
exit();
}
$conn -> close();
?>
Okay so first thing as already told you andrewsi, you can get all the info in one query. But if you want to select only one row, you should use a WHERE clause telling what to look for.
Check this:
<?php
$servername = "localhost";
$username = "jon";
$password = "test";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
//test connection
if($conn -> connect_error) {
die("Connection Error: " . $conn -> connect_error);
}
//input from the user . addslashes is for security, so they won't break your query and potentially abuse it.
$firstname = addslashes($_POST['first']);
$lastname = addslashes($_POST['last']);
$email = addslashes($_POST['email']);
//query for the database to select the columns
$query = "SELECT firstname, lastname, email FROM users WHERE firstname = '$firstname' and lastname = '$lastname' and email = '$email'";
//query results
$result = $conn -> query($query);
$numRows = $result->num_rows;
//redirection if successful or if failure
$success = "../loggedin.php";
$failure = "../fail.php";
if($numRows > 0) {
header($success);
exit();
} else {
header($failure);
exit();
}
$conn -> close();
?>
Haven't tested it but the idea is to check for a match in the query, not afterwards. Then if there's a match, it will return at least one row (if you defined your table correctly it shouldn't be possible to have duplicates).
Then based on that you make your choice.

Categories