I have this command:
$result = $db->Execute("select id,name,description,requirements,display_condition from works order by id");
And I want to insert < td > INSIDE of the command, that way it enters all of the above select information into a table box.
I tried inserting echo "< td >"; right before the command, but it only creates ONE table box.
Screenie displaying my code and what is happening:
You need to execute something like this:
echo "<table border='1'>";
$query = 'SELECT id, name, description, requirements FROM works ORDER BY id';
$rows = $db->query($query, PDO::FETCH_OBJ);
while( $entry = $rows->fetch() ) {
echo "<tr>"
."<td>".$entry->id."</td>"
."<td>".$entry->name."</td>"
."<td>".$entry->description."</td>"
."<td>".$entry->requirements."</td>"
."</tr>";
}
echo "</table>";
This code allows to populate the data from the DB into a table.
BTW, if you have to skip some rows in the DB by analyzing the "display_condition" field, you would better do it in the SQL query. (I mean in the "$query = ..." line)
Related
If i use the following line in PHP the MySQL query works perfectly fine:
$query = "
SELECT *
FROM customers_1
JOIN customers_2
ON customers_1.id = customers_2.cus_id;
";
but when i try to just get two single fields like this:
$query = "
SELECT customers_1.product,customers_1.id
FROM customers_1
JOIN customers_2
ON customers_1.id = customers_2.cus_id;
";
it doesnt work at all, means i do not get any output. When i copy and paste the second query and put it into PHPMyAdmin it works perfectly fine.
What i do afterwards is this:
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
if($row['id']=="3")
{
foreach($row as $field)
{
echo $field;
}
}
}
mysql_close($dbhandle);
Because $row['id'] is nothing in your code because you just fetch one column from your query SELECT customers_1.product.. and that column is product
And no need foreach loop . You just get your value in single while loop
Updated Answer after question updated
while ($row = mysql_fetch_array($result)) {
if (!empty($row['id'])) {
echo $row['id'];
}
}
Note:- Mysql is deprecated instead use mysqli or PDO
You are comparing id in your code while your query is retrieving customers_1.product.
I've been trying to run queries through my website(online) and managed to do it with INSERT , DELETE and UPDATE but I've got some problems with SELECT... Not sure how to print the result. Here what I tried:
$execute = mysqli_query($con, $query);
$res = mysqli_fetch_array($execute);
foreach($res as $key=>$value) {
echo $key.' '.$value.'<br>';
}
That prints only one row..
also tried:
while($row = mysqli_fetch_array($execute))
but still nothing.So let's say for example i've got a table users with ~50 rows and columns - id, name and username. And when I run:
SELECT * FROM users
I want to print everything from that table.
Can someone help me with that?
Are you looking for something like this?
<?php
$execute = mysqli_query($con, $query);
while($recSet = mysqli_fetch_array($execute)) {
echo $recSet['id'].' - '.$recSet['name'].' - '.$recSet['username'];
}
I want to display all of the rows shown in the picture where CID = 1.
Here is my PHP code with SQL:
`
$contractCount = 1;
$sql = "SELECT categories.categoryID
FROM categories
LEFT JOIN link
ON categories.categoryID = link.categoryID
WHERE link.CID = '$contractCount'";
$res = $con->query($sql);
if (!$res) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while ($row = mysqli_fetch_array($res)) {
echo $row['categoryID'];
}
Here is an image showing the table in PHPMyAdmin called categories.
So I need output as ITSM, Mar and HrAd but I am only getting ITSM and not the rest.
EDIT 1: The LEFT JOIN makes no difference here, the link table has no bearing on the SELECT statement
EDIT 2: I have solved the problem, my mistake was that I had the table names the wrong way round in the SQL query.
You need to use the function mysql_fetch_row, this will fetch a row and move the pointer to the next one.
while ($row = mysqli_fetch_row($res)) {
echo $row['categoryID'];
}
I'm working with a file and I'm attempting to do multiple select statements one after another and insert some values. So far the insert and the select I've got working together but when attempting to get the last SELECT to work I get no value. Checking the SQL query in workbench and everything works fine. Here's the code:
$id = "SELECT idaccount FROM `animator`.`account` WHERE email = '$Email'";
$result = mysqli_query($dbc, $id) or die("Error: ".mysqli_error($dbc));
while($row = mysqli_fetch_array($result))
{
echo $row[0];
$insert_into_user = "INSERT INTO `animator`.`user` (idaccount) VALUES ('$row[0]')";
}
$select_userid = "SELECT iduser FROM `animator`.`user` WHERE iduser = '$row[0]'";
$results = mysqli_query($dbc, $select_userid) or die("Error: ".mysqli_error($dbc));
while($rows = mysqli_fetch_array($results))
{
echo $rows[0];
}
I do not want to use $mysqli->multi_query because of previous problems I ran into. Any suggestions? And yes I know the naming conventions are close naming... They will be changed shortly.
Your code makes no sense. You repeatedly build/re-build the $insert_int-User query, and then NEVER actually execute the query. The $select_userid query will use only the LAST retrieved $row[0] value from the first query. Since that last "row" will be a boolean FALSE to signify that no more data is available $row[0] will actually be trying to de-reference that boolean FALSE as an array.
Since you're effectively only doing 2 select queries (or at least trying to), why not re-write as a single two-value joined query?
SELECT iduser, idaccount
FROM account
LEFT JOIN user ON user.iduser=account.idaccount
WHERE email='$Email';
I'm not sure what you're trying to do in your code exactly but that a look at this...
// create select statement to get all accounts where email=$Email from animator.account
$id_query = "SELECT idaccount FROM animator.account WHERE email = '$Email'";
echo $id_query."\n";
// run select statement for email=$mail
$select_results = mysqli_query($dbc, $id_query) or die("Error: ".mysqli_error($dbc));
// if we got some rows back from the database...
if ($select_results!==false)
{
$row_count = 0;
// loop through all results
while($row = mysqli_fetch_array($result))
{
$idaccount = $row[0];
echo "\n\n-- Row #$row_count --------------------------------------------\n";
echo $idaccount."\n";
// create insert statement for this idaccount
$insert_into_user = "INSERT INTO animator.user (idaccount) VALUES ('$idaccount')";
echo $insert_into_user."\n";
// run insert statement for this idaccount
$insert_results = mysqli_query($dbc, $insert_into_user) or die("Error: ".mysqli_error($dbc));
// if our insert statement worked...
if ($insert_results!==false)
{
// Returns the auto generated id used in the last query
$last_inisert_id = mysqli_insert_id($dbc);
echo $last_inisert_id."\n";
}
else
{
echo "insert statement did not work.\n";
}
$row_count++;
}
}
// we didn't get any rows back from the DB for email=$Email
else
{
echo "select query returned no results...? \n";
}
Hello guys i am trying to show all the users pokemon were the table belongsto = there username here is my code
i have a connect on top of this has well
// Get all the data from the "example" table
$result = "SELECT * FROM user_pokemon WHERE
belongsto='".$_SESSION['username']."'
AND (slot='0')'";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo $row['pokemon'];
echo $row['id'];
}
i have print red the username and there username is in the username session .
i think i mite be missing a ' or something i add or mysql at the end of the query but then the pages dies with no error
You are not running the query and have an error in it. And you're not escaping strings going into query.
A proper version of the code would be
// escape a string going to query.
$username = mysql_real_escape_string($_SESSION['username']);
// create a query
$sql = "SELECT * FROM user_pokemon WHERE belongsto='$username' AND slot=0";
// run a query and output possible error for debugging purposes.
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
// keep getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo $row['pokemon'];
echo $row['id'];
}
It appears to me that the final query would be:
SELECT * FROM user_pokemon WHERE belongsto='NAME' AND (slot='0')'
where NAME is the name you pass in. If that is the case, there is an extra single quote at the end. I presume you are getting a SQL error?