How to store mysql column info in an array? - php

The site I am creating will allow users to update their status. The only reasonable way I could think of storing the information would be to post a new line to my database for every post they make. I can associate the posts with their usernames on the same line. The problem is that no matter what I try nothing works on retrieving the info. I need to select the column where their status will be held and store all the status's in an array. Is this even possible? I am using PHP. As all the code I have used has been very ineffective I will not include it. Any help would be VERY appreciated!

Try to use:
<?php
$q = $db->prepare("DESCRIBE tablename");
$q->execute();
$result = $q->fetchAll();
var_dump($result);

Related

How to select a persons lastname in php from mysql table and put it in a $_Session variable

I want a straightforward answer with an example (NO LINKS please) to see how I can retrieve a specific person's last name from a table in a MySQL database using PHP and save it into a variable (i.e. $_Session). I have been looking for this question and I don't get anything related to one row in PHP. If this can be done better with mysqli* functions then I would be glad to see it.
Example
$getData = "SELECT lastname FROM person WHERE name='$name'";
$getData_q = mysql_query($getData) or die('Error');
Then i want something like this:
$_SESSION['lastame']=$getData_q; <- Please correct me,
I'm not asking to get a lastname from an input, I'm asking to get it from the table person in the database with the SELECT.
Use
$getData_q = mysqli_fetch_object($getData_q);
$_SESSION['lastame']=$getData_q->lastame;
But ensure that that num rows is greater than 0 and data would return a single row.

Fetch MySQLi with custom database (aka, column names for array keys)

I wrote a website using Google App Engine intended for the 'Datastore' database but now the website is already complete and because of Google's resources limit the website goes down too frequently (and I don't have any money to spend on it, even if I did I wouldn't :/)
Question:
When ever I fetch a table, I want to simply return it like this:
Each row is in it's own array to make it easy to iterate through
Instead of the standard number, each key would be the name of column is from
Problem:
No matter if I use fetch_all, fetch_assoc, or fetch_objectI never get anything that looks like what I am trying to get.
I am pretty bad with iterations and for loops so I couldn't figure how to do it manually.
The simplest way to build an associative array of database rows with mysqli_* is to do this:
$mysqli_query = mysqli_query($db_link, "SELECT * FROM tablename");
$result = array();
while($result[] = $mysqli_query->mysqli_fetch_assoc());
and $result will hold the array you want

Reuse fetched data in PHP and MySQL with PDO

I'm writing an accounting system with PHP and MySQL and using PDO for working with database. In the payment page, I must have 2 drop down lists from defined accounts.
At the first, I've used a simple query to fetch all records for accounts.
$sql = "SELECT id,title FROM tbl_accounts WHERE uid = ? ORDER BY title";
$q = $db->prepare($sql);
$q->execute(array($uid));
I have put fetched rows in a drop down list and everything was ok.
<select name="account_id2" class="medium" id="account_id2">
<option value="null">---</option>
<?php
while ($r = $q->fetch()) {
echo '<option value="'.$r['id'].'">'.$r['title'].'</option>';
}
?>
</select>
But when I wanted to make second drop down list using fetched records, nothing have been shown. So I thought I had to create another query for it. I did it and second one made correctly.
But my question is: Can not we use fetched data more than 1 time ? If I needed 3rd drop down, I had to write another query?
Depending on your conditional loop, you may need to reset the array pointer using reset().
Update:
To reset a result set you can use data_seek() or built a separate array as noted in the other answers.
Save the fetched results to an array using $r = $q->fetchAll() then loop through $r instead of fetching while you're looping.
Why not save the data to an array the first time you use it, so you don't have to bother your Database again for the same data.

pull specific table row column data to output in html using php

how do i pull data from my database table and put that data into a readonly textinput in a form for a user to see.
<input readonly type="text" name="em1" id="em1"/>
for this specific field i want to pull the data from a column called wmyt000 in the row of the currently logged in user using a $username variable with session that i created to get the current logged in users username in a table called cash_flow_plan
im using mysql and php
i havnt attempted to try anything yet. i was looking into mysql_fetch_array but that wants to display all of the users information for their row.
i havnt attempted to try anything yet.
ya. well, if you had, you'd know that you can do more with it than you think.
if you write a query limiting your results, then you're going to get what you want.
$query = "SELECT wmyt000 FROM cash_flow_plan WHERE username = '$username'"
$row = mysql_fetch_row($query);
print_r($row); // now you can see the structure of your array. access it accordingly.

How to allocate one ID to another and show this in HTML

For my iPhone web app I have created a database in php MyAdmin, it contains two tables (bookings, waiters and allocations) the I wish to view the bookings within a table in HTML where i can allocate a waiter to that booking. Any tips on how this would be done, any help will be appreciated as this is my first contact with web app development and MyAdmin. Thanks!
Mysql work with autoincrement id. You will have an id after of an insertion. You can to create a table only to id's and after to use it in the insertion of the waiter.
First you will need to get the data from the table via MySQL
$sql = "SELECT * FROM table_name";
$result = mysql_query($sql);
Then you would loop through the result
while($row = mysql_fetch_assoc($result)){
extract($row);
}
Then you can do as you wish with the returned data...
You really need to give us a little more... What have you got so far?

Categories