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'];
}
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:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Using LIKE in bindParam for a MySQL PDO Query [duplicate]
(2 answers)
Closed 7 years ago.
I'm trying to get PDO to return the results of a wildcard search. My code is:
$search = "%Notes%";
$result = $db->prepare("SELECT * FROM books WHERE 'name' LIKE :search");
$result->bindParam(':search', $search);
$result->execute();
while($arr = $result->fetch(PDO::FETCH_ASSOC)){
echo $arr['name'];
}
At the moment, I get a blank screen. If I run the sequel through PHPMyAdmin:
SELECT * FROM books WHERE name LIKE '%Notes%'
I get the appropriate result.
I assume it's something to do with the way I am formatting my PDO statement, I know you can't have a dynamic column name but I don't see what is going wrong?
in your query you have 'name' change that to just backticks instead of quotes
aka
$result = $db->prepare("SELECT * FROM `books` WHERE `name` LIKE :search");
you can also just remove the backticks
This question already has answers here:
Can PHP PDO Statements accept the table or column name as parameter?
(8 answers)
Closed 8 years ago.
I'm having trouble figuring out what I'm doing wrong. If i use this set of code I get the result I intend:
$x = $db->prepare('SELECT * FROM table LIMIT 2');
$x->execute();
print_r($x->fetchALL());
When I use this set of code I don't get anything in return:
$a = "table";
$b = "2";
$x = $db->prepare('SELECT * FROM ? LIMIT ?');
$x->execute(array($a,$b));
print_r($x->fetchALL());
Is there something I'm missing? Thanks in advance.
Parameter placeholders can only be used to replace column values; not table names, column names, or other syntax elements (including LIMIT values).
In order to make your query dynamic with respect to things that can't be parameterized, you have to build it yourself, without PDO's help. However, you should still build it so that the values that can be parameterized, are paramerized.
This question already has answers here:
MYSQL query returning 'resource id#12 instead of the numerical value it should return
(2 answers)
Closed 9 years ago.
EDIT::
The current code being used is :
//$username= 'lmfsthefounder';
$type = "SELECT account_type from user_attribs WHERE username='lmfsthefounder';";
$type_again = mysql_query($type);
$row = mysql_fetch_row($type_again);
$usertype = $row['account_type'];
echo $usertype;
which returns like this: Home Login Register Usertype is
When I search this query in mysql query explorer, it shows me the value is 1. I need that 1 to echo in my page.
(This should display 'Usertype is 1' in my navigation bar)
The mysql function you are looking for is mysql_fetch_assoc (link) instead of mysql_fetch_row.
But please, PLEASE don't use mysql_ functions. The link to the PHP manual tells you the same.
I would recommend trying to teach yourself how to use the mySQL PDO. There are many great tutorials online.
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'];
}