How to select multiple query and retrieve them - php

When I retrieve two tables there's an error. What is the problem with my code? I have no idea how to fix this
<?php
include ('includes/config.php');
$mysqli = new mysqli(DB_SERVER, DB_UNAME,DB_PASSWD,DB_NAME);
if(!$mysqli){
throw new Exception($mysqli->connect_error, $mysqli->connect_errno);
}
$jqry = $mysqli->prepare("SELECT time FROM table_time ORDER BY time");
if (!$jqry){
throw new Exception($mysqli->error);
}
$jqry->execute();
$jqry->bind_result($time);
$jqry->store_result();
$times = array();
while ($jqry->fetch()){
$times[] = $time;
}
$jqry->close();
$gqry = $mysqli->prepare("SELECT table_group.group FROM table_group.table_group ORDER BY group");
if(!$gqry){
throw new Exception($gqry->error);
}
$gqry->execute();
$gqry->bind_result($group);
$gqry->store_result();
$groups = array();
while ($gqry->fetch()){
$groups[] = $group;
}
?>
This is the error I got:
Notice: Trying to get property of non-object in C:\xampp\htdocs\~Jeremiah\system5\joborder.php on line 31
Fatal error: Uncaught exception 'Exception' in C:\xampp\htdocs\~Jeremiah\system5\joborder.php:31 Stack trace: #0 {main} thrown in

your SQL just before the line 31 is invalid. Try executing it against db directly.
from the manual:
mysqli_prepare() returns a statement object or FALSE if an error occurred.
...and thats why we don't suppress notices in dev enviroment! :)

Related

Call to undefined method PDOStatement::numColumns()

I can't run the method numColumns() on my $result.
My code:
try {
$db = new PDO('sqlite:temps.sqlite',null,null,array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
} catch (PDOException $e) {
print 'Exception : ' . $e->getMessage();
}
$result = $db->query("SELECT * FROM data LIMIT 30");
$fields = $result->numColumns();
The error is the following one:
Uncaught Error: Call to undefined method PDOStatement::numColumns() in /*/export.php:7 Stack trace: #0 {main} thrown in /*/export.php on line 7
(line 7 is the last one)
Why does it not work?
You are trying to execute functions from different APIs. You call for PDO, but trying to execute numColumns from SQLite3Result.
As the docs state, PDO::query() returns a PDOStatement object and your error also points you to that:
Call to undefined method PDOStatement::numColumns()
You need to execute columnCount instead:
$result = $db->query("SELECT * FROM data LIMIT 30");
$fields = $result->columnCount();
Or if you wish to work with SQLite3, initialize an SQLite3 connection:
$db = new SQLite3('mysqlitedb.db');
$result = $db->query("SELECT * FROM data LIMIT 30");
$fields = $result->numColumns();

I suddenly get a fatal error. I don't understand it

I have been working at a code that used to work. However, suddenly, this message pops up.
Fatal error: Uncaught exception 'Exception' with message 'Email is incorrect!' in C:\xampp\htdocs\Prototype\classes\User.php:23
Stack trace: #0 C:\xampp\htdocs\Prototype\index.php(14): User->setEmail(true) #1 {main} thrown in C:\xampp\htdocs\Prototype\classes\User.php on line 23
But I just don't understand what this means. I tried using try and catch, but it keeps popping up.
This is the code where the error occurs
public function setEmail($p_email)
{
if (empty($p_email)) {
throw new Exception('Email kan niet leeg zijn!');
}
$this->email = $p_email;
if (!filter_var($p_email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Email is incorrect!'); //here is the error (line 23)
}
}
this is the code where it is summoned
$user = new User();
$user->setEmail($_SESSION['login']); //line 14
$currentUser = $user->getProfile();
$userEmail = $user->getEmail();
$userName = $user->getUserName();
$userID = $currentUser['userID'];
Your error is that you are passing a boolean instead of a valid string email...
Stack trace: #0 C:\xampp\htdocs\Prototype\index.php(14): User->setEmail(true) #1 {main} thrown in C:\xampp\htdocs\Prototype\classes\User.php on line 23
So, this line is incorrect:
$user->setEmail($_SESSION['login']); //line 14

error : Fatal error: Uncaught exception 'MongoException'

who can help me i have this error when i want go to the file article.php:
Fatal error: Uncaught exception 'MongoException' with message 'Invalid object ID' in /Applications/XAMPP/xamppfiles/htdocs/www/Site/Test/article1.php:14 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/www/Site/Test/article1.php(14): MongoId->__construct('Notice...') #1 {main} thrown in
beginning of the file article1.php
<?php
$id = $_GET['id'];
try {
$connection = new MongoClient();
$database = $connection->selectDB('test');
$collection = $database->selectCollection('articles');
} catch(MongoConnectionException $e) {
die("Failed to connect to database ".$e->getMessage());
}
$article = $collection->findOne(array('_id' => new MongoId($id)));
?>
You are only catching an exception of the type "MongoConnectionException" while the code is throwing a more generic "MongoException"
Catch(MongoException $e)

php multidimensional SplFixedArray declaration is throwing fatal error

I want to declare SplFixedArray(); to save memory consumption. but it is throwing fatal error.
$items=new SplFixedArray();
echo "Array Started...";
for($h=0;$h<5000;$h++)
{
for($i=0;$i<24;$i++)
{
$items[$h][$i]=$objSheet->getCellByColumnAndRow($i,$h+1)->getValue();
}
}
The same is working if do not declare new SplFixedArray();
Error:
Fatal error: Uncaught exception 'RuntimeException' with message 'Index
invalid or out of range' in /home/twa/files.php:168 Stack trace: #0
/home/twa/files.php(168): unknown() #1 {main} thrown in
/home/twa/files.php on line 168
$items=new SplFixedArray(SplFixedArray()); is also failing...
Please let me know correct syntax...
$items = new SplFixedArray(5000);
for ($h=0; $h<5000; $h++) {
$items[$h] = new SplFixedArray(24);
for ($i=0; $i<24; $i++) {
$items[$h][$i] = $objSheet->getCellByColumnAndRow($i,$h+1)->getValue();
}
}

PHP exception handling on DateTime object

Does anybody know why this function, when passed an invalid date (e.g. timestamp) to it, still throws an error despite the try-catch?
function getAge($date){
try {
$dobObject = new DateTime($date);
$nowObject = new DateTime();
$diff = $dobObject->diff($nowObject);
}
catch (Exception $e) {
echo 'Error: ', $e->getMessage();
}
return $diff->y;
}
Error:
Fatal error: Uncaught exception 'Exception' with message 'DateTime::_construct() [datetime.--construct]: Failed to parse time string (422926860) at position 7 (6): Unexpected character' in ... .php:4 Stack trace: #0 ... .php(4): DateTime->_construct('422926860') #1 ... .php(424): getAge('422926860') #2 {main} thrown in/... .php on line 4
Thank you very much in advance!
Chris, you cannot catch fatal errors, at very least you shouldn't.
Quoting keparo:
PHP won't provide you with any conventional means for catching fatal errors because they really shouldn't be caught. That is to say, you should not attempt to recover from a fatal error. String matching an output buffer is definitely ill-advised.
If you simply have no other way, take a look at this post for more info and possible how-tos.
Try this:
function isDateValid($str) {
if (!is_string($str)) {
return false;
}
$stamp = strtotime($str);
if (!is_numeric($stamp)) {
return false;
}
if ( checkdate(date('m', $stamp), date('d', $stamp), date('Y', $stamp)) ) {
return true;
}
return false;
}
And then :
if isDateValid( $yourString ) {
$date = new DateTime($yourString);
}

Categories