Can't select from sys.extended_properties using PHP PDO - php

When querying a SQL Server database from PHP with this statement:
SELECT [Value] FROM sys.extended_properties WHERE name='MS_Description'
I get this error message:
"Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IMSSP]: Invalid type.' in C:\inetpub\wwwroot\index.php:104 Stack trace: #0 C:\inetpub\wwwroot\index.php(104): PDOStatement->fetch(2) #1 {main} thrown in C:\inetpub\wwwroot\index.php on line 104"
The php that I'm using is below, line 104 is the start of the While loop:
$sql = "SELECT [Value] FROM sys.extended_properties WHERE name='MS_Description'";
global $pdo;
$qry = $pdo->query($sql);
if (!$qry) {
exit('<pre>' . print_r($qry->errorInfo()));
}
while($row = $qry->fetch(PDO::FETCH_ASSOC)) {
foreach ($row as $key => $value) {
echo '<p>Key: ' .$key. ', Value: ' .$value. '</p>';
}
}
The code above works fine when selecting from any other table or view e.g:
SELECT * FROM sys.events
If I execute the statement via SSMS it works fine, any ideas why it doesn't work via PHP?

Casting the columns being selected as varchar(255) solved the problem i.e.
$sql = "SELECT CAST([Value] AS VARCHAR(255)) FROM sys.extended_properties WHERE name='MS_Description'";
Got to the solution based on the answers to this question and this question.

Related

Insert Arabic values in SqlServer 2019 using php

