No results on select
I've tried this for hours. I can connect to my data base and insert into it but can't seem to get anything out of it.
// Declarations
$connection = mysql_connect("xxxxxxx", "xxxxxxx", "xxxxxxxx");
mysql_select_db("kettle_test1", $connection);
if (!$connection)
{
die ('Cold not connect: ' . mysql_error());
}
else
{
echo "connected <br> <br>";
}
// INSERT
mysql_query("INSERT INTO bob2 (Danumber)
VALUES ('32234245')");
// QUERY AND DISPLAY
$result = mysql_query("SELECT * FROM bob2");
ec开发者_运维知识库ho $result;
// if this is commented out I get the 'connected' message above. without it the page is blank,
while ($row = mysql_fetch_assoc($result)) {
echo $row["Danumber"];
}
*/
echo "<br> <br> <br>";
There are a lot of possibilities here. You could try, for example, the following things:
- Use
mysql_num_rows
to check how many rows have been returned - Use
print_r
to print the whole row. - Use
isset
to check that the key "Danumber" actually exists
Summarizing: use a lot more checks to make sure things you retrieve are actually there.
I would first test if inserting really went ok:
// INSERT
$result = mysql_query("INSERT INTO bob2 (Danumber)
VALUES ('32234245')");
if (!$result) {
die('Invalid insert query: ' . mysql_error());
}
Otherwise you could not select later on. But that's probably not the cause of your problem.
But you can do the same for the SELECT
query as well:
// QUERY AND DISPLAY
$result = mysql_query("SELECT * FROM bob2");
if (!$result) {
die('Invalid select query: ' . mysql_error());
}
That done you can verify if the queries went through w/o errors or not at least.
Then you should ensure that you can fetch and display the data:
// if this is commented out I get the 'connected' message above. without it the page is blank,
while ($row = @mysql_fetch_assoc($result)) {
print_r(array_keys($row));
print_r($row["Danumber"]);
}
精彩评论