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.
Related
$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>";
}
?>
Whoops firgured otu where my parse error was. I had my semi-colon inside my first query.
So essentially I am trying to query with three different select statements in the same PHP script. Is this possible? (Last question I promise, after this I think the basics should get me a few weeks without having to ask more)
<?php
include("server_connect.php");
mysql_select_db("rnissen");
$query = "SELECT column_one, column_two, column_four FROM tbltable;"
$results = mysql_query($query) or die(mysql_error());
$querytwo = "SELECT column_one, column_two, column_five FROM tbltable WHERE column_five = 1989";
$results = mysql_query($querytwo) or die(mysql_error());
$querythree = "SELECT COUNT(column_five) FROM tbltable WHERE column_five = 1989";
$results = mysql_query($querythree) or die(mysql_error());
?>
Part Two
Ok so I changed the code as suggested and tried to add it into a table. I'm still getting
Parse error: syntax error, unexpected '$results1' (T_VARIABLE) in C:\xampp\htdocs\3718\assign5SELECT.php on line 7
I tried it without the table and it is still the same error. Is there something I am missing? Here is the updated code with the new variables.
mysql_select_db("rnissen");
$query = "SELECT column_one, column_two, column_four FROM tbltable;"
$results1 = mysql_query($query) or die(mysql_error());
echo "Column One, Column Two, Column Four : </br>";
echo "<table border=\"1\">\n";
while ($row1 = mysql_fetch_assoc($results1)) {
echo "<tr>\n";
foreach($row1 as $value1) {
echo "<td>\n";
echo $value1;
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
$querytwo = "SELECT column_one, column_two, column_five FROM tbltable WHERE column_five = 1989";
$results2 = mysql_query($querytwo) or die(mysql_error());
echo "Column One, Column Two, Column Five : </br>";
echo "<table border=\"1\">\n";
while ($row2 = mysql_fetch_assoc($results2)) {
echo "<tr>\n";
foreach($row2 as $value2) {
echo "<td>\n";
echo $value2;
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
$querythree = "SELECT COUNT(column_five) FROM tbltable WHERE column_five = 1989";
$results3 = mysql_query($querythree) or die(mysql_error());
echo "Column 4 has this many 1989s : </br>";
echo "<table border=\"1\">\n";
while ($row3 = mysql_fetch_assoc($results3)) {
echo "<tr>\n";
foreach($row3 as $value3) {
echo "<td>\n";
echo $value3;
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
$query = "SELECT column_one, column_two, column_four FROM tbltable;"
$results = mysql_query($query) or die(mysql_error());
$querytwo = "SELECT column_one, column_two, column_five FROM tbltable WHERE column_five = 1989";
$results = mysql_query($querytwo) or die(mysql_error());
$querythree = "SELECT COUNT(column_five) FROM tbltable WHERE column_five = 1989";
$results = mysql_query($querythree) or die(mysql_error());
The problem you are encountering is that you are overwriting your results by using the same variable name.
Example:
$Var = "test";
echo $Var; // Will output "test"
$Var = "Another String";
echo $Var; // Will output "Another String" rather than "test";
So append:
$Results_a = ...;
$Results_b = ...;
$Results_c = ...;
So you can work with your variables easily, another thing you could look at is SQL joins: http://dev.mysql.com/doc/refman/5.0/en/join.html to cut down your amount of seperate queries
You need to save the queries to a different variable:
<?php
include("server_connect.php");
mysql_select_db("rnissen");
$query = "SELECT column_one, column_two, column_four FROM tbltable;"
$results1 = mysql_query($query) or die(mysql_error());
$querytwo = "SELECT column_one, column_two, column_five FROM tbltable WHERE column_five = 1989";
$results2 = mysql_query($querytwo) or die(mysql_error());
$querythree = "SELECT COUNT(column_five) FROM tbltable WHERE column_five = 1989";
$results3 = mysql_query($querythree) or die(mysql_error());
?>
However, this is a more convenient way to do perform multiple queries using mysqli_multi_query:
Example from the docs:
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if (mysqli_multi_query($link, $query)) {
do {
/* store first result set */
if ($result = mysqli_store_result($link)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
}
/* print divider */
if (mysqli_more_results($link)) {
printf("-----------------\n");
}
} while (mysqli_next_result($link));
}
Edit:
For your updated question, you are missing a semicolon here:
$query = "SELECT column_one, column_two, column_four FROM tbltable";
I have been stuck with a problem and I am newbie in mysql and php, Here is the code first , so that I can explain in detail:
$metros = array(1,263);
foreach($metros as $metro_id) {
$sql = "SELECT cuisine_id, cuisine_name_en FROM poi_restaurant_cuisines";
$result = mysql_query($sql);
$cuisine_id = array();
$cusine_name = array();
while($row = mysql_fetch_assoc($result)) {
$cuisine_id[] = $row['cuisine_id'];
$cuisine_name[] = $row['cuisine_name_en'];
}
foreach ($cuisine_id as $cuisine) {
$sql = "
SELECT COUNT(*)
FROM poi AS p
LEFT JOIN poi_restaurant AS pr USING (poi_id)
WHERE p.poi_address_prefecture_id = '$metro_id'
AND pr.poi_restaurant_cuisine_id_array
AND find_in_set('$cuisine', poi_restaurant_cuisine_id_array)
AND p.poi_status = 1";
$result = mysql_query($sql);
$count_cuisine = array();
while($row = mysql_fetch_array($result)) {
$count_cuisine[$metro_id][$cuisine] = $row['COUNT(*)'];
}
echo "<table border = 1 cellpadding= 5 cellspacing= 5 width= 100>";
echo "<tr><th>CuisineID</th><th>Count</th></tr>";
echo "<tr><td>";
echo $cuisine;
echo "</td><td>";
echo $count_cuisine[$metro_id][$cuisine];
echo "</td><td>";
echo "</tr>";
echo "</table>";
}
}
The poi_restaurant_cuisine_id_array contains csv values. I am able to produce the count and the cuisine ID on the web page. I want to replace the cuisine ID with the name of the cuisine. I am not very good at sql or either PHP as I am a beginner. I hope I am being clear enough. Any help is highly appreciated ...Thank you.
Try this:
echo '<table border="1" cellpadding="5" cellspacing="5" width="100">';
echo "<tr><th>Cuisine</th><th>Count</th></tr>";
$metros = array(1,263);
foreach($metros as $metro_id) {
$sql = "SELECT cuisine_id, cuisine_name_en FROM poi_restaurant_cuisines";
$result = mysql_query($sql);
$cuisines = array();
while($row = mysql_fetch_assoc($result)) {
$cuisines[] = array(
'id' => $row['cuisine_id'],
'name' => $row['cuisine_name_en'],
);
}
foreach ($cuisines as $cuisine) {
$sql = "
SELECT COUNT(*)
FROM poi AS p
LEFT JOIN poi_restaurant AS pr USING (poi_id)
WHERE p.poi_address_prefecture_id = '$metro_id'
AND pr.poi_restaurant_cuisine_id_array
AND find_in_set('{$cuisine['id']}', poi_restaurant_cuisine_id_array)
AND p.poi_status = 1";
$result = mysql_query($sql);
$count_cuisine = array();
while($row = mysql_fetch_array($result)) {
$count_cuisine[$metro_id][$cuisine['id']] = $row['COUNT(*)'];
}
echo "<tr>
<td>{$cuisine['name']}</td>
<td>{$count_cuisine[$metro_id][$cuisine['id']]}</td>";
</tr>";
}
}
echo "</table>";
Assuming cuisine_id is a unique identifier, then just use it as the array index....
while($row = mysql_fetch_assoc($result)) {
$cuisines[$row['cuisine_id']] = $row['cuisine_name_en'];
}
....
foreach ($cuisines as $cuisine_id=>$cuisine_name_en) {
However storing multiple values in a single column is a very bad idea.
Generating and running queries in a loop is another very bad idea.
It is possible to reduce this to a single query, declared and invloked outside the inner loop but because your data is not mormalized, this is rather complex.
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);
}
I have the following code I want to run, but the problem is $this->type is set when the class is created by specifying either petition, proposal, or amendment. As you can see my $sql statement is a UNION of all three, and I want to specify which table (pet,prop,or amend) each row of data comes from.
public function userProposals() {
$username = User::getUsername();
$sql = "SELECT * FROM petition WHERE author = '$username'
UNION SELECT * FROM proposition WHERE author = '$username'
UNION SELECT * FROM amendment WHERE author = '$username'";
$query = mysql_query($sql);
$state = User::userState();
while ($row = mysql_fetch_assoc($query)) { // $this->type needs to specify pet,prop,amend
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=$this->type&id=$row[id]'>$row[title]</a></td>
<td>$this->type</td>
<td>$state</td>
</tr>";
}
}
As you can see, $this->type will only say one of the three. To get my function work how i wanted to, I did this (which I feel is too long & there must be a shorter way).
public function userProposals() {
$username = User::getUsername();
$state = User::userState();
$sql = "SELECT * FROM petition WHERE author = '$username'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=petition&id=$row[id]'>$row[title]</a></td>
<td>Petition</td>
<td>$state</td>
</tr>";
}
$sql = "SELECT * FROM proposition WHERE author = '$username'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=proposition&id=$row[id]'>$row[title]</a></td>
<td>Proposition</td>
<td>$state</td>
</tr>";
}
$sql = "SELECT * FROM amendment WHERE author = '$username'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=amendment&id=$row[id]'>$row[title]</a></td>
<td>Amendment</td>
<td>$state</td>
</tr>";
}
}
I would normally do something like the following:
public function userProposals() {
$username = User::getUsername();
$state = User::userState();
$tables = array('petition', 'proposition', 'amendment');
foreach($tables as $table) {
$label = ucwords($table);
$sql = "SELECT * FROM $table WHERE author = '" . mysql_real_escape_string($username) . "'";
$query = mysql_query($sql);
while ($row = mysql_fetch_assoc($query)) {
echo "
<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=$table&id=$row[id]'>$row[title]</a></td>
<td>$label</td>
<td>$state</td>
</tr>";
}
}
}
Why don't you try the modified SQL:
SELECT 'petition' as typ,title,id FROM petition
WHERE author = '$username'
UNION SELECT 'proposition' as typ,title,id FROM proposition
WHERE author = '$username'
UNION SELECT 'amendment' as typ,totle,id FROM amendment
WHERE author = '$username'"
and then use the typ from each returned row ($row[typ]) instead of $this->type?
The whole thing should be:
public function userProposals() {
$username = User::getUsername();
$sql = "SELECT 'petition' as typ,id,title FROM petition
WHERE author = '$username'
UNION SELECT 'proposition' as typ,id,title FROM proposition
WHERE author = '$username'
UNION SELECT 'amendment' as typ,id,title FROM amendment
WHERE author = '$username'"
$query = mysql_query($sql);
$state = User::userState();
while ($row = mysql_fetch_assoc($query)) {
echo "<tr>
<td>$row[id]</td>
<td><a href='viewproposal.php?type=$row[typ]&id=$row[id]'>
$row[title]
</a></td>
<td>$row[typ]</td>
<td>$state</td>
</tr>";
}
}
based on what was in your question.