I want to insert arabic values, so i used this syntax :
$sql = "
insert into sdka_creation (creation_time, sdka_for, creator_ip)
values ('$creation_time',N\'$name\', '$creator_ip')
";
// or N"$name"
this gives me this error :
Fatal error: Uncaught TypeError: sqlsrv_rows_affected(): Argument #1 ($stmt) must be of type resource, bool given in C:\xampp\htdocs\quran\go.php:443 Stack trace: #0 C:\xampp\htdocs\quran\go.php(443): sqlsrv_rows_affected(false) #1 {main} thrown in C:\xampp\htdocs\quran\go.php on line 443
and other time :
$fnmae = N'$name';
$sql = "
insert into sdka_creation (creation_time, sdka_for, creator_ip)
values ('$creation_time','$fname', '$creator_ip')
";
this gives me synatx error :
Syntax error: unexpected token ''$name''
how could be solved ?
Note : db type is nvarchar
summary (all of these errors) :
$sql = "
insert into sdka_creation (creation_time, sdka_for, creator_ip)
values ('$creation_time',N\'$name\', '$creator_ip')
";
and :
$fnmae = N'$name';

updating data using get method

it seems like I can't update my table
I'm using href like this:
href="data/updatefunction.php?updtch=0&teachid=<?php echo $row['id']; ?>&classid=<?php echo $classid;?>"
and the data goes here:
updatefunction.php
if(isset($_GET['updtch'])){
global $con;
$teachid = $_GET['teachid'];
$classid = $_GET['classid'];
$q = $con->query("UPDATE class SET teacher=$teachid WHERE id=$classid");
I echoed my teachid and classid they transferred without problem
mysqli error is nothing, I get this:
Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\gradingsystem\admin\data\updatefunction.php:8
Stack trace: #0 {main} thrown in C:\xampp\htdocs\gradingsystem\admin\data\updatefunction.php on line 8
In your database , data type of teacher is a string or int ?
if data type of teacher is string than you have to pass $teachid as string like
UPDATE class SET teacher = '$teachid' WHERE id = $classid

PDO::fetchAll not working with MySQL stored procedure

I'm trying to return a row from a database by calling a stored procedure throught PHP. However, when I do this how I normally would I get a "General Error".
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error in C:\xampp\htdocs\Skilaverkefni 4\Courses\read.php:11
Stack trace:
#0 C:\xampp\htdocs\Skilaverkefni 4\Courses\read.php(11): PDOStatement->fetchAll(2)
#1 C:\xampp\htdocs\Skilaverkefni 4\index.php(13): ReadCourse('FOR3L3U')
#2 {main}
thrown in C:\xampp\htdocs\Skilaverkefni 4\Courses\read.php on line 11
Here is the code:
<?php
function ReadCourse($courseID)
{
require "dbCon.php";
$SQL = "SET #p0='" . $courseID . "'; CALL ReadCourse(#p0);";
echo "$SQL";
$logon = $pdo->prepare($SQL);
$logon->execute();
$records = $logon->fetchAll(PDO::FETCH_ASSOC);
print_r($records);
}
?>
After a long Google-session I found out that the issue is most caused by the way I'm handling the reading of the data returned from the stored procedure, how do I do this correctly?
The whole point of PDO is to make it impossible to run any old database query you choose to pass as a string.
From the documentation:
<?php
/* Call a stored procedure with an INOUT parameter */
$colour = 'red';
$sth = $dbh->prepare('CALL puree_fruit(?)');
$sth->bindParam(1, $colour, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 12);
$sth->execute();
print("After pureeing fruit, the colour is: $colour");
?>

PHP Inserting array values into MySQL prepared statement

I have an array of values that I am trying to insert into a database. Currently the below only inserts the first key value pair in the array and then returns an error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in /_/components/php/functions.php:33
Stack trace:
0 /_/components/php/functions.php(33): PDOStatement->fetchAll()
1 /testdb.php(44): Photo\DB\query('INSERT into cat...', Array, Object(PDO))
2 {main} thrown in /_/components/php/functions.php on line 33
line 33 in functions.php is in this function
function query($query, $bindings, $conn){
$stmt = $conn->prepare($query);
$stmt->execute($bindings);
$results = $stmt->fetchAll(); <---line 33
return $results ? $results : false;
}
The main code is
foreach ($sitecategories as $key => $value) {
// print "this is key $key and this is value $value";
if ( $conn ) {
$catquery=query("INSERT into categories (cat_id, label) VALUES (:catid, :label)",
array('catid'=>$key, 'label'=>$value),
$conn);
} else {
print "could not connect to the database";
}
}
So I am connecting OK to the DB (elsewhere in the code) and the first key value pair is inserted successfully but not the rest of the array. I'm guessing my syntax is incorrect somewhere in
array('catid'=>$key, 'label'=>$value),
but I can't figure it out. Any help much appreciated!
You shouldn't fetchAll() after you INSERT. INSERT has no result set.
I just ran a test of inserting and then calling fetchAll() on the PDOStatement object, and I confirm that this causes the "General error" exception you showed.
$stmt = $dbh->prepare("insert into foo () values ()");
$stmt->execute();
$result = $stmt->fetchAll();
Throws:
PHP Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY000]: General error' in /Users/billkarwin/workspace/PHP/21194489.php:14
Stack trace:
#0 /Users/billkarwin/workspace/PHP/21194489.php(14): PDOStatement->fetchAll()
#1 {main}
thrown in /Users/billkarwin/workspace/PHP/21194489.php on line 14
Your array of values is incorrect. The keys need to have the :, too.
array(':catid'=>$key, ':label'=>$value)

unexpected error on inserting values and error with implode()

I am inserting the values using PDO but i am getting error as:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc,price,nick_name,gender,size,color,birth_date,uname,uphone,ucountry,ustate,u' at line 1' in C:\wamp\www\aa\abc.php:58 Stack trace: #0 C:\wamp\www\www\aa\abc.phpphp(58): PDOStatement->execute(Array) #1 {main} thrown in C:\wamp\www\www\aa\abc.php.php on line 58
also getting Warning: implode() [function.implode]: Bad arguments for implode function
Code:
foreach ($_POST['pcheck'] as $p_check) ////storing checkbox values
{
$pcheckp[] = $p_check;
} $finalcheck = implode(',', $pcheck);
foreach ($_POST['pinc'] as $p_inc) ////storing inputfield values
{
$pinc[] = $p_inc;
} $finalpinc = implode(',', $pinc);
$sql = "INSERT INTO list (u_id,list_type,list_ff,breed,title,desc,price,nick_name,gender,size,color,birth_date,uname,uphone,ucountry,ustate,ucity,usite,pcheck,pinc,photo)
VALUES(:uid,:list_type,:list_ff,:breed,:title,:desc,:price,:nick_name,:gender,:size,:color,:date,:uname,:uphone,:ucountry,:ustate,:ucity,:usite,:pcheck,:pinc,:p_photo)";
$q = $db->prepare($sql);
$q->execute(array(':uid'=>dd,
':list_type'=>$list_type,
':breed'=>$breed,
':title'=>$title,
':desc'=>$desc,
':price'=>$price,
':list_ff'=>$list_ff,
':nick_name'=>$nick_name,
':gender'=>$gender,
':size'=>$size,
':color'=>$color,
':date'=>$date,
':uname'=>$uname,
':uphone'=>$uphone,
':ucountry'=>$ucountry,
':ustate'=>$ustate,
':ucity'=>$ucity,
':usite'=>$usite,
':pcheck'=>$finalcheck,
':pinc'=>$finalpinc,
':p_photo'=>$p_photo));
$_POST['pcheck'] and $_POST['pinc'] is used to get checkbox and input values which i am going to store in column in mysql.
I have checked many times to find the syntax error in insert query but nothing wrong is in it
Hoping to get help
Thanks!
for Warning: implode()
$finalcheck = implode(',', $pcheck);
should be
$finalcheck = implode(',', $pcheckp);
also desc is reserved for mysql you need to use it with `
$sql = "INSERT INTO list (`u_id`,`list_type`,`list_ff`,`breed`,`title`,`desc`,`price`,`nick_name`,`gender`,`size`,`color`,`birth_date`,`uname`,`uphone`,`ucountry`,`ustate`,`ucity`,`usite`,`pcheck`,`pinc`,`photo`)
VALUES(:uid,:list_type,:list_ff,:breed,:title,:desc,:price,:nick_name,:gender,:size,:color,:date,:uname,:uphone,:ucountry,:ustate,:ucity,:usite,:pcheck,:pinc,:p_photo)";

Categories