My question is about an Oracle database which i can connect with PHP. With the same query using SQL Developer i've got this:
But in PHP:
$tsql= "select orden, fase, maquina_id, DescMaterial, dhsalida, scg_MaterialesLotes.lote, scg_MaterialesLotes.lotecli from scg_fases inner join scg_materiales on scg_fases.IdBoletin = scg_Materiales.IdBoletin inner join scg_MaterialesLotes on scg_Materiales.IdMatOf = scg_MaterialesLotes.IdMatOF";
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
?>
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
$orden = $row["orden"];
$fase = $row["fase"];
$puesto = $row["maquina_id"];
$art = $row["CodMaterial"];
$desc = $row["DescMaterial"];
$fecha = $row["dhsalida"];
$lote = $row["scg_MaterialesLotes.lote"];
$loteCli = $row["scg_MaterialesLotes.lotecli"];
echo "<tr>";
echo "<td align=center>" . $orden . "</td>";
echo "<td align=center>" . $fase . "</td>";
echo "<td align=center>" . $puesto . "</td>";
echo "<td align=center>" . $art . "</td>";
echo "<td align=center>" . $desc . "</td>";
echo "<td align=center>" . $fecha . "</td>";
echo "<td align=center>" . $lote . "</td>";
echo "<td align=center>" . $loteCli . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
<?php
}
else
{
echo "Submission unsuccessful.";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
Using browser it shows:
Why i can't show the same info? Where is the problem? Thanks
1) Add CodMaterial field to your query You're not including the field CodMaterial in your query, so this line:
$art = $row["CodMaterial"];
Does nothing. So you can either add this field to the long list of fields in your query, or just use SELECT * FROM... to retrieve every field.
2) Don't use tablename.columnname in your query results rows When you query for scg_MaterialesLotes.lote for example, the resulting attribute will be called lote, so instead of having this in your PHP code:
$lote = $row["scg_MaterialesLotes.lote"];
Use this:
$lote = $row["lote"];
Do the same for scg_MaterialesLotes.lotecli
3) Echo $fecha as a string, at the moment is an object of type Instead of using:
echo "<td align=center>" . $fecha . "</td>";
Use this instead:
echo "<td align=center>" . ($fecha ? $fecha->format("Y-m-d H:i:s") : ""). "</td>";
Related
<html>
<head>
<meta http-equiv = "content-type" content = "text/html; charset = utf-8" />
<title>Using file functions PHP</title>
</head>
<body>
<h1>Web Development - Lab05</h1>
<?php
require_once("settings.php");
$dbconnect = #mysqli_connect($host, $user, $pswd, $dbnm);
if($dbconnect->connect_errno >0)
{
die('Unable to connecto to database [' . $db->connect_error . ']');
}
$queryResult = "SELECT car_id, make, model, price FROM cars";
echo "<table width='100%' border='1'>";
echo "<tr><th>ID</th><th>Make</th><th>Model</th><th>Price</th></tr>";
//initiate array
$displayrow= mysqli_fetch_array($queryResult);
//initiate while loop to iterate through table
while($displayrow)
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Make'] . "</td>";
echo "<td>" . $row['Model'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($dbconnect);
?>
</body>
</html>
This is doing my head in, I cannot figure out why it will not display the actual data apart from the Table header. No matter what I used.
I have tried mysqli_fetch_array, mysqli_fetch_row, mysqli_fetch_assoc but nothing works.
Help and explanation why it was not displaying the data would be much appreciated :)
First: You aren't running a query, you are only putting the query text in a variable. You need to use mysqli_query.
Second: You should add mysqli_fetch_array to the loop.
For example:
while($displayrow = mysqli_fetch_array($queryResult))
{
}
Otherwise you are only getting the first row.
Third: Array keys are case sensitive. There is no $row['ID'], as Jeribo pointed out, it is $row['car_id'] as referenced in your query. $row['Make'] is not the same as $row['make'].
Please Precision to names of field in Query ( car_id,make,...)
while($displayrow= mysql_fetch_assoc($queryResult) )
{
echo "<tr>";
echo "<td>" . $displayrow['car_id'] . "</td>";
echo "<td>" . $displayrow['make'] . "</td>";
echo "<td>" . $displayrow['model'] . "</td>";
echo "<td>" . $displayrow['price'] . "</td>";
echo "</tr>";
}
If you want to query outside you still have to set it in the loop:
$result = $db->query($queryResult)
while($row = $result ->fetch_assoc()){
...
}
a Good Tutorial is shown here: http://codular.com/php-mysqli
$row needs to be initialized so why don't you try:
while($row = mysqli_fetch_array($queryResult))
{
....
}
You have to get the result set first and then try fetching array from result set
<?php
require_once("settings.php");
$dbconnect = #mysqli_connect($host, $user, $pswd, $dbnm);
if($dbconnect->connect_errno >0)
{
die('Unable to connecto to database [' . $db->connect_error . ']');
}
$query = "SELECT car_id, make, model, price FROM cars";
$resultSet=mysqli_query($dbconnect,$query)
echo "<table width='100%' border='1'>";
echo "<tr><th>ID</th><th>Make</th><th>Model</th><th>Price</th></tr>";
//initiate array
$displayrow= mysqli_fetch_array( $resultSet);
//initiate while loop to iterate through table
while($displayrow)
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Make'] . "</td>";
echo "<td>" . $row['Model'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($dbconnect);
?>
http://www.w3schools.com/php/func_mysqli_fetch_array.asp
I have a loop inside other loop which is not working, this is the code:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
echo "</tr>";
}
When I query $result2 directly into the BBDD for the first result it shows three results but the code doesn't go in the second LOOP. Why? Any error here?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
$result2 = mysqli_query($connection, $result2);
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
echo "</tr>";
}
Use:
$query = "SELECT ....";
$result2 = mysqli_query($db, $query);
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
Before read this How can I prevent SQL injection in PHP? topic. After try to use mysql_query()
Try This
<?php
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
$results = mysqli_query($db,$result2);
while($row2 = mysqli_fetch_array($results))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row['outcomeName'] . "</td>";
}
echo "</tr>";
}
?>
Before fetching data execute mysql query using mysqli_query() function then run mysqli_fetch_array(). It would be good if you count result as $count = mysqli_num_rows($query) and manage your code with if... else .
I think $result2 should be output of mysqli_query not just merely query. Talking in analogous to MySQL.
Probably you should have something like this
$result2 = mysqli_query($result2);
I am trying to display a link in a php document. the data is stored in mysql. I have stored the url in the field course_url.
i can get the page to show the hyperlink asd a plain text but want it to show ashyperlink with "Click Here" anchor text. The coding i got so far is:
<?php
$con=mysqli_connect("localhost","root","","mentertraining");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT `coursedates`.`coursedate_id`,`coursedates`.`course_id`,`coursedates`.`date1`,`courses`.`course_title`,`courses`.`course_url`,`courses`.`no_of_days` FROM coursedates\n"
. "LEFT JOIN `mentertraining`.`courses` ON `coursedates`.`course_id` = `courses`.`course_id` LIMIT 0, 30 ";
$result = mysqli_query($con,$query);
echo "<table border='1'><tr><th>Course Title</th><th>Course Date</th><th>No of Days</th><th>Course URL</th></tr>";
while($row = mysqli_fetch_assoc($result))
{
$date = new DateTime($row['date1']);
$row['date1'] = $date->format('d/m/Y');
echo "<tr>";
echo "<td>" . $row['course_title'] . "</td>";
echo "<td>" . $row['date1'] . "</td>";
echo "<td>" . $row['no_of_days'] . "</td>";
echo "<td>""<a href=" . $row['course_url'] . >"'Click Her'"</a>""</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Your echo is malformed on this line:
echo "<td>""<a href=" . $row['course_url'] . >"'Click Her'"</a>""</td>";
It should be:
echo "<td><a href='" . $row['course_url'] . "'>Click Here</a></td>";
You have used the wrong "' quotations
while($row = mysqli_fetch_assoc($result))
{
$date = new DateTime($row['date1']);
$row['date1'] = $date->format('d/m/Y');
echo "<tr>";
echo "<td>" . $row['course_title'] . "</td>";
echo "<td>" . $row['date1'] . "</td>";
echo "<td>" . $row['no_of_days'] . "</td>";
echo "<td><a href='" . $row['course_url'] ."'>Click Her</a></td>";
echo "</tr>";
}
I have two tables like this:
[tblFacilityHrs] id uid title description
[tblFacilityHrsDateTimes] owner_uid startEventDate endEventDate startTime endTime days recurrence finalDate
I've joined the table as follows:
$result = mysql_query("SELECT * FROM tblFacilityHrs JOIN tblFacilityHrsDateTimes ON tblFacilityHrs.uid =tblFacilityHrsDateTimes.owner_uid") or trigger_error(mysql_error());
To display both tables I go through each record and put it all in a table:
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
echo "<tr>";
echo "<td valign='top'>" . nl2br( $row['title']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['description']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['startEventDate']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['endEventDate']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['startTime']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['endTime']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['days']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['recurrence']) . "</td>";
echo "<td valign='top'>" . nl2br( $row['finalDate']) . "</td>";
My question is now, how do I edit each record?
In my main page under the table I would normally do this:
pseudo code
echo "<td valign='top'><a href=edit.php?id={$row['id']}>Edit</a></td><td><a href=delete.php?id={$row['id']}>Delete</a></td> ";
Then on my edit page I would do something like this:
pseudo code
if (isset($_GET['id']) ) {
$id = (int) $_GET['id'];
if (isset($_POST['submitted'])) {
foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); }
$sql = "`title` = '{$_POST['title']}' , `description` = '{$_POST['description']}' WHERE `id` = '$id' ";
mysql_query($sql) or die(mysql_error());
echo (mysql_affected_rows()) ? "Edited row.<br />" : "Nothing changed. <br />";
echo "<a href='list.php'>Back</a>";
How does this work since the tables are joined? How can I edit both of them?
I think you should update each Table seperatly ; first the master table and then the detail table , and after that reabind your dataview
Hey guys, first time using stackoverflow.
can you guys help me debug?
Heres the problem, this query is selecting all of the rows from my database, its only outputting the first one twice for some reason.
$top10_query = "SELECT * FROM kicks";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
$row = mysqli_fetch_assoc($result);
$rating = $row['rating'];
$description = $row['description'];
$completed = $row['completed'];
$userid = $row['userid'];
$posted = $row['posted'];
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td class='rating'>" . $rating . "</td>";
echo "<td class='description'>" . $description . " </td>";
echo "<td class='completed_" . $completed . "'>" . $completed . "</td>";
echo "<td class='author'>";
echo "Posted by: <a href='profile?userid=" . $userid . "'>" . $userid . "</a><br />";
echo "on "; echo $posted;
echo "</td>";
echo "</tr>";
}
You are looping over the rowset, but never retrieving its value more than once. You pulled all of the values out of the first row, and cached them here:
$rating = $row['rating'];
$description = $row['description'];
$completed = $row['completed'];
$userid = $row['userid'];
$posted = $row['posted'];
Move this code into the loop, and remove the first fetch.
You need to update $rating, $description, etc. within the while loop:
<?php
$top10_query = "SELECT * FROM kicks";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
while($row = mysqli_fetch_assoc($result)) {
$rating = $row['rating'];
$description = $row['description'];
$completed = $row['completed'];
$userid = $row['userid'];
$posted = $row['posted'];
echo "<tr>";
echo "<td class='rating'>" . $rating . "</td>";
echo "<td class='description'>" . $description . " </td>";
echo "<td class='completed_" . $completed . "'>" . $completed . "</td>";
echo "<td class='author'>";
echo "Posted by: <a href='profile?userid=" . $userid . "'>" . $userid . "</a><br />";
echo "on "; echo $posted;
echo "</td>";
echo "</tr>";
}
?>
Or, of course, you can inline $rating, etc., writing $row['rating'] instead.
Note: you probably want to run your variables through htmlspecialchars before inserting them into HTML. Otherwise, a description like <script>alert('hacked');</script> could execute a script, opening yourself up to XSS attacks.
You can also use extract. I do not recommend you do this, however, as it may cause problems and confusion for other developers:
<?php
$top10_query = "SELECT * FROM kicks";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
while($row = mysqli_fetch_assoc($result)) {
extract($row);
echo "<tr>";
echo "<td class='rating'>" . $rating . "</td>";
echo "<td class='description'>" . $description . " </td>";
echo "<td class='completed_" . $completed . "'>" . $completed . "</td>";
echo "<td class='author'>";
echo "Posted by: <a href='profile?userid=" . $userid . "'>" . $userid . "</a><br />";
echo "on "; echo $posted;
echo "</td>";
echo "</tr>";
}
?>
The variables $rating etc are not "binded" to the expressions $row['rating'] etc. Once set, They will forever take these values unless you modify them again.
See PHP: Assignment Operators for detail.
Try to rewrite them as:
$top10_query = "SELECT * FROM kicks";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
while($row = mysqli_fetch_assoc($result)) {
$rating = $row['rating']; // <-- use the new value every time a row is fetched.
$description = $row['description'];
$completed = $row['completed'];
$userid = $row['userid'];
$posted = $row['posted'];
echo "<tr>";
echo "<td class='rating'>" . $rating . "</td>";
echo "<td class='description'>" . $description . " </td>";
echo "<td class='completed_" . $completed . "'>" . $completed . "</td>";
echo "<td class='author'>";
echo "Posted by: <a href='profile?userid=" . $userid . "'>" . $userid . "</a><br />";
echo "on "; echo $posted;
echo "</td>";
echo "</tr>";
}