As of now I have no errors in my program, but I need the primary key for one of the tables for a relation for the following Query. but instead of getting a actual number the value the query is sending back is Resource id #4
Here is my Code: (The query that I'm having issues with is the $sql_branch, is there a function to change the result from "Resource id #4" to just 4?
$sql_branch = "SELECT BranchNum
FROM Branch
WHERE BranchName = '$_POST[branch]'";
$sql_result = "SELECT AuthorFirst, AuthorLast, OnHand, Title
FROM Inventory i, Wrote w, Author a, Book b
WHERE i.BookCode = b.BookCode AND i.BookCode = w.BookCode
AND a.AuthorNum = w.AuthorNum AND i.BranchNum = 1";
$connect = mysql_connect('students', 'xxxx', 'xxxx') or exit(mysql_error());
mysql_select_db('henrybooks', $connect);
if(mysql_query($sql_branch, $connect)) {
$branch = mysql_query($sql_branch, $connect);
}
else {
echo mysql_error();
}
if(mysql_query($sql_result, $connect)) {
$result = mysql_query($sql_result, $connect);
}
else {
echo mysql_error();
}
echo $branch."<br>";
echo $sql_branch."<br>";
echo "<table>
<tr>
<td>Author</td>
<td>Title</td>
<td>Number Available</td>
</tr>";
while( $row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['AuthorFirst'].$row['AuthorLast']."</td>";
echo "<td>".$row['Title']."</td>";
echo "<td>".$row['OnHand']."</td>";
echo "</tr>";
}
echo "</table>";
?>
Thanks!
You are not pulling results from the mysql_query. Try this:
if($branch_result = mysql_query($sql_branch, $connect)) {
$branch = mysql_fetch_array($branch_result);
}
Related
I am trying to display my query results on page. However, whenever I run the code although the query is correct it does not display anything.
Can anyone help? Would be muchly appreciated
<?php
session_start();
require_once("../config1.php");
if(isset($_POST["DailySales"])) {
$linkid = mysqli_connect(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Could not connect:" . connect_error());
$sql = "SELECT Retdatetime AS date , sum(rentalrate + overduecharge) AS mny
FROM frs_FilmRental
WHERE shopid='2'
Order BY retdatetime DESC ";
$result = mysqli_query($linkid, $sql);
if (!$result)) {
printf("Errormessage: %s\n", mysqli_error($linkid));
}
echo "<table border = '1' align='center'>";
echo "<th> Shop ID 2</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2><center>Shop ID 2 daily sales : </center></h2>";
echo "<tr><td>";
echo $row['mny'];
echo "</td><td>";
echo $row ['date'];
echo "</td></tr>";
}
}
?>
replace
echo $row ['date'];
by
echo $row ['Retdatetime'];
To understand query errors you should use mysqli_error() in your code. If there are no errors for executed query, then you can run while loop for it.
<?php
session_start();
require_once("../config1.php");
if(isset($_POST["DailySales"])) {
$linkid = mysqli_connect(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Could not connect:" . connect_error());
$sql = "SELECT Retdatetime , sum(rentalrate + overduecharge) AS mny
FROM frs_FilmRental
WHERE shopid='2'
Order BY retdatetime DESC ";
$result = mysqli_query($linkid, $sql);
if (!$result)) {
printf("Errormessage: %s\n", mysqli_error($linkid));
} else {
echo "<table border = '1' align='center'>";
echo "<tr><th> Shop ID 2</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2><center>Shop ID 2 daily sales : </center></h2>";
echo "<tr><td>";
echo $row['mny'];
echo "</td><td>";
echo $row ['date'];
echo "</td></tr>";
}
echo "</table>";
}
}
?>
fixes given in comments also applied
Please copy/paste the error, given by mysqli_error()
I need some help with php code.
I want to put data that I get with this query into simple php/html table.
Can you guys help me with this?
<?php
$konekcija=mysqli_connect("111.111.111.111","test","test123","publishers_instagram_accounts");
if (mysqli_connect_errno())
die ("Error:".mysqli_connect_error());
$select = "select * from publishers_instagram_accounts where followed_id <= (select followed_id from publishers_instagram_accounts where username='zika')*1.2 and followed_id >= (select followed_id from publishers_instagram_accounts where username='zika')*0.8";
$stmt = mysqli_prepare($konekcija, $select);
if (mysqli_error($konekcija))
die ("Error:" . mysqli_error($konekcija));
mysqli_stmt_bind_param($stmt, "ss", $trazi1, $trazi2);
if (!mysqli_stmt_execute($stmt))
{
die ("Error:" . mysqli_stmt_error($stmt));
}
else
{
$rezultat = mysqli_stmt_get_result($stmt);
while ($red = mysqli_fetch_array($rezultat, MYSQL_ASSOC))
{
var_dump($red);
}
}
while ($red = mysqli_fetch_array($rezultat, MYSQL_ASSOC)) {
echo "
<table>
<thead>
<td>Username</td>
<td>Password</td>
</thead>
<tbody>
<td>$red['username']</td>
<td>$red['password']</td>
</tbody>
</table>
";
}
Something like this.. Hope it helps you.
Hello #filipche you can try by replacing your 'while' loop with following code:
echo "<table><thead><tr><th>Field 1</th><th>Field 2</th><th>Field 3</th></tr></thead>";
while ($red = mysqli_fetch_array($rezultat, MYSQL_ASSOC)) {
echo "<tbody><tr>";
echo "<td>".$red["field1"]."</td>";
echo "<td>".$red["field2"]."</td>";
echo "<td>".$red["field3"]."</td>";
echo "</tr><tbody>";
}
echo "</table>";
Hope this is what you are looking for...
$qstring = "Your query goes here";
$result = mysql_query($qstring);
if($result)
{
while($row = mysql_fetch_array($result))
{
echo "<table>";
echo "<td>".$row[column name]."</td>";echo "</table>";
}
$i = 1;
$sql = "
SELECT
heroes.char_id, characters.char_name, heroes.class_id, heroes.count,
heroes.played, heroes.active FROM heroes, characters
WHERE characters.obj_Id = heroes.char_id AND heroes.played = 1
";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
echo "
<tr>
<td><span id='lefttop'><b><font color='#007aa2'>".$i++." </font></td><td><b><font color='#f6ff00'>".$row['char_name']."</font></td></span><div style='float:right;'><td><b> ".$row['count']."</td></div> <br />
</tr>
";
}
echo "";
?>
Anyone can help? im trying to make a hero status script on a java server, and i need to connect into 2 tables which is "heroes"(this is where i get the hero) and "character" (this is where i get the hero names)
Try this Temple:
<?php
$sql1 = "SELECT * your table1";
$query1 = mysql_query($sql);
while($row1 = mysql_fetch_array($query1))
{
echo "<tr>";
$sql2 = "SELECT * your table2";
$query2 = mysql_query($sql2);
while($row2 = mysql_fetch_array($query2))
{
echo "<td>[yor Vars here]</td>";
}
echo "</tr>";
}
?>
I am using a for loop to construct a HTML table from the contents of a MySQL table select query. I have a link on the end of each row to copy that row into another table.
I'm unsure how to get the data from the table row for the MySQL insert query - I have marked the place where I'm struggling with XXX.
<?php
mysql_select_db("cardatabase");
$link = mysql_connect("localhost", "root", "password");
$query = "SELECT * from cars";
$result = mysql_query($query);
if($_GET['rent']) {
$rent = "INSERT INTO rentedcars VALUES('XXX','XXX','XXX','XXX','XXX','XXX','XXX','XXX','XXX','XXX')";
mysql_query($rent);
echo "<meta http-equiv='refresh' content='0;url=rent.php'/>";
}
echo "<table>";
echo "<tr><td>ID</td><td>Make</td><td>Model</td><td>Fuel Type</td><td>Transmission</td><td>Engine Size</td><td>Doors</td><td>Amount</td><td>Available</td><td>Date Added</td><td>Remove</td></tr>";
for ($i = 0; $i < mysql_num_rows($result); $i++) {
$row = mysql_fetch_object($result);
echo "<tr>
<td>$row->ID</td>
<td>$row->CARMAKE</td>
<td>$row->CARMODEL</td>
<td>$row->FUELTYPE</td>
<td>$row->TRANSMISSION</td>
<td>$row->ENGINESIZE</td>
<td>$row->DOORS</td>
<td>$row->AMOUNT</td>
<td>$row->AVAILABLE</td>
<td>$row->DATEADDED</td>
<td><a href='?rent=$row->ID'>Rent</a></td>
</tr>";
}
echo "</table>";
edit (updated code):
<?php
mysql_select_db ("cardatabase");
$link = mysql_connect ("localhost", "root", "password");
$query = "SELECT * from cars";
$result = mysql_query ($query);
if($_GET['rent']) {
$query_car = sprintf("SELECT * from cars WHERE ID=%s",$_GET['rent']);
$rslt = mysql_query($query_car);
$car = mysql_fetch_object ($rslt);
$rent = "INSERT INTO rentedcars VALUES('$car->ID','$car->CARMAKE','$car->CARMODEL','$car->FUELTYPE','$car->TRANSMISSION','$car->ENGINESIZE','$car->DOORS','$car->AMOUNT','$car->AVAILABLE','$car->DATEADDED')";
mysql_query($rent);
echo "<meta http-equiv='refresh' content='0;url=rent.php'/>";
}
echo "<table>";
echo "<tr>";
echo "<td>ID</td><td>Make</td><td>Model</td><td>Fuel Type</td><td>Transmission</td><td>Engine Size</td><td>Doors</td><td>Amount</td><td>Available</td><td>Date Added</td><td>Remove</td>";
echo "</tr>";
while ($row = mysql_fetch_object($result)) {
echo "<tr>
<td>$row->ID</td>
<td>$row->CARMAKE</td>
<td>$row->CARMODEL</td>
<td>$row->FUELTYPE</td>
<td>$row->TRANSMISSION</td>
<td>$row->ENGINESIZE</td>
<td>$row->DOORS</td>
<td>$row->AMOUNT</td>
<td>$row->AVAILABLE</td>
<td>$row->DATEADDED</td>
<td><a href='?rent=$row->ID'>Rent</a></td>
</tr>";
}
echo "</table>";
mysql_* is deprecated as of PHP 5.5.0, you should use something like PDO.
try {
$DBH = new PDO('mysql:dbname=cardatabase;host=localhost', 'root', 'password');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$STH = $DBH->query("SELECT * FROM cars")->execute();
while ($row = $STH->fetch(PDO::FETCH_OBJ)) {
echo "<tr>
<td>$row->ID</td>
<td>$row->CARMAKE</td>
<td>$row->CARMODEL</td>
<td>$row->FUELTYPE</td>
<td>$row->TRANSMISSION</td>
<td>$row->ENGINESIZE</td>
<td>$row->DOORS</td>
<td>$row->AMOUNT</td>
<td>$row->AVAILABLE</td>
<td>$row->DATEADDED</td>
<td><a href='?rent=".$row->ID."'>Rent</a></td>
</tr>";
}
Edit: Just like #Skatox said!
I would do it like this:
<?php
$link = mysql_connect ("localhost", "root", "password");
mysql_select_db ("cardatabase");
$query = "SELECT * from cars";
$result = mysql_query ($query);
Get car information and store it
if($_GET['rent'])
{
$query_car = sprintf("SELECT * from cars WHERE ID=%s",$_GET['rent']); //Avoids sql injection
$rslt = mysql_query($query_car);
$car = mysql_fetch_object ($rslt)
Here you need to validate if there's no car
$rent = "INSERT INTO rentedcars VALUES('$car->ID','$car->CARMAKE','$car->CARMODEL','$car->FUELTYPE','$car->TRANSMISSION','$car->ENGINESIZE','$car->DOORS','$car->AMOUNT','$car->AVAILABLE','$car->DATEADDED')";
mysql_query($rent);
echo "<meta http-equiv='refresh' content='0;url=rent.php'/>";
}
Change it to while like #Vinoth Babu said:
while ($row = mysql_fetch_object ($result))
{
$row = mysql_fetch_object ($result);
echo "<tr>
<td>$row->ID</td>
<td>$row->CARMAKE</td>
<td>$row->CARMODEL</td>
<td>$row->FUELTYPE</td>
<td>$row->TRANSMISSION</td>
<td>$row->ENGINESIZE</td>
<td>$row->DOORS</td>
<td>$row->AMOUNT</td>
<td>$row->AVAILABLE</td>
<td>$row->DATEADDED</td>
<td><a href='?rent=$row->ID'>Rent</a></td>
</tr>";
}
print "</table>";
?>
I would recommend you to switch to MySQL PDO, it's safer and you'll get a better and secure code.
you are missing the column names in your insert query
$rent = "INSERT INTO rentedcars (id ,carmake, carmodel,fueltype,transmission, enginesize,doors,amount ,available, dateadded)
VALUES('xxx','xxx','".$carmodel."','XXX','XXX','XXX','XXX','XXX','XXX','XXX')";
^^^^^-------------i showed u exempel under
those XXX are values you get the from the inputs values
exemple
<input name= "car_model" id= "car_model" value="mercedes" >
then you get this value
if (isset($_POST['car_model'])){ $carmodel = $_POST['car_model']}
and then use this value $carmodel in your sql
I have no actual php errors, but When I make my query this I get output :
$sql_result = "SELECT AuthorFirst, AuthorLast, OnHand, Title
FROM Inventory i, Wrote w, Author a, Book b
WHERE i.BookCode = b.BookCode AND i.BookCode = w.BookCode
AND a.AuthorNum = w.AuthorNum AND 2 = i.BranchNum";
When I change the 2 to a variable in the last line I get no output. Here is my full code:
<?php
$sql_branch = "SELECT BranchNum
FROM Branch
WHERE BranchName = '$_POST[branch]'";
$sql_result = "SELECT AuthorFirst, AuthorLast, OnHand, Title
FROM Inventory i, Wrote w, Author a, Book b
WHERE i.BookCode = b.BookCode AND i.BookCode = w.BookCode
AND a.AuthorNum = w.AuthorNum AND '$branch[BranchNum]' = i.BranchNum";
$connect = mysql_connect('students', 'xxxxx', 'xxxxx') or exit(mysql_error());
mysql_select_db('henrybooks', $connect);
if($branch_result = mysql_query($sql_branch, $connect)) {
$branch = mysql_fetch_array($branch_result);
}
else {
echo mysql_error();
}
if(mysql_query($sql_result, $connect)) {
$result = mysql_query($sql_result, $connect);
}
else {
echo mysql_error();
}
echo $branch[BranchNum];
echo "<table>
<tr>
<td>Author</td>
<td>Title</td>
<td>Number Available</td>
</tr>";
while( $row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row['AuthorFirst'].$row['AuthorLast']."</td>";
echo "<td>".$row['Title']."</td>";
echo "<td>".$row['OnHand']."</td>";
echo "</tr>";
}
echo "</table>";
?>
I feel like I don't fully understand the mysql_fetch_array() function and I'm doing something wrong as far as the variable name, but I'm unsure.
Thanks!
when I echo $sql_result it displays as such:
SELECT AuthorFirst, AuthorLast, OnHand, Title
FROM Inventory i, Wrote w, Author a, Book b
WHERE i.BookCode = b.BookCode AND i.BookCode = w.BookCode AND a.AuthorNum = w.AuthorNum AND '' = i.BranchNum
For some reason the variable is void here... but when I echo the variable itself it has a value.
Looking at the code, it looks $sql_result is being set before $branch. Try defining $sql_result after executing the mysql_fetch_array($branch_result). Move it to the same location where you have the "echo $branch[BranchNum];" line and see what happens.