Trying to Insert a Variable into MSSQL using PHP - php

I am attempting to insert some variables into MSSQL and its giving me a 500 server error
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
However it works when I substitute simple number or text values for those variables. I am running Wamp server and connecting to an amazon RDS MSSSQL instance. Here is my code:
$status = "closed";
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
$query = "INSERT INTO DatabaseTableName (ColumnName) VALUES ('$status')";
$results = odbc_exec($connection, $query);
if ($results){
echo "Query Executed";
}else {
echo "Query failed " .odbc_error();
}
It works if I remove the variable $status and put in a number or a word instead. Any help would be greatly appreciated.

$query = "INSERT INTO DatabaseTableName (ColumnName) VALUES ('".$status."')";
You may want to look into cleansing your input as well.
See mysqli real escape string

Related

PHP Warning: Error while sending INIT_DB packet. PID=3629

My automation code encountered a subject-like problem when calling a particular function.
It occurs randomly during execution, not immediately after execution, and after the problem occurs, the error "mysql_num_rows() expect parameters 1 to be resource, boolean given in" occurs and normal behavior is not performed.
The OS is Ubuntu 18.04.
PHP version is 5.6.40.
Mysql version is 5.7.38.
The problematic function code.
$conn = mysql_connect("127.0.0.1","ID","PW");
if($conn ==NULL)
{
echo "<script> alert(\" Error : DB 연결 에러입니다. 불편을 드려 죄송합니다. 관리자에게 문의 하십시요\"); </script>";
return $conn;
}
mysql_select_db("mysql");
mysql_query("set session character_set_connection=utf8;");
mysql_query("set session character_set_results=utf8;");
mysql_query("set session character_set_client=utf8;");
$dbname = "test_table";
$str = "select * from $dbname where 1";
$leak_result = mysql_query($str);
$leak_num1 = mysql_num_rows($leak_result);
Please give me a solution.
This error is appearing because of the maximum number of database connection requests have made.
Your Ubuntu 18 with PHP server it's making a lot of requests.
The "PID=3629" you read it's the Process Id that generates the error.
The method you are using mysql_query it's old and deprecated.
Now PHP uses mysqli method like this
$connect = mysqli_connect( $host, $user, $pass, $DBname );
$query = 'SELECT .... '; //here your query
$result = mysqli_query($connect,$query);
Like said in comments, if there's an error mysqli_query gives back a false result
PHP OFFICIAL SITE
If you want a more complete answer on the error use the mysqli_error() function.
Good luck

PHP While Loop that gets data from the database

I'm building a mobile application that requires push notifications to be sent whenever a new post is added.
Here's the PHP notification script: (it should be added inside the loop so it will be used again for each result.
$to = (should get DEVICEID from the database).
$title = "A new request"
sendPush($to,$title,$message);
function sendPush($to,$title,$message){
//Sends noti.
}
So How do I place the above code in a while loop, and get the DEVICEID from the database and place it into $to ?
I suppose that you have an mysql connection set up and that you are using mysqli.
After you have created the connection to the database, you should have a variable holding your connection:
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
So right now you are able to execute a SQL statement to your database. Your SQL statement should look similarily to this:
$sql = "SELECT DeviceID FROM Devices";
Now we can execute the statement we created to the database:
$result = mysqli_execute($con, $sql);
Now we get a result object. We can use this to get a while loop:
while ($row = mysqli_fetch_assoc($result)) {
$to = $row["DeviceID"];
// Rest of your code, this will be executed for every DeviceID in the database.
}
If you are using PDO you can read more about it here: http://php.net/manual/en/ref.pdo-mysql.php
But in general, the steps are the same.

Query not executing. No errors

$query = "SELECT `ip` FROM `banned` WHERE `ip` = '$ip'";
$retval = mysqli_query($conn, $query);
if(!$retval){
die("Could not Execute Query: " . mysqli_error($conn));
} else {
if(mysqli_num_rows($retval) == 0){
echo "test";
} else {
header('Location: http://www.teutonic-development.net/index.php?p=banned');
}
}
when I'm running this code all that's printed out is: "Could not Execute Query:"
I really have absolutely no idea why it's doing this. I'm connecting fine in my init.php file. Which is where this file is.
My other script which just adds a log entry works fine. And if I run my $query in phpmyadmin's sql interpreter it runs perfectly fine (when I replace the $ip part with an actual ip of course)
Any suggestions?
Normally one would say that hey your query failed to execute story finish. But this case is interesting.
Your code is
die("Could not Execute Query: " . mysqli_error($conn));
and your error message is
Could not Execute Query:
Notice even though you have mysqli_error($conn) but there is no mysql error being shown. That confirms 100% that $conn is not properly established (contrary to what you think)
So take a look at your code again and see if $conn is really a mysqli resource and is available to your file in proper variable scope.

PHP - call SQL Server procedure via ODBC error

Ugh. I've spent hours searching for a solution for this, so it's probably something simple. I have a stored procedure in SQL Server (2008) that I'm trying to call from PHP code. I'm using ODBC, and everything works if I use a hard-coded SQL statement.
But I'm trying to call a stored procedure that returns a set of rows (cursor, dataset, etc.), passing two parameters. When I run the code through my browser I just get a 500 http error. It is bombing on the odbc_execute() line. Here's my code:
// DSN-less connection:
$conn = odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", '', '');
if (!$conn)
{exit("Connection Failed: " . $conn);}
$fromDate = new DateTime('2012-1-1');
$toDate = new DateTime('2013-12-31');
$sql = odbc_prepare($conn, "{call GET_EVENTS_BY_DATE_RANGE(?, ?)}");
$params = array($fromDate, $toDate);
$result = odbc_execute($sql, $params);
if ( $result === false)
{
exit("Query failed");
}
odbc_close($conn);
Again, no problems until it hits the odbc_execute() function. Any help is appreciated.

"The page cannot be displayed because an internal server error has occurred." as a result of The FastCGI process exited unexpectedly

I am working with a php application deployed on Windows Azure.
I am running into an issue that results in the error:
"The page cannot be displayed because an internal server error has occurred."
when I look at the logs I see:
HTTP Error 500.0 - Internal Server Error
D:\Program Files (x86)\PHP\v5.3\php-cgi.exe - The FastCGI process exited unexpectedly
The problem is that this happens when I want to get data from a sql server. When I try to post data to the sql server everything works fine.
The following code represents what I am trying to do with comments to explain what's happening:
try {
// Try to connect to the database.
$conn = new PDO ( "sqlsrv:server = server,1433; Database = name", "user", "password");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$conn->beginTransaction();
// This works out when executed (I see the row in the table).
$query = "INSERT INTO owner (email) VALUES ('email#email.com')";
$conn->exec($query);
$result = $conn->query("SELECT email FROM owner");
$conn->commit();
// If the SQL select is succesfully performed ($result not false)
if($result !== false) {
echo "there are results";
// Traverse the result set and shows the data from each row
foreach($result as $row) {
echo $row['email']. '<br />';
}
}
$conn = null; // Disconnect
} catch ( PDOException $e ) {
print( "Error connecting to SQL Server. Please call support!" );
die(print_r($e));
}
The code above does insert information in the table on the server but causes the error explained above. Please let me know if there is any more information that I can provide.
Check access database credentials or maybe you have duplicate entry in database for email#email.com.
What is logged in error log?

Categories