This question already has answers here:
Why does this PDO statement silently fail?
(2 answers)
Closed 2 years ago.
I am trying to return information from my database, but all I get is an empty array when I know the database table is not empty.
This is my code
$pdo_dbconn = new PDO("pgsql:host=localhost;dbname=null;user=null;password=null")
or die ("Could not connect");
$sql = $pdo_dbconn->prepare("SELECT * FROM prsnl_codes;");
$sql->execute();
$array = $sql->fetchALL();
var_dump($array);
This is what the response is
array(0) {}
Can anyone tell me what is going on?
I solved the issue, and all the code up there was correct. The problem I was having was permission on the database, which I resolved.
Related
This question already has answers here:
mysqli last insert id
(3 answers)
Closed 5 years ago.
I am executing the following sql code
$query = "INSERT INTO order_shipping_addresses(user_customer_id, shipping_address_details, created, modified) VALUES('".$_SESSION['user_id']."', '".serialize($address)."', '".$created."', '".$modified."')";
$connection = $this->establish_connection();
$data = $connection->query($query);
$order_shipping_address_id = last_insert_id();
$connection->close();
What i wanted was when this query is executed i wanted the row id in which this data is inserted, i am using the last_insert_id() function but i am getting an error of Call to undefined method mysqli::lastInsertId()
how to extract all the data from the $data variable and also the row id in which the data is inserted. I am using PHP OOP style.
Thanks in advance..
You want to use $connection->insert_id
This question already has answers here:
Why does this PDO statement silently fail?
(2 answers)
Closed 6 years ago.
I get an empty array when running:
try {
$pdo = new PDO('mysql:127.0.0.1:dbname=mytodo', 'root', 'root');
}
catch (PDOException $e) {
die('Could not connect.');
}
$statement = $pdo->prepare('select * from todos');
$statement->execute();
var_dump($statement->fetchAll());
I've checked the database and running the same query 'select * from todos' returns the results as expected.
I've tried using different databases and tables. I always get an empty array.
Any ideas as to what's going wrong?
I'm running MAMP PRO and get the same issue whatever PHP version I choose.
Any answers or pointers greatly appreciated
By default PDO will die silently on a lot of query errors. Try to check for typo errors also.
How to view query error in PDO PHP
// The rest of the statement can also go into the try block. And why not echo the $e error msg if you have one?
This question already has answers here:
How can I use PDO to fetch a results array in PHP?
(2 answers)
Closed 2 years ago.
I'm doing var_dump since I started to learn PHP/PDO when I want to display the SQL datas, like:
$tickets = $db->query('SELECT * FROM tickets')->fetchAll();
var_dump($tickets);
And it shows everything.
But now I'd like to echo a list of all tickets name, usually I would do an echo $tickets->name; but first it doesn't work with fetchAll(); and when I use fetch(); it returns only the last row
Noob question but all the answers I get are outdated and uses depreciated mysql, how can I just display the rows with PDO ?
Thanks !
PDO fetch_all default to fetching both numeric and associative arrays
$tickets = $db->query('SELECT * FROM tickets')->fetchAll();
foreach($tickets as $ticket) {
echo $ticket['name'];
}
This question already has an answer here:
Fetching one row only with MySQLi
(1 answer)
Closed 7 years ago.
I've tried looking around and couldn't find an answer myself, so I'll post my problem instead, maybe it will help other people :)
I'm making a mysql query and getting an array out of it as it's supposed to do, but I want to take my first result out of this array and put that result in a variable instead so I can use it in my logic.
$conn = new mysqli($servername, $username, $password, $dbname);
$value_High = mysqli_query($conn, "SELECT MAX(picID) from pictures");
$id = mysqli_data_seek($value_High, 0);
var_dump($id);
I've tried some different things, I get a bool out of myqli_data_seek which is not what I want ofc, so I obviously need to use something else, I just don't know what.
$id = mysqli_fetch_row($value_High);
This question already has answers here:
How to get Insert id in MSSQL in PHP?
(3 answers)
Closed 10 years ago.
I want to get last inserted id of particular table. can any one tell me how i can get that?
we have mysql_insert_id() in mysql. Do we have similar kind of function in sql server 2008?
something like this works very nicely
$sql = "insert into tables(field) values (value);SELECT SCOPE_IDENTITY()";
$db = getConnection();
$results = mssql_fetch_assoc(mssql_query($sql));
$lastid=$results['computed'];
I made this function to make it easy :)... if you have more then one db resource just pass that into the mssql_query request. This will retrieve the last unique id generated on that resource connection.
mssql_query("insert into data(name) values('bob')");
$id = getLastId();
function getLastId() {
$result = mssql_fetch_assoc(mssql_query("select ##IDENTITY as id"));
return $result['id'];
}