Re: Get values from a DB.
What error? Always specify what error.
The query doesn't look right to me. What's dss supposed to do? It's a table name, yet you use it like a function. And if it's a function, then you will only get one result column, namely the result of the function.
Re: Get values from a DB.
The table name is DSS, the database name is home_app
i want to search my DB for that criterial above, and display all the results with the while loop.
Re: Get values from a DB.
Your query is written wrong. Your while() loop and if() statement also have the wrong syntax: if you open an IF statement before the loop, you can't put an ELSE statement before the loop ends. You have to end the loop, then do the ELSE statement.
PHP Code:
<?php
require_once ('mysql_connect.php');
$query = "SELECT MBlock_Part_Number, MBlock_Ref FROM dss ORDER BY MBlock_Part_Number";
$result = @mysql_query ($query);
$count = mysql_num_rows($result); //number of results
if ($count > 0){
echo "found $count results.<br /><br />\n";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo ' <tr><td align="left">' . $row['MBlock_Part_Number'] . '</td><td align="left">' . $row['MBlock_Ref'] . '</td></tr>';
}
}else{
echo "No results found";
}
?>
Re: Get values from a DB.
worked perfect
kows i love you :)
Re: Get values from a DB.
ok weird, when i take the code out of the page you made me it works, but when i keep it in there it dosent.
I wanted to run 2 different querys from the same page.
1 is where a person can enter there info, then it enters it into the DB, and thenthe one above, i can run either or of the 2 codes when i have them seperate, but not together.
PHP Code:
<?php
if(isset($_POST['submitted'])){$errors = array();
}
if(empty($_POST['partnumber'])){$errors[] = 'You forgot a part number. ';
}else{$partnumber = trim($_POST['partnumber']);
}
if(empty($_POST['refnumber'])){$errors[] = 'You forgot a ref number. ';
}else{$refnumber = trim($_POST['refnumber']);
}
if(empty($_POST['partcomments'])){$errors[] = 'You forgot part comments. ';
}else{$partcomments = trim($_POST['partcomments']);
}
IF (empty($errors)){
require_once ('mysql_connect.php');
$query = "INSERT INTO dss(MBlock_Part_Number,MBlock_Ref,MBlock_Defect,MBlock_Date) VALUES ('$partnumber','$refnumber','$partcomments',NOW())";
$result = @mysql_query($query);
if($result){echo 'Information entered into DB';
mysql_close();
}else{echo '<p>'. mysql_error(). $query . '</p>';
include("DSS_Footer.php");
exit();
}}?>
<?php
require_once ('mysql_connect.php');
$query = "SELECT MBlock_Part_Number, MBlock_Ref FROM dss ORDER BY MBlock_Part_Number";
$result = @mysql_query ($query);
$count = mysql_num_rows($result); //number of results
if ($count > 0){
echo "found $count results.<br /><br />\n";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo ' <tr><td align="left">' . $row['MBlock_Part_Number'] . '</td><td align="left">' . $row['MBlock_Ref'] . '</td></tr>';
}
}else{
echo "No results found";
}
?>
Re: Get values from a DB.
what do you mean? does it just not run, or do you get errors? if so, what errors do you get?
Remove the second require_once() call (no point in trying to include it twice for no reason). require_once() is only used to stop that page from being included/evaluated more than once, so your second require_once() does absolutely nothing. Also, don't forget to move mysql_close() down to below the code that displays the results, so that you don't close the connection before the second query is ran (this might be your actual problem).
the only other thing I can think of, other than those listed above, is to change the second variable names of $query and $result, to something like $query2 and $result2, so that there can be no unintentional conflict between the two queries.
Re: Get values from a DB.
When i go to the page, its fine, the query you gave me above works perfectly.
But also on the same page, i have code where i can enter data into fields, hit the submit button and it puts data into the DB, now that works, but i get this message now with it.
Information entered into DB
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Inetpub\jf1\php\testing2.php on line 38
No results found
Re: Get values from a DB.
Ok when i first go to my page, it looks like this below. (i typed in the field box's)http://www.imagesticky.com/userimages/25051.jpg
Then when i hit "Enter Button" it does this:
http://www.imagesticky.com/userimages/80652.jpg
Here is my code:
PHP Code:
<?php include("DSS_Header.php"); ?>
<?php require_once ('mysql_connect.php'); ?>
<?php
if(isset($_POST['submitted'])){$errors = array();
}
if(empty($_POST['partnumber'])){$errors[] = 'You forgot a part number. ';
}else{$partnumber = trim($_POST['partnumber']);
}
if(empty($_POST['refnumber'])){$errors[] = 'You forgot a ref number. ';
}else{$refnumber = trim($_POST['refnumber']);
}
if(empty($_POST['partcomments'])){$errors[] = 'You forgot part comments. ';
}else{$partcomments = trim($_POST['partcomments']);
}
IF (empty($errors)){
$query = "INSERT INTO dss(MBlock_Part_Number,MBlock_Ref,MBlock_Defect,MBlock_Date) VALUES ('$partnumber','$refnumber','$partcomments',NOW())";
$result = @mysql_query($query);
if($result){echo 'Information entered into DB';
mysql_close();
}else{echo '<p>'. mysql_error(). $query . '</p>';
include("DSS_Footer.php");
exit();
}}?>
<?php
$query = "SELECT MBlock_Part_Number, MBlock_Ref FROM dss ORDER BY MBlock_Part_Number";
$result = @mysql_query ($query);
$count = mysql_num_rows($result); //number of results
if ($count > 0){
echo "found $count results.<br /><br />\n";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo ' <tr><td align="left">' . $row['MBlock_Part_Number'] . '</td><td align="left">' . $row['MBlock_Ref'] . '</td></tr>';
}
}else{
echo "No results found";
}
?>
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td><form action="testing2.php" method="post">
<fieldset>
<span style="font-size:small;font:sans serif;">Part Number:</span><br><input type="text" name="partnumber" size="20" maxlenght="20" /><br>
<span style="font-size:small;font:sans serif;">Ref #:</span><br><input type="text" name="refnumber" size="20" maxlenght="20" /></td>
<td valign="top"><fieldset>Date: <?php echo date('M n, Y'); ?></td>
<td rowspan="2" valign="top" width="510"><fieldset><span style="font-size:small;font:sans serif;">DSS Overview:</span>
<li><span style="font-size:small;font:sans serif;">0 : Massage Blocks Pending</span>
<li><span style="font-size:small;font:sans serif;">0 : Inverters Pending</span>
<li><span style="font-size:small;font:sans serif;">0 : Total Items Pending</span>
</fieldset>
</td>
</tr>
<tr>
<td colspan="2"><fieldset><span style="font-size:small;font:sans serif;">Part Comments:</span><br> <textarea name="partcomments" rows="6" cols="40">
</textarea><br><input type="submit" name="submit" value="Enter" align="right"/>
</fieldset>
</form></td>
</tr>
</table>
<?php include("DSS_Footer.php"); ?>
Re: Get values from a DB.
That's because you call mysql_close() after processing the INSERT query. With this call, you close the connection, so the SELECT query later tries to work on a closed connection.
At least that's my guess. Remove the mysql_close() and try again. PHP closes the connection anyway, so the call is not necessary.
Re: Get values from a DB.
Re: Get values from a DB.
Quote:
Originally Posted by kows
Also, don't forget to move mysql_close() down to below the code that displays the results, so that you don't close the connection before the second query is ran (this might be your actual problem).
always helps if you read :